How to move geometry in the direction its facing

Hello everyone. I am using myGeometry.rotate(x,y,x); to rotate my ship.



Now I want to make it move forward based on the direction its facing. I found this on the wiki:


Q: How do I let my player move forward to the direction it's facing (i.e when you press the “forward” button , it moves to the direction it's facing)

A: Rotation.mult(VectorWithMovementData, VectorToStoreResult);


But I do not understand how to use that code. What is Rotation.mult and all that stuff? Thanks!

Rotation is the rotation Quaternion of your geometry, mult is a method the Quaternion has, VectorWithMovementData is the movement Vector (probably (0,0,10.0f * tpf) or something like that) and VectorToStoreResult is the Vector you can use to translate your geometry afterwards (Geometry.move(VectorToStoreResult)).

You have to replace the following with the jme classes.

multLocal is in jme object implemented, so its out = quat.multLocal(out) if I remember correctly



out contains after using the emthod the vector pointing in the direction.





Logic:

A quaterion can rotate a Vector.

The object has the rotation of x

we rotate the localforward vector 0,0,1 with the quaternion and have now a forward vector in worldspace.



[java] public static void getForward(Quat4d quat, Vector3d out) {

out.set(0, 0, 1);

multLocal(quat, out);

}



public static void getRight(Quat4d quat, Vector3d out) {

out.set(-1, 0, 0);

multLocal(quat, out);

}



public static void getUp(Quat4d quat, Vector3d out) {

out.set(0, 1, 0);

multLocal(quat, out);

}[/java]

Hi guys, I know this is an old post, but I have been struggling with the same problem for a while now. I tried to use trigonometry to solve it and other complicated methods which didn’t work :confused: The answer is actualy SOOOOO simple! To get the forward direction vector, i.e. the vector pointing in the direction the geometry is facing, you just use

Vector3f forward = spatial.getLocalRotation().getRotationColumn(0);

And there you have it. Now all you do is spatial.move(forward.mult(tpf).mult(speed)) and you have it!

@Salomon said: Hi guys, I know this is an old post, but I have been struggling with the same problem for a while now. I tried to use trigonometry to solve it and other complicated methods which didn't work :/ The answer is actualy SOOOOO simple! To get the forward direction vector, i.e. the vector pointing in the direction the geometry is facing, you just use

Vector3f forward = spatial.getLocalRotation().getRotationColumn(0);

And there you have it. Now all you do is spatial.move(forward.mult(tpf).mult(speed)) and you have it!

It’s clearer, better, and more logical to use:
Vector3f forward = spatial.getLocalRotation().mult(Vector3f.UNIT_Z);

…as it says exactly what it’s doing and doesn’t require the construction of a matrix.

Actually, reading yours again, it’s abnormal so maybe your model is oriented poorly and not pointing down Z. In which case, replace Vector3f.UNIT_Z with Vector3f.UNIT_X… but consider fixing your model because other things will be happier later. As it stands now, every advice you will ever see about yaw, pitch, and roll will be incorrect because for your model pitch is roll and roll is pitch.

Also, many of these topics are well covered in https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies
and: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies
…so if people still aren’t finding that then it’s worth including the links here.