Problem with rotating spatials around their center of mass

There’s probably a straightforward solution to this, but I’m not seeing it.

I am trying to apply rotations to some small parts of a larger model. Can anybody tell me what’s wrong with my reasoning here?

Let the big model origin be O, and the small parts center of mass origin be o. When extracting smaller parts of the model, these will have the same origin O initially, so applying a rotation simply by calling Spatial.rotate() will not work - the spatial will rotate about O, not it’s center of mass o.

I know the vector describing the center of mass o related to O - let this be p.

In order to rotate about o, I make myself a rotationNode, then translate the rotationNode by p. It is now located in o.

My unit rotations are simply defined by these quaternions:

[java]Quaternion xRotation = new Quaternion().fromAngleAxis(FastMath.QUARTER_PI, Vector3f.UNIT_X);
Quaternion yRotation = new Quaternion().fromAngleAxis(FastMath.QUARTER_PI, Vector3f.UNIT_Y);
Quaternion zRotation = new Quaternion().fromAngleAxis(FastMath.QUARTER_PI, Vector3f.UNIT_Z);

How do I go from here to making the spatial rotate around its center of mass by these unit rotations? Note that I cannot apply a translation to the Spatial first, as this would move it out of place (it does not have any information on it’s location relative to the parent model). I need to make the spatial rotate around the new origin o, as defined by the rotationNode, rather than its “actual” origin O.

Basically, I want a way to rotate the spatial around an arbitrary point [x,y,z] without moving the spatial.

Just to clarify what I’m doing in code. I have a rootNode, a rotationNode, my position vector and my spatial as described above:

[java]Node rotationNode = new Node();

Okay, I figured out what I was actually looking at.

My rotationNode.move§ also moves the spatial with it, so the spatial rotates around the correct point.

Okay, I figured it out. :P. Because the rotationNode.attachChild(mySpatial) also moves the Spatial by p (which is something I don’t want as the Spatial is already in the correct location), I have to then move just the spatial back by -p. Basically:


And now it works! I didn’t think of the fact that the rotationNode applies its transform to the children whichever order you choose to do things.

This might be helpful: