Libgdx Trigonometry Wrong Angle -
i building top down shooter counter strike 2d there problem 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 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); (int = 0; < bullets.size(); i++) { bullets.get(i).update(dt); } } public void render(spritebatch sb) { sb.setprojectionmatrix(game.cam.combined); sprite.draw(sb); (int = 0; < bullets.size(); i++) { bullets.get(i).render(sb); } } public void dispose() { tex.dispose(); shape.dispose(); }
on part of code trying go looking , should work this
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); }
but when character moves sideways when try go forward
İf change cos , sin's places works fine shouldn't
float xvel = -(mathutils.sin(body.getangle()) * movespeed); float yvel = mathutils.sin(body.getangle()) * movespeed;
a possible cause because player texture (the png/jpg itself) may not pointing right, considered 0 degree angle.
if that's case easiest solution edit texture player points right.
all of code seems fine, except this:
// player movement if (body.getangle() >= 360) { body.settransform(body.getposition().x, body.getposition().y, 0); }
where compare radians degrees. anyway if-clause should not necessary, box2d clamp angle value between 0 , 2*pi.
Comments
Post a Comment