Rotation around the spatial’s center (SOLVED)

Hello there!



As a newbie I am working my way through the tutorials (jMonkeyEngine 3), but got stuck a bit. The “Hello Loop” tutorial tells me to rotate two cubes, so I thought about placing them side by side and rotating them in opposite directions. I placed them and used the method cube.rotate(…) to rotate them. I noticed that they rotate around the origin, but I want them to rotate around their own center. I searched and tested for a few hours now (including use of Quaternion multiplication and pivot nodes), but everything I tried resulted in either rotating around the origin or not rotating at all. I can’t believe that this issue has no easy realization - imho rotating around the own center is needed far more often than rotating around the global origin. That’s why I assume that I have some problem in core understanding how the differend coordinate systems are related/used.



Below is the “closest” result I got. I couldn’t find an easy way to set a rotation point, so I defined a Quaternium for general rotation around the y-axis and tried to use it locally on the cube.

[java]import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.FastMath;

import com.jme3.math.Quaternion;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;



public class TestTemplate2 extends SimpleApplication {



protected Geometry cube1;

protected Geometry cube2;



@Override

public void simpleInitApp() {



Box b = new Box(new Vector3f(1.5f, 1, 1), 1, 1, 1);

cube1 = new Geometry(“blue cube”, b);

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

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

cube1.setMaterial(mat);

rootNode.attachChild(cube1);



Box b2 = new Box(new Vector3f(-1.5f, 1, 1), 1, 1, 1);

cube2 = new Geometry(“red cube”, b2);

Material mat2 = new Material(assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);

mat2.setColor(“m_Color”, ColorRGBA.Red);

cube2.setMaterial(mat2);

rootNode.attachChild(cube2);



}



private static Quaternion DEFAULT_ROTATION_Y = new Quaternion().fromAngleAxis(0.001f, Vector3f.UNIT_Y);



@Override

public void simpleUpdate(float tpf) {

// make the cube rotate

cube1.getLocalRotation().multLocal(DEFAULT_ROTATION_Y);

//cube1.rotate(DEFAULT_ROTATION_Y);



}

}[/java]

Make cube mesh with center in origin and then move it and rotate it via setLocalTranslation

Box b = new Box(new Vector3f(1.5f, 1, 1), 1, 1, 1);



Actuall you are rotaing it around the center, but the box is not in the center, as you told it with that Vector.

Your answers were confusing at first, but now that I thought about it, everything makes sense. Just for the case anyone else has this problem, I’ll post these thoughts:


  1. There are two coordinate systems: a global one where everything is set, and a local one for each spatial whose origin is the center of the spatial.
  2. Usually spatials are created with parameters of the local system (here was my problem - I thought the coordinates in the constructor are global ones).
  3. To place the spatial in the global system, we can use the method spatial.setLocalTranslation(…).



    The result is the following code:

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

    cube1 = new Geometry(“blue cube”, b);

    cube1.setLocalTranslation(new Vector3f(1.5f, 1, 1));

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

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

    cube1.setMaterial(mat);

    rootNode.attachChild(cube1);[/java]



    Thanks for the fast help. Maybe these things are explained later in one of the other tutorials and I just didn’t stumble over them yet. However, if there is no paragraph that explains these details, I suggest that it should be added.

to 3.



Actuall y it’s not the global one, every setlocalrotation/Translation is in relation to the parents coordinate system.(Wich in your case is the global, since the rootnode defines the global systems itself.) You can easly test this behaviour, just attach the second box to the first.

1 Like

Ah, another quite important detail that isn’t mentioned in the javadoc (yet). Thank you for saving me some future trouble :slight_smile:

… its the first thing mentioned in the tutorials …

yajirobee said:
Ah, another quite important detail that isn't mentioned in the javadoc (yet). (...)

Though it's in the tutorials already, if you think it's worth reiterating in the JavaDoc feel free to submit a simple patch.

I checked the tutorials again and found following lines in the second one (Hello Node):


// create a blue box at coordinates (1,-1,1)
Box box1 = new Box( new Vector3f(1,-1,1), 1,1,1);


We create a box Geometry.
* The box Geometry's extends are (1,1,1), that makes it 2x2x2 world units big.
* We place the box at (1,-1,1)


I found no clue which coordinate systems are used or that different ones exist (at least I didn't recognize any), so I naturally assumed the positioning in the global system. Could you please give me a little hint, where exactly "it's the first thing mentioned in the tutorials"? Either it's not in the tutorials, or it's too hidden for newbies like me. Either way, that point is not clear enough imho.

Thanks for the little "how to" in adding code parts. I'll think about it when I'm a bit more confident with JME.