For a school project we need to move objects with cranes. At first we thought we could attach an object node to the crane with attachChild() and when it reaches it’s destination detach it and attach it to the rootNode. This doesn’t work however, as the object’s local translation gets applied when it’s attached to the rootNode too, therefore changing it’s position in the world. Is there a better way to do this, or are we just looking at this wrong?
It would greatly help your understanding if you spend 15 minutes carefully going over the jMonkeyEngine3 Scene Graph for Dummies.
You need to think carefully about which nodes are the parents, and which is the child… then which one you should be moving.
I should probably have been more clear. We can move the objects without any problems, it’s just that when we try to re-attach them to another node they appear at a different location. I’ll go over the Scene Graph for Dummies again, regardless.
- let it attached to the rootNode (or what ever node it’s attached to) and update its location via a control
let’s say you have a “hook” object at the tip of the crane, the control would be attached to the picked object and would copy the position of the hook to the object. Remove the control when you drop the object
- Keep the same logic as you did but compute and update the new relative position from the new parent when picking and dropping the object (basically a couple of vector subtractions).
- compute offset translation between the object node and the new parent (subtract world translation of both nodes)
- set local translation of the picked node to the resulting vector
- attach the picked node to the new node. (note that attaching a spatial to a node automatically removes it from its former parent)
We were thinking something along the line of option 2 too. We’ll try that and I’ll let you know how it went. Thanks.
It’s not too hard.
For the new rootNode location of an ‘object’ attached somewhere else:
Vector3f world = object.getParent().localToWorld( object.getLocalTranslation(), null )
Then detach object, set its local translation to ‘world’ from above and attach it to rootNode.
@nehon’s approach won’t take rotations of the parent hierarchy into account.
Hmm… actually, in this case it may be even simpler since object.getWorldTranslation() will tell you where it should be as a local translation of root node. And getWorldRotation() will tell you its orientation. So:
Vector3f worldTrans = object.getWorldTranslation();
Quaternion worldRot = object.getWorldRotation();
…or something like that.