I'm creating an Arkanoid game for practice. I have an abstract parent class GameObject for game sprites(Bat, Brick, Ball):
public abstract class GameObject extends Sprite {
protected World world;
protected Body body;
public GameObject(Texture texture, float width, float height, Vector2 centerPosition, World world) {
super(texture);
this.world = world;
setOrigin(width / 2, height / 2);
setSize(width, height);
setCenter(centerPosition.x, centerPosition.y);
defineBody();
}
public void render(float delta) {
setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2);
draw(batch);
}
protected abstract void defineBody();
public Body getBody() {
return body;
}
}
My Bat:
public class Bat extends GameObject {
public Bat(World world) {
super(batTexture, BAT_WIDTH, BAT_HEIGHT, new Vector2(WORLD_WIDTH / 2, 5 + BAT_HEIGHT / 2), world);
}
public void moveLeft(float delta) {
body.getPosition().set(-BAT_MOVEMENT_SPEED * delta, 0);
checkForBoundaries();
}
public void moveRight(float delta) {
translateX(BAT_MOVEMENT_SPEED * delta);
checkForBoundaries();
}
private void checkForBoundaries() {
if (body.getPosition().x - getWidth() / 2 < 0) setX(getWidth() / 2);
else if (body.getPosition().x + getWidth() / 2 > WORLD_WIDTH) setX(WORLD_WIDTH - getWidth() / 2);
}
@Override
protected void defineBody() {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(getX() + getWidth() / 2, getY() + getHeight() / 2);
body = world.createBody(bodyDef);
FixtureDef fixtureDef = new FixtureDef();
PolygonShape shape = new PolygonShape();
shape.setAsBox(getWidth() / 2, getHeight() / 2);
fixtureDef.shape = shape;
fixtureDef.friction = 0;
fixtureDef.density = 700;
fixtureDef.restitution = 1;
body.createFixture(fixtureDef);
}
}
My Ball:
public class Ball extends GameObject {
public Ball(World world) {
super(ballTexture, BALL_RADIUS * 2, BALL_RADIUS * 2, new Vector2(WORLD_WIDTH / 2, BALL_Y), world);
}
@Override
protected void defineBody() {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(getX() + getWidth() / 2, getOriginY() + getHeight() / 2);
System.out.println("X: " + (getX() + getWidth() / 2) + " Y: " + (getY() + getHeight() / 2));
body = world.createBody(bodyDef);
System.out.println(bodyDef.position);
System.out.println(body.getPosition());
FixtureDef fixtureDef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(BALL_RADIUS);
fixtureDef.shape = shape;
fixtureDef.friction = 0;
fixtureDef.density = 700;
fixtureDef.restitution = 1;
body.createFixture(fixtureDef);
}
}
Bat is working ok, but Ball is falling down for 30units after creating a body:
Before world.createBody() bodyDef.position: X: 200.0 Y: 50.0
After world.createBody() bodyDef.position: (200.0,20.0)
After world.createBody() body.getPosition(): (200.0,20.0)
My world has 0 gravitation. If I move up my ball from 50 to 100units, ball is similarly falling down for exactly 30 units:
Before world.createBody() bodyDef.position: X: 200.0 Y: 100.0
After world.createBody() bodyDef.position: (200.0,70.0)
After world.createBody() body.getPosition(): (200.0,70.0)
bodyDef.position.set(getX() + getWidth() / 2, getOriginY() + getHeight() / 2);
You are working with the y origin position and not with the y position. Simply change getOriginY() to getY().