Sign in to follow this  
Martin Rohwedder

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

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 - https://www.youtube.com/watch?v=aml5FoyyKLo

 

Hopes anyone can help me :)

Share this post


Link to post
Share on other sites
TheChubu    9451

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 this post


Link to post
Share on other sites
krinosx    606

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 this post


Link to post
Share on other sites
Doublefris    316

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 this post


Link to post
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 this post


Link to post
Share on other sites
ferrous    6137

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 this post


Link to post
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 this post


Link to post
Share on other sites
ferrous    6137

 

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;
    this.rigidbody.AddTorque(FlipForce*Vector3.Cross(Vector3.up, dir));
}
			
		

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this