Question about local coordinates

Let’s say I have a Box centered at the origin:

[java]Box b = new Box (Vector3f.ZERO, 5, 5, 5);[/java]

Now, let’s say I rotate that Box’s geometry by 90 degrees:

[java]g.rotate(0, FastMath.PI/2f, 0);[/java]

What I would like to have happen is that the Box’s local coordinate system also rotates. For instance, the Box’s z-vector should always be pointing in front of it (if we assume that the “front” was originally in the world.z direction). So, in terms of world coordinates, the Box’s “z” is now the world’s “x”. Of course, I’d like this to be the case for any arbitrary rotation.



Any thoughts?



Just to clarify(!), the following code should move the box in the world’s “x” direction, and not the world’s “z” direction…

[java]

Box box = new Box(Vector3f.ZERO, 5, 5, 5);

Geometry geom = new Geometry(“side1”, box);

Material mat = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

mat.setBoolean(“UseMaterialColors”, true);

mat.setColor(“Ambient”, ColorRGBA.Black);

mat.setColor(“Diffuse”, ColorRGBA.Blue);

mat.setColor(“Specular”, ColorRGBA.White);

mat.setFloat(“Shininess”, 12);

geom.setMaterial(mat);

geom.rotate(0, FastMath.PI/2f, 0);

geom.setLocalTranslation(Vector3f.UNIT_Z);

rootNode.attachChild(geom);

[/java]



… even though the parameter Vector3f.UNIT_Z is passed… because I really do want the Box’s local translation to be in it’s z-direction, or in the direction it is “facing”.



Hope this makes sense!

It doesn’t work that way. Translation controls where the box’s origin is. Rotation and scale then control where the children are relative to that origin… in this case the children are the vertexes in the mesh.



What you’ve actually asked for is almost two different translations anyway… if you work out what happens over time then things will get really confused.



If you want to place a box geometry one meter in front of itself then use geom.localToWorld() to calculate what the world position should be.

pspeed said:
If you want to place a box geometry one meter in front of itself then use geom.localToWorld() to calculate what the world position should be.


Thanks for your reply.

How do I know what to pass as the in-Vector to geom.localToWorld? I mean, you're correct that I want the box geometry to move forward, but how do I know what forward is? The box could be facing any direction.

local to world will translate a point in the box’s coordinate space to world. So “in front of the box” is down the Z axis just like you originally wanted.



localToWorld( new Vector3f(0,0,1), null ) should give you a point one meter “in front of” the box based on its current orientation.

pspeed said:
local to world will translate a point in the box's coordinate space to world. So "in front of the box" is down the Z axis just like you originally wanted.
localToWorld( new Vector3f(0,0,1), null ) should give you a point one meter "in front of" the box based on its current orientation.

So would you think the following two lines of code would accomplish the same thing, assuming that the geometry has not had any previous transform applied to it?

[java]
geom.setLocalTranslation(0f, 0f, 5f);

// or...

geom.setLocalTranslation(geom.localToWorld(Vector3f.ZERO.setZ(5f), null));
[/java]

Just a note: do not EVER and I mean NEVER EVER set things directly on the Vector3f constants. These are supposed to be read-only.



When you do something like: Vector3f.ZERO.setZ(5f) You have now changed the actual Vector3f.ZERO object and so anything else using it will now have a non-zero zero vector.



[java]

geom.setLocalTranslation(0,0,5);

…and

geom.setLocalTranslation( geom.localToWorld( new Vector3f(0,0,5) ), null ) );

[/java]



look like they would be equivalent to me given no other transformation. But I haven’t tested it.

1 Like
pspeed said:
Just a note: do not EVER and I mean NEVER EVER set things directly on the Vector3f constants. These are supposed to be read-only.


Got it!! Lol.

Thanks for your reply :)