Translating object in a certain direction

I have got a cube, which is attached to a physics node. I set its moving speed with

[java]cube_phy.setLinearVelocity( newVector( 0, 0, 20*tpf );[/java]

If I rotate the cube, I want it to move in the new direction, but how do I do this?

Yes, it is working! Thank you so much! :slight_smile:

sorry, it’s "new Vector3f( 0, 0, 20*tpf );

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

I am using this code now to rotate the cube and change the moving direction:

[java]rotation.fromAngleAxis( 0.005f , Vector3f.UNIT_Y );

cube_phy.setPhysicsRotation(rotation);

linearVelocity = rotation.mult( linearVelocity );[/java]

where rotation is a quaternion and linearVelocity the vector for the linear Velocity of the cube. The object is now moving in the right direction, but the cube itself doesn’t really rotate. It also doesn’t work if I use the function [java]cube.setLocalRotation( quaternion )[/java]

I have absolutely no idea why it doesnt rotate, but a rotation of 0.005 seems not noticable to me, try with FastMath.PI/16 and see if that changes something

Maybe its deactivated because it didn’t move for some time? call activate() on it when you set the rotation.

I tried to call activate(), but it is still not working. I choose a tiny value for the rotation because it is constantly rotated while I press the D button.

I tried to create a picture with paint to try to show how my cube is moving, I hope you can see what I mean :slight_smile:







It seems that just the object, or the physics is rotating, but not the shape itself. Could this be because the method setPhysicsRotation(rotation) does not apply to the shape?

Err, did you add the rigidBodyControl to the spatial?

Okay, I don’t know why I can’t upload the picture :(. However, I did add the rigidBodyControl to the spatial, and the object is also moving in a curve. The shape is just not rotating.

Post your whole code. You do something wrong.

okay, here’s my code:



my main class:

[java]

protected static Spatial plane = new Spatial();

protected static Geometry plane = new Geometry();



private BulletAppState bulletAppState = new BulletAppState();

protected static RigidBodyControl plane_phy = new RigidBodyControl();



@Override

public void simpleInitApp() {

bulletAppState = new BulletAppState();

this.stateManager.attach(bulletAppState);



plane = assetManager.loadModel( CManager.l.getActivePlane().getModelPath());

plane.scale(1.0f, 1.0f, 1.0f);

plane.setLocalTranslation( 0.0f, 0.0f, 4.0f );

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

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

plane.setMaterial( matPlane );

rootNode.attachChild( plane );



plane_phy = new RigidBodyControl(2.0f);

plane.addControl( plane_phy );

bulletAppState.getPhysicsSpace().add( plane_phy );

plane_phy.setLinearVelocity( new Vector3f() );

}



@Override

public void simpleUpdate(float tpf)

{

CManager.l.getActivePlane().fly( plane_phy, tpf );

}

[/java]



my class Plane:

[java]

public class Plane

{

Vector3f linearVelocity = new Vector3f();

Vector3f angularVelocity = new Vector3f();

Quaternion rotation = new Quaternion();



public void fly( RigidBodyControl plane_phy, float tpf ) {

plane_phy.setLinearVelocity( linearVelocity );

}



public void left( RigidBodyControl plane_phy, Spatial plane, float tpf ) {

rotation.fromAngleAxis( 0.005f , Vector3f.UNIT_Y );

plane_phy.setPhysicsRotation(rotation);

linearVelocity = rotation.mult( linearVelocity );

}

[/java]



every time I press the A button, the method right() is called and this is where the motion path should be canged (this is actually be done, but the object itself doesn’t rotate).

If you have any questions to methods or classes etc., just ask me. Thank you for helping, I really don’t know what’s wrong :?

Well, what I can see is that you should move and attach the spatial to the rootnode after adding the control, otherwise the local translation of the collision shape will be affected…

Then here in the left() method:

[java]rotation.fromAngleAxis( 0.005f , Vector3f.UNIT_Y );

plane_phy.setPhysicsRotation(rotation);[/java]

You always set the same rotation to the physics body, you don’t increment it before you set the physics rotation. First get the rotation, then do quaternion.multLocal() on it, then set it again.