Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.applyLinearImpulse(Global.IMPULSE, 
                                    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 has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

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
1  
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.