It's my first time posting here!
I'm working on a top-down shooter game in LibGDX and Box2D (so gravity is set to (0, 0)
). At the moment, to move my character, I'm using the setTransform() method. I understand it can be a bit buggy, but in past experiences the method worked fine and collisions were detected and handled.
Here's an image of my character (DynamicBody) colliding with a static obstacle. Even using setTransform()
, it should try to push the character outside of the box's bounds? Bodies do not seem to be colliding.
Here's my Player
code. The Vector2 set as linearVelocity
is not being used.
public class Player extends BodyHolder implements Disposable {
private float speed = 2.3f;
private Sprite sprite;
private Vector3 position;
private Vector2 linearVelocity;
private Vector3 mousePos;
private static final Vector2 SIZE = new Vector2(93, 84);
private int playerPhase = 0, prevPlayerPhase = 0;
private int ID;
private boolean isMain = false;
public Player(final Vector2 position, final WorldManager world) {
super(world, position, new Vector2(SIZE.x / 2, SIZE.y / 2), BodyDef.BodyType.DynamicBody, 0.8f, false, -1);
}
public Player(int id, Vector2 pos, WorldManager world, boolean main) {
super(world, pos, new Vector2(SIZE.x / 2, SIZE.y / 2), BodyDef.BodyType.DynamicBody, 0.8f, false, id);
this.ID = id;
this.position = new Vector3(pos.x, pos.y, 0);
this.mousePos = new Vector3();
this.isMain = main;
linearVelocity = new Vector2();
getBody().setFixedRotation(true);
sprite = new Sprite(new Texture(Gdx.files.internal("player/player_nogun.png")));
playerPhase = 1;
}
public void update(SpriteBatch batch, ModifiedShapeRenderer renderer, CameraController cam) {
sprite.setCenter(position.x, position.y);
getBody().setTransform(position.x, position.y, 0);
// getBody().setLinearVelocity(linearVelocity);
if(isMain()) this.handleInput(cam);
if(prevPlayerPhase != playerPhase) {
prevPlayerPhase = playerPhase;
if(playerPhase == 1) {
sprite = new Sprite(new Texture(Gdx.files.internal("player/player_nogun.png")));
sprite.setOrigin(sprite.getOriginX() + 5, sprite.getOriginY());
} else if(playerPhase == 2) {
sprite = new Sprite(new Texture(Gdx.files.internal("player/player.png")));
}
}
for(Gun gun : GunSpawner.getGuns()) {
if(gun.obtainObject().getBoundingRectangle().overlaps(this.sprite.getBoundingRectangle())) {
gun.hoverOver(this);
}
}
batch.setProjectionMatrix(cam.getCamera().combined);
batch.begin();
sprite.draw(batch);
batch.end();
}
public void handleInput(CameraController cam) {
mousePos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
cam.getCamera().unproject(mousePos);
sprite.setRotation((float) (MathUtils.radiansToDegrees * Math.atan2((position.y - mousePos.y) , (position.x - mousePos.x))));
boolean keyPressed = false;
if(Gdx.input.isKeyPressed(Input.Keys.W)) {
position.y += speed;
linearVelocity.y = 100;
keyPressed = true;
}
if(Gdx.input.isKeyPressed(Input.Keys.S)) {
position.y -= speed;
linearVelocity.y = -100;
keyPressed = true;
}
if(Gdx.input.isKeyPressed(Input.Keys.A)) {
position.x -= speed;
linearVelocity.x = -100;
keyPressed = true;
}
if(Gdx.input.isKeyPressed(Input.Keys.D)) {
position.x += speed;
linearVelocity.x = 100;
keyPressed = true;
}
if(keyPressed) {
keyPressed = false;
} else {
linearVelocity.set(0, 0);
}
if (Gdx.input.isKeyJustPressed(Input.Keys.K)) {
Gdx.app.log(position.x + "", position.y + "");
}
}
public Vector2 get2DPosition() {
return new Vector2(position.x, position.y);
}
public int getID() {
return ID;
}
public boolean isMain() {
return this.isMain;
}
public Sprite obtainObject() {
return sprite;
}
@Override
public void dispose() {
}
}
If it's any help, I'm stepping the world at 6 velocity iterations and 2 position iterations.
position
variable every frame (so if the physics engine tried to move the bodies apart, you undo that and force them back together), and you never read a new position out of the physics body to update thisposition
variable. So the communication is one-way. No matter what happens in the physics simulation, your code doesn't care - it never asks the physics engine where it should move to. Am I misunderstanding your code? \$\endgroup\$