How to move an object (ship) in the same direction it's looking at?

Well the title pretty much says it.

I imported a ship model from blender and I made it so that if I press the up arrow key the ship moves forward and left and right arrows rotate her, using the analog listener. Now I want her to move the same direction she’s looking at.

My idea was to split the total speedvalue into the z and x axis. For example, if i rotate the ship 45° to the left, I would give the z translation 50% of the total speedvalue and 50% to the x translation, but I don’t even now how I could do this.

Any ideas?? Maybe this is way too complicated and there’s is probably a better way, i’m just a noob :stuck_out_tongue:

If someone has already done something like this it’d be good if i could see some code snippets or an example.

I’d appreciate any help.

The usual way to get the vector the “forward” vector of an object is getLocalRotation().getRotationColumn(2).

1 Like

Nice, that´s work, but I have another question.

How could I let the ship move faster, now?

Once you have the rotation/direction you want do go, you multiply that by some scalar to increase the speed. You may want to call normalize() on the rotational vector first so that it’s a unit vector (length equal to 1), and then multiply by the scalar (speed you want to go), for more precise calculations.