and whenever the controller moves I add this offset back in
var pos = this.controller.getPosition().add(this.grabbedPosOffset);
which seems to work ok for the position but I don’t think is the right way to move it. My gut feeling is that you store the orientation of the controller and the relative position of the grabbed entity. And then when the controller moves you rotate the grabbedEntity around the controller somehow. I am just not sure how to go about it. Can anyone show me and example or give me a steer in the right direction (pun not intended)
Sometimes I struggle to understand exactly what you are asking
Principle is like this:
Get target position.
Get current position of an object.
If distance is too far of minimum (probably few centimeters).
Then copy current position, subtract target position.
Normalize that vector, and scale to speed of moving * dt.
If vector is longer than distance, then normalize it and scale by distance.
Add current position to that vector.
Set position of grabbed object to that vector.
This will do linear speed grabbing.
Target position shall be probably in front of your camera.
If you want eased “elastic” dragging, then change option 5 to possibly: scale by 0.95. Or play with that number. You need to do that dt dependant as well, to prevent different speeds on different FPS’s.
Sorry I am not making myself very well understood. The problem is one of both position and orientation.
Imagine that you have a parent model and a child model. The parent is the torch and the child is the flame. Moving the torch should also move the flame so that it stays at the top of the torch and the flame needs to always be in the correct orientation to the torch. Basically I want to do manually what the PlayCanvas engine does automatically when you create a child entity.
One important thing to remember is that Vec’s, Mat’s and Quat’s are mutable - means that they are actual references, not copies. Making copy - is expensive in real-time it pollutes GC. So you want to prepare some Vec’s in advance, and copy values to them - reuse them.
3D Maths experience comes with experimentation. I still get confused sometimes with all those transformations etc, but it gets better when you play and experiment with it.