我绘制了一个矩形,在所有边上都有4个较小的矩形,用于碰撞检测。 但是当我与我发生碰撞时,我会得到一些奇怪的结果,比如玩家正在经历它们。 我的Block Class
public Block(float x, float y, float width, float height) {
super(x, y, width, height);
top = new Rectangle(this.x+2, this.y, this.width-4, 4);
bottom = new Rectangle(this.x+2, this.y+this.height-4, this.width-4, 4);
left = new Rectangle(this.x, this.y, 4, this.height);
right = new Rectangle(this.x+this.width-4, this.y, 4, this.height);
}
这是我的碰撞检测
for (Block b : blocks) {
if (player.intersects(b.top)) {
player.setY(player.getY()-1);
velY = 0;
} else if (player.intersects(b.left)) {
player.setX(player.getX()-1);
velX = 0;
} else if (player.intersects(b.bottom)) {
player.setY(player.getY()+1);
velY = 0;
} else if (player.intersects(b.right)) {
player.setX(player.getX()+1);
velX = 0;
}
}
我真的需要这个工作:/我可以得到任何帮助吗?