【发布时间】:2014-02-08 22:11:25
【问题描述】:
我正在做一个平铺游戏。我想检查瓷砖从玩家那里的方向,什么时候发生碰撞。
static public boolean intersectRectangles(Rectangle rectangle1, Rectangle rectangle2, Rectangle intersection) {
if (rectangle1.overlaps(rectangle2)) {
intersection.x = Math.max(rectangle1.x, rectangle2.x);
intersection.width = Math.min(rectangle1.x + rectangle1.width, rectangle2.x + rectangle2.width) - intersection.x;
intersection.y = Math.max(rectangle1.y, rectangle2.y);
intersection.height = Math.min(rectangle1.y + rectangle1.height, rectangle2.y + rectangle2.height) - intersection.y;
return true;
}
return false;
}
如果没有碰撞,我希望这个函数返回 0
1 = rect1 在左边
2 = rect1 在右边
3 = rect1 在底部
4 = rect1 在顶部
喜欢这张照片:
我试过这样做
// if there is a collision between player and tile
float xdif = player.x + 40 - j * 40;
float ydif = player.y + 40 - i * 40;
Direction direction = Direction.RIGHT;
if (Math.abs(xdif) > Math.abs(ydif)) {
if (player.y > i * 40)
direction = Direction.DOWN;
else
direction = direction.UP;
} else {
if (player.x > j * 40)
direction = Direction.LEFT;
else
direction = direction.RIGHT;
}
但不工作..
【问题讨论】:
-
This example 可能有帮助