简单的碰撞检测 - Android

发布于 2024-12-07 09:54:09 字数 736 浏览 1 评论 0原文

我想在类似乒乓球的游戏中进行非常简单的碰撞检测。 球是正方形,球拍(球棒)是长方形。

我有两个实体可以获取当前的 X 和 Y 位置以及位图高度和宽度。哪种方法最简单?

我有这个代码:

public void getCollision(Entity enitityOne, Entity enitityTwo){

    double eventCoordX = (enitityOne.getCenterX() - (enitityTwo.getBitmapWidth() / 2));
    double eventCoordY = (enitityOne.getCenterY() - (enitityTwo.getBitmapHeight() / 2));

    double X = Math.abs(enitityTwo.getxPos() - eventCoordX);
    double Y = Math.abs(enitityTwo.getyPos() - eventCoordY);

    if(X <= (enitityTwo.getBitmapWidth()) && Y <= (enitityTwo.getBitmapHeight())){
        enitityOne.collision();
        enitityTwo.collision();
    }
}

但我很盲目,这只适用于桨的中间而不是侧面。 问题是我看不出代码哪里错了。 有人吗? 有人有更好的主意吗?

I want to do really simple collision detection in a pong like game.
The ball is a square and the paddle (bats) is rectangles.

I have two entities coming in where I can get the current X and Y position, and the bitmap height and width. Which is the simplest way to do this?

I have this code:

public void getCollision(Entity enitityOne, Entity enitityTwo){

    double eventCoordX = (enitityOne.getCenterX() - (enitityTwo.getBitmapWidth() / 2));
    double eventCoordY = (enitityOne.getCenterY() - (enitityTwo.getBitmapHeight() / 2));

    double X = Math.abs(enitityTwo.getxPos() - eventCoordX);
    double Y = Math.abs(enitityTwo.getyPos() - eventCoordY);

    if(X <= (enitityTwo.getBitmapWidth()) && Y <= (enitityTwo.getBitmapHeight())){
        enitityOne.collision();
        enitityTwo.collision();
    }
}

But I'm pretty blind, this only works in the middle of the paddle not on the sides.
The problem is I can't see where the code is wrong.
Anybody?
Anybody have a better idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2024-12-14 09:54:09

如果您想要的只是查找 2 个给定矩形是否以某种方式相交(因此发生碰撞),这是最简单的检查(C 代码;随意使用浮点值):

int RectsIntersect(int AMinX, int AMinY, int AMaxX, int AMaxY,
                   int BMinX, int BMinY, int BMaxX, int BMaxY)
{
    assert(AMinX < AMaxX);
    assert(AMinY < AMaxY);
    assert(BMinX < BMaxX);
    assert(BMinY < BMaxY);

    if ((AMaxX < BMinX) || // A is to the left of B
        (BMaxX < AMinX) || // B is to the left of A
        (AMaxY < BMinY) || // A is above B
        (BMaxY < AMinY))   // B is above A
    {
        return 0; // A and B don't intersect
    }

    return 1; // A and B intersect
}

矩形 A 和 B 由最小值和其角点的最大 X 和 Y 坐标。

嗯...之前已经问过这个问题

If all you want is to find whether or not 2 given rectangles somehow intersect (and therefore collide), here's the simplest check (C code; feel free to use floating-point values):

int RectsIntersect(int AMinX, int AMinY, int AMaxX, int AMaxY,
                   int BMinX, int BMinY, int BMaxX, int BMaxY)
{
    assert(AMinX < AMaxX);
    assert(AMinY < AMaxY);
    assert(BMinX < BMaxX);
    assert(BMinY < BMaxY);

    if ((AMaxX < BMinX) || // A is to the left of B
        (BMaxX < AMinX) || // B is to the left of A
        (AMaxY < BMinY) || // A is above B
        (BMaxY < AMinY))   // B is above A
    {
        return 0; // A and B don't intersect
    }

    return 1; // A and B intersect
}

The rectangles A and B are defined by the minimum and maximum X and Y coordinates of their corners.

Um... This has been asked before.

挽心 2024-12-14 09:54:09

如果您正在处理矩形,那么

    /**
 * Check if two rectangles collide
 * x_1, y_1, width_1, and height_1 define the boundaries of the first rectangle
 * x_2, y_2, width_2, and height_2 define the boundaries of the second rectangle
 */
boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2)
{
  return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2);
}

这是一个很好的例子......

if you are dealing with rectangles then

    /**
 * Check if two rectangles collide
 * x_1, y_1, width_1, and height_1 define the boundaries of the first rectangle
 * x_2, y_2, width_2, and height_2 define the boundaries of the second rectangle
 */
boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2)
{
  return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2);
}

this is a good example...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文