2015-06-20 71 views
0

我建立一個自上而下的射手,像反恐精英2D那裏velecity的問題:Libgdx三角錯誤的角度

public class Player extends Entity { 

private float moveSpeed; 
private float turnSpeed; 
private float maxSpeed; 
private float moveFriction; 
private float turnFriction; 

private Texture tex; 
private Sprite sprite; 

private ArrayList<Bullet> bullets; 

public Player() { 
    moveSpeed = 15/Game.PPM; 
    turnSpeed = 400/Game.PPM; 
    maxSpeed = 300/Game.PPM; 
    moveFriction = 5/Game.PPM; 
    turnFriction = 10/Game.PPM; 

    tex = new Texture(Gdx.files.internal("ingame/TempPlayer.png")); 
    sprite = new Sprite(tex); 

    bodyDef = new BodyDef(); 
    bodyDef.position.set(Game.cam.position.x/Game.PPM, Game.cam.position.y/Game.PPM); 
    bodyDef.type = BodyType.DynamicBody; 
    body = Game.world.createBody(bodyDef); 

    shape = new PolygonShape(); 
    shape.setAsBox(sprite.getWidth()/2/Game.PPM, sprite.getHeight()/4/Game.PPM); 

    fixDef = new FixtureDef(); 
    fixDef.shape = shape; 
    body.createFixture(fixDef); 

    shape.setAsBox(sprite.getWidth()/10/Game.PPM, sprite.getHeight()/5/Game.PPM, new Vector2(sprite.getWidth()/2/Game.PPM, sprite.getHeight()/3/Game.PPM), 0/Game.PPM); 

    fixDef.shape = shape; 
    body.createFixture(fixDef); 

    bullets = new ArrayList<Bullet>(); 
} 

public void update(float dt) { 
    sprite.setCenter(body.getPosition().x * Game.PPM, body.getPosition().y * Game.PPM); 
    sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees); 

    // FRICTON 
    if (body.getLinearVelocity().x > 0) { 
     body.setLinearVelocity(body.getLinearVelocity().x - moveFriction, body.getLinearVelocity().y); 
    } else if (body.getLinearVelocity().x < 0) { 
     body.setLinearVelocity(body.getLinearVelocity().x + moveFriction, body.getLinearVelocity().y); 
    } 
    if (body.getLinearVelocity().y > 0) { 
     body.setLinearVelocity(body.getLinearVelocity().x, body.getLinearVelocity().y - moveFriction); 
    } else if (body.getLinearVelocity().y < 0) { 
     body.setLinearVelocity(body.getLinearVelocity().x, body.getLinearVelocity().y + moveFriction); 
    } 
    if (body.getAngularVelocity() > 0) { 
     body.setAngularVelocity(body.getAngularVelocity() - turnFriction); 
    } else if (body.getAngularVelocity() < 0) { 
     body.setAngularVelocity(body.getAngularVelocity() + turnFriction); 
    } 

    // MAX SPEED 
    if (body.getLinearVelocity().x > maxSpeed) { 
     body.setLinearVelocity(maxSpeed, body.getLinearVelocity().y); 
    } else if (body.getLinearVelocity().x < -maxSpeed) { 
     body.setLinearVelocity(-maxSpeed, body.getLinearVelocity().y); 
    } 
    if (body.getLinearVelocity().y > maxSpeed) { 
     body.setLinearVelocity(body.getLinearVelocity().x, maxSpeed); 
    } else if (body.getLinearVelocity().y < -maxSpeed) { 
     body.setLinearVelocity(body.getLinearVelocity().x, -maxSpeed); 
    } 

    // PLAYER MOVEMENT 
    if (body.getAngle() >= 360) { 
     body.setTransform(body.getPosition().x, body.getPosition().y, 0); 
    } 

    float xVel = -(MathUtils.sin(body.getAngle()) * moveSpeed); 
    float yVel = MathUtils.cos(body.getAngle()) * moveSpeed; 

    if (Gdx.input.isKeyPressed(Keys.LEFT)) { 
     body.setAngularVelocity(turnSpeed); 
    } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) { 
     body.setAngularVelocity(-turnSpeed); 
    } else { 
     body.setAngularVelocity(0); 
    } 
    if (Gdx.input.isKeyPressed(Keys.UP)) { 
     body.setLinearVelocity(body.getLinearVelocity().x + xVel, body.getLinearVelocity().y + yVel); 
    } else if (Gdx.input.isKeyPressed(Keys.DOWN)) { 
     body.setLinearVelocity(body.getLinearVelocity().x - xVel, body.getLinearVelocity().y - yVel); 
    } 
    if (Gdx.input.isKeyJustPressed(Keys.SPACE)) { 
     bullets.add(new Bullet(body.getPosition().x, body.getPosition().y, body.getAngle())); 
    } 

    // CAMERA MOVEMENT 
    Game.b2dCam.position.set(body.getPosition().x, body.getPosition().y, 0); 
    Game.cam.position.set(body.getPosition().x * Game.PPM, body.getPosition().y * Game.PPM, 0); 

    for (int i = 0; i < bullets.size(); i++) { 
     bullets.get(i).update(dt); 
    } 
} 

public void render(SpriteBatch sb) { 
    sb.setProjectionMatrix(Game.cam.combined); 
    sprite.draw(sb); 
    for (int i = 0; i < bullets.size(); i++) { 
     bullets.get(i).render(sb); 
    } 
} 

public void dispose() { 
    tex.dispose(); 
    shape.dispose(); 
} 

在這部分代碼我試圖去的地方,我在尋找,它應該像這

float xVel = MathUtils.cos(body.getAngle()) * moveSpeed; 
    float yVel = MathUtils.sin(body.getAngle()) * moveSpeed; 

    if (Gdx.input.isKeyPressed(Keys.LEFT)) { 
     body.setAngularVelocity(turnSpeed); 
    } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) { 
     body.setAngularVelocity(-turnSpeed); 
    } else { 
     body.setAngularVelocity(0); 
    } 
    if (Gdx.input.isKeyPressed(Keys.UP)) { 
     body.setLinearVelocity(body.getLinearVelocity().x + xVel, body.getLinearVelocity().y + yVel); 
    } else if (Gdx.input.isKeyPressed(Keys.DOWN)) { 
     body.setLinearVelocity(body.getLinearVelocity().x - xVel, body.getLinearVelocity().y - yVel); 
    } 

,但是當我這樣做,當我試着往前走

如果我改變COS和罪惡的地方,它工作正常一樣,性格一側移動,但它不應該

float xVel = -(MathUtils.sin(body.getAngle()) * moveSpeed); 
    float yVel = MathUtils.sin(body.getAngle()) * moveSpeed; 

回答

0

一個可能的原因很簡單,因爲你的Player紋理(png/jpg本身)可能沒有指向右邊,這被認爲是0度角。

如果是這種情況,最簡單的解決方案是編輯紋理,以便玩家指向右側。

所有的代碼看起來很好,除了這一點:

// PLAYER MOVEMENT 
if (body.getAngle() >= 360) { 
    body.setTransform(body.getPosition().x, body.getPosition().y, 0); 
} 

,你有度比較弧度。無論如何,if-clause不應該是必要的,Box2D將已經將角度值限制在0和2 * PI之間。

+0

感謝您的回覆。我發現我認爲北方的問題是box2D中的0度,但我意識到0度是東方。這就是爲什麼我的球員側身移動。 – Crystale