Beginner Question About Rotating About a Pivot Point

Hello,

I was reading the Hello World tutorials and I am trying to become familiar with jme.

I have the code below that displays 2 boxes, and i want to make the red one rotate around his center point (spin)

i read https://wiki.jmonkeyengine.org/legacy/doku.php/jme2:rotate_about_a_point, however this was for jme2 and i can’t make it work.

Can someone help me and tell me how to move the red square to a 2nd pivot move and make it spin?

[java]

package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import com.jme3.scene.Node;



public class Main extends SimpleApplication {



public static void main(String[] args) {

Main app = new Main();

app.start();

}



protected Geometry player;

protected Geometry player2;



@Override

public void simpleInitApp() {



Box a = new Box(new Vector3f(1,2,0), 1, 1, 1);

player = new Geometry(“blue cube”, a);

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

matb.setColor(“Color”, ColorRGBA.Blue);

player.setMaterial(matb);



Box b = new Box(new Vector3f(5,-1,0),1,1,1);

player2 = new Geometry(“red cude”,b);

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

mata.setColor(“Color”,ColorRGBA.Red);

player2.setMaterial(mata);



Node pivot = new Node(“pivot”);

rootNode.attachChild(pivot);// put this node in the scene



/** Attach the two boxes to the pivot node. /

pivot.attachChild(player);

pivot.attachChild(player2);

}



/
This is the update loop /

@Override

public void simpleUpdate(float tpf) {

player.rotate(0,2
tpf,0);



}

}[/java]

99.999% of the time when new users create a box not at 0,0,0 they are making their lives harder.



Create the box at 0,0,0 and then move the geometry with setLocalTraslation(). Then it will rotate about the box’s center.

ye i know that if i create it at 0.0.0 it rotates around itself.

But i wanted to find out how i can create a box at a random location without moving it and make it spin there.

With the code below the red box spins on it’s new location, but what how can i do that if i have created it from start in that location?

Please tell me how it’s done!



[java]

protected Geometry player;

protected Geometry player2;



@Override

public void simpleInitApp() {



Box a = new Box(new Vector3f(1,2,0), 1, 1, 1);

player = new Geometry(“blue cube”, a);

Material matb = new Material(assetManager,

“Common/MatDefs/Misc/Unshaded.j3md”);

matb.setColor(“Color”, ColorRGBA.Blue);

player.setMaterial(matb);





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

player2 = new Geometry(“red cude”,b);

Material mata = new Material(assetManager,

“Common/MatDefs/Misc/Unshaded.j3md”);

mata.setColor(“Color”,ColorRGBA.Red);

player2.setMaterial(mata);

player2.setLocalTranslation( new Vector3f( 5f, -1f, 0f ) );



Node pivot = new Node(“pivot”);

rootNode.attachChild(pivot);// put this node in the scene



/** Attach the two boxes to the pivot node. /

pivot.attachChild(player);

pivot.attachChild(player2);

}





/
This is the update loop /

@Override

public void simpleUpdate(float tpf) {

player.rotate(0,2
tpf,0);

player2.rotate(2*tpf,0,0);

}

}

[/java]

Hi @odygrd



Have you had a chance to go over the jMonkeyEngine3 Scene Graph for Dummies documentation yet ? if not I would highly recommend you go through this, paying particular attention around slide 20, which covers off spatial rotation.

Ye i have read them and i understand how the scene graph works.

If i create an item with zero vector and then move the item or the whole Node with setLocalTraslation() or move() to a new location and then try a rotation loop the item will rotate around itself on that location.

However if i create the item from start at that location with Box a = new Box(new Vector3f(1,2,0), 1, 1, 1); for example and then try a rotation loop the item will be rotating-moving around.

Would appreciate it if someone could explain me how this is working

Don’t set location when creating the box. Just don’t. You are setting the raw location of triangles relative to the geometry’s origin when you do that and it’s_not_ what you want. Create all triangles relative to the Geometry’s origin and then move the Geometry to where you want the object.



Edit: I only just read the second code… and yeah, like the follow-up poster says, rotate the pivot. I still have trouble understanding why you go through so much trouble to create triangles off in space and then pretend they have a different origin, though. of course, you have to have moved the pivot to counteract the triangles being in an odd place and then move the Geometry negative that amount.

rotate the node instead :



[java]pivot.rotate(0,2*tpf,0);[/java]

I guess its easy to get confused and think the Box will be positioned in world space on that coordinate and not as a geometry offset when its created. While we are at this, Quad needs a similar function. For example billboard quads that should be “grounded” and always rotate around the y axis is nice to get centered in the x axis and be created from y=0 and up. Same with e.g. quads used for tree branch/leaf billboards is nice to have centered in the y direction to have the pivot point around the branch.



The way quads are defaultly created now its centered on its bottom left vertice which is not very useful, although its very easy to create your own version (which is what I did).

And it’s one line to reset the coordinates of the quad mesh by directly modifying the mesh. JME shapes are designed to be really basic, I guess. Beyond that it’s good to create your own.

thanks for all answers