我正在为练习创造一个打砖块游戏。我有一个抽象的父类游戏对象游戏精灵(蝙蝠,砖,球):
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;
}
}
我的蝙蝠
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);
}
}
我的球:
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);
}
}
蝙蝠工作正常,但Ball在创建一个身体后跌落了30个单位:
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)
我的世界有0引力。如果我将球从50提升到100单位,球同样会下降30个单位:
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);
您正在使用y原点位置而不是y位置。只需将getOriginY()更改为getY()即可。