Libgdx三角法错误角度
我正在打造一款像Counter Strike 2D一样的自上而下的射手。velecity存在问题:Libgdx三角法错误角度,libgdx,trigonometry,Libgdx,Trigonometry,我正在打造一款像Counter Strike 2D一样的自上而下的射手。velecity存在问题: public class Player extends Entity { private float moveSpeed; private float turnSpeed; private float maxSpeed; private float moveFriction; private float turnFriction; private Texture tex; private Sp
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();
}
但是当我这样做的时候,当我试图前进的时候,角色会侧移
İ如果我改变了cos和sin的位置,它可以像那样工作,但不应该
float xVel = -(MathUtils.sin(body.getAngle()) * moveSpeed);
float yVel = MathUtils.sin(body.getAngle()) * moveSpeed;
一个可能的原因很简单,因为您的播放器纹理(png/jpg本身)可能没有指向右侧,这被认为是0度角 如果是这种情况,最简单的解决方案是编辑纹理,使玩家指向右侧 您的所有代码似乎都很好,但以下代码除外:
// PLAYER MOVEMENT
if (body.getAngle() >= 360) {
body.setTransform(body.getPosition().x, body.getPosition().y, 0);
}
比较弧度和度数的地方。无论如何,如果不需要if子句,Box2D将已经将角度值钳制在0和2*PI之间。感谢您的回复。我发现了一个问题,我以为box2D中的北方是0度,但我意识到0度是东方。这就是为什么我的球员横着移动。
// PLAYER MOVEMENT
if (body.getAngle() >= 360) {
body.setTransform(body.getPosition().x, body.getPosition().y, 0);
}