Local transformations of objects

I’m trying to figure out the axes of objects. The problem is that when the object rotates, the axis remains in the same position. And I can not move one of the axes, for example, in a blender when switching to local axes, the object moves along its own axes, and not the world axes.

Example we have an object - a cube:

http://imgur.com/JWoBa52

I moved it along the x-axis to 1.

cube.setLocalTranslation(new Vector3f(1, 0, 0));

Imgur

All perfectly. Now I pre-rotate it to 45 degrees.

float[] angles = {0, 0.785398f, 0};
cube.getLocalRotation().fromAngles(angles);

http://imgur.com/q5jWWm0

And I move it again along the x axis.

cube.setLocalTranslation(new Vector3f(1, 0, 0));

http://imgur.com/IIhoUs0

But something went wrong, because I’m moving in its axis x, and not the world. However, logically, it should look like this:

Imgur

The actual issue is that, obviously, jME does not rotate the object matrix. How can I force it. So he moved along the logic in the last image?

And another question why get, not set.

cube.getLocalRotation().fromAngles(angles);

1 Like

https://jmonkeyengine.github.io/wiki/tutorials/scenegraph/assets/fallback/index.html

The location of a spatial controls its location in parent space. If you want to rotate what location movement means then you need to rate the parent. Or rotate the vectors that you want to move by.

Because you are abusing the API. getLocalRotation() will return the Quaternion that the Spatial is actually using… not a copy. (because that would be horribly horribly wasteful: see other thread)

So you are forcing the rotation to change without telling the spatial about it and will have strange bugs later.

You should really be using:
cube.setLocalRotation(new Quaternion().fromAngles(angles));

…Or if you really want to reuse the quaternion just to overwrite all of its values:
cube.setLocalRotation(cube.getLocalRotation().fromAngles(angles));

And then if you want move the cube along its local Z:
cube.move(cube.getLocalRotation().mult(new Vector3f(0, 0, 1)));

Edit: fixed a typo in the movement vector.

2 Likes

Concerning the API, getLocalRotation (). FromAngles (angles)

I do not quite understand the point, but this code is mentioned on the Wiki - quaternion.

1 Like

Unless you call setLocalRotation(), the Spatial doesn’t know that the rotation has been updated and things like world bounds, etc. won’t be refreshed.

Yes, the wiki is very very very wrong in this case.

1 Like