Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I'm trying to apply force to a polygon physics object to move it. Whilst I can get it to move upwards easily enough, I'm trying to get it to move in the direction in which it is pointing.

Here's my current movement code -

private void controls(){
    if(Gdx.input.isKeyPressed(Keys.D)|| Gdx.input.isKeyPressed(Keys.RIGHT)){
        shipBody.applyAngularImpulse(-Global.ROTATIONSPEED, true);

    if(Gdx.input.isKeyPressed(Keys.A)|| Gdx.input.isKeyPressed(Keys.LEFT)){
        shipBody.applyAngularImpulse(Global.ROTATIONSPEED, true);

    if(Gdx.input.isKeyPressed(Keys.W)|| Gdx.input.isKeyPressed(Keys.UP)){
                                    shipBody.getWorldCenter(), true);

    System.out.println("Ship angle: " + shipBody.getAngle() * (float)(180 / Math.PI));

Global.Impulse is:

public static final Vector2 IMPULSE = new Vector2(0, 25f * MetersToPixels);

Whilst this works fine for rotating the ship, when i apply thrust, it just goes vertically up (I understand why this happens, I'm just not entirely sure how to make it move in the direction it's pointing).

share|improve this question

marked as duplicate by Byte56, Sean Middleditch, Nicol Bolas, Josh Petrie, msell Jun 6 '13 at 5:33

This question was marked as an exact duplicate of an existing question.

force.X = cos(shipBody.getAngle()));
force.Y = sin(shipBody.getAngle()));

Then you'll probably want to normalise that force and then multiply it by a magnitude to keep speed uniform

share|improve this answer
Sine and cosine would already yield a normalized force. six(x)^2 + cos(x)^2 = 1. They only need the magnitude. – Seth Battin Jun 6 '13 at 3:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.