4
\$\begingroup\$

I am learning jMonkeyEngine by making some small games with it. I have a simple rectangular cube that I want the player to be able to roll around the game world.

When he presses the arrow keys, the cube should make a roll in the direction of the key press. (So, for instance, pressing the right arrow key makes the cube roll 1 WU to the right.)

The problem isn't how to move the cube (I can do that with the move() method), but with how to make this rotation effect so it doesn’t look like the cube is moving to the destination, but is actually rolling.

The rolling effect should be like the cube in Bloxorz. (Here's a gameplay video.)

UPDATE

After I got some good help below, I have come up with a test solution, which I want to share with you.

Box box1 = new Box(1,1,1);
Geometry boxGeom = new Geometry("Box", box1);
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", ColorRGBA.Blue);
boxGeom.setMaterial(mat1);

//attach the cube to the pivot node
pivot = new Node("pivot");
pivot.setLocalTranslation(0.0f, 0.0f, 0.0f);
rootNode.attachChild(pivot); // put this node in the scene

pivot.attachChild(boxGeom);

//Translate the box so the pivot node is at the bottom right edge of the box
boxGeom.move(-1.0f, 1.0f, 0.0f);

And then rotate it like this.

@Override
public void simpleUpdate(float tpf) {
    //rotate along z-axis
    pivot.rotate(0.0f, 0.0f, -0.001f);
}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ It's totally fine to post your own solution as an answer by the way. Keeps things clear for future readers! \$\endgroup\$
    – Anko
    Commented Mar 8, 2014 at 22:53

1 Answer 1

8
\$\begingroup\$

Conceptually, you can do that by animating the rotation of the cube (or as in Bloxorz, a cuboid) 90 degrees around one of its edges. You don't need move() at all!

Side-on view of one rotation:

side-on perspective of rotation of a cuboid around one of its edges

Here's a seriously good JMonkeyEngine tutorial showing you how to rotate Boxes around pivot Nodes. It explains everything step-by-step. These are the important lines:

/** Create a pivot node at (0,0,0) and attach it to the root node */
Node pivot = new Node("pivot");
rootNode.attachChild(pivot);

/** Attach your cuboid to the *pivot* node. (And transitively to the root node.) */
pivot.attachChild(yourCuboid);
/** Rotate the pivot node: This rotates everything attached to it! */
pivot.rotate(.4f, .4f, 0f);

Once you've got that, rolling around becomes successive rotations around different pivots:

side-on perspective of two successive rotations

\$\endgroup\$
1
  • \$\begingroup\$ Thank you, it seems that using a pivot is a good solution, I din't think of that... Thank you! \$\endgroup\$ Commented Mar 7, 2014 at 15:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .