# JME3 - How to roll-move a cube along the game world

This topic is 2041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all

I am trying jMonkeyEngine out and is trying to learn how to make some small games with it. I am having a problem. I have a simple rectangular cube which is my intention that the player should be able to roll around the gameworld.

When he presses the arrow keys the cube should make a roll towards the way the user pressed the array key, so if he presses the right arrow key, the cube rolls 1 WU to the right.

The problem is not to move the cube, which I can du with the move() method, but my problem is how I 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 this game -

Hopes anyone can help me :)

##### Share on other sites

Afaik jMonkeyEngine has a physics system based on jBullte, so I'm guessing you'd need to set up the cube so the key presses apply an impulse on it to flip it around.

I haven't seen anyone here using JME, you might want to check their own forums.

##### Share on other sites

Check this tutorial in JME3 forum:

http://hub.jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_input_system

It show you how to use input system to move your player around:

        if (name.equals("Rotate")) {
player.rotate(0, value*speed, 0);
}
if (name.equals("Right")) {
Vector3f v = player.getLocalTranslation();
player.setLocalTranslation(v.x + value*speed, v.y, v.z);
}
if (name.equals("Left")) {
Vector3f v = player.getLocalTranslation();
player.setLocalTranslation(v.x - value*speed, v.y, v.z);
}


If you see the full source, you will realize that 'player' is a instance of Geometry class... you can use methods from Geometry class to deal with positioning, rotations, etc...

So you may use your 'cube' as a instance of geometry or some derived class and use the same approach if you don't intend to use physics engine.

Hope it helps... btw... JME3 has a lot of tutorials here: http://hub.jmonkeyengine.org/wiki/doku.php/jme3

Try to play with it and see what you can get....

[]´s

##### Share on other sites

You could do something like, each frame find the closest edge of the cube to the ground along the velocity vector. then rotate the cube along this line.

##### Share on other sites

Hi all

I have solved the problem by attaching a pivot node to the cube, and then translate the cube according to the pivot point.

Normally the rotation is done in the center of the object, which is also the case witht the pivot point, but by translating the object relatively to the pivot point, then I was able to move the center of this rotation to the edge of the cube.

I got some good help from this post - http://gamedev.stackexchange.com/questions/71445/how-do-i-make-a-cube-move-by-rolling

What I did was the following.

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);


Then I could rotate it along to z-axis in the simpleUpdate method.

@Override
public void simpleUpdate(float tpf) {
//rotate along z-axis
pivot.rotate(0.0f, 0.0f, -0.001f);
}


##### Share on other sites

Nice work.  I did something similar with a coin-like object (nearly flat cylinder) in Unity.  It's a bit simpler since it's round, so it's just a matter of making it rotate towards the direction it wants to go.  Though in my case, I wanted to have it interact with the physics, so I actually gave it an impulse on the opposite edge of the direction it wanted to go.

You can see my implementation in action here.

##### Share on other sites

Nice work.  I did something similar with a coin-like object (nearly flat cylinder) in Unity.  It's a bit simpler since it's round, so it's just a matter of making it rotate towards the direction it wants to go.  Though in my case, I wanted to have it interact with the physics, so I actually gave it an impulse on the opposite edge of the direction it wanted to go.

You can see my implementation in action here.

Is it possible to see the source code (I also try learn unity), so I can learn some of the basics of your implementation??

##### Share on other sites

Nice work.  I did something similar with a coin-like object (nearly flat cylinder) in Unity.  It's a bit simpler since it's round, so it's just a matter of making it rotate towards the direction it wants to go.  Though in my case, I wanted to have it interact with the physics, so I actually gave it an impulse on the opposite edge of the direction it wanted to go.

You can see my implementation in action here.

Is it possible to see the source code (I also try learn unity), so I can learn some of the basics of your implementation??

Sure, for the coins, I forgot that was actually my first implementation, I found it to be more reliable to instead just add torque.

if( (Time.fixedTime - prevFlipTime) > TimeBetweenFlips && isTouchingGround)
{
Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
}



• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 14
• 30
• 9
• 16
• 22