检测碰撞方向

发布于 2024-10-18 03:16:37 字数 205 浏览 0 评论 0原文

一块方形瓷砖与另一块方形瓷砖碰撞。调酒师说...

我有:

  • 两个图块的高度、宽度、x 和 y。
  • 引起碰撞的运动的 2D 矢量。

我需要知道碰撞发生在哪一侧(例如顶部、底部、左侧、右侧),以便适当地重置位置。

我会给任何能回答这个问题的人一个心理饼干,因为我已经尝试了太多时间,这似乎很重要。

A square tile collides with another square tile. The bartender says...

I have:

  • The height, width, x, and y of both tiles.
  • The 2D vector of the movement which caused the collision.

I need to know from what SIDE the collision occurred (e.g. top, bottom, left, right) in order to reset the location appropriately.

I will give a mental cookie to whoever can answer this question, because I've been trying for too many hours and this seems fundamental.

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

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

发布评论

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

评论(5

梦明 2024-10-25 03:16:37
float player_bottom = player.get_y() + player.get_height();
float tiles_bottom = tiles.get_y() + tiles.get_height();
float player_right = player.get_x() + player.get_width();
float tiles_right = tiles.get_x() + tiles.get_width();

float b_collision = tiles_bottom - player.get_y();
float t_collision = player_bottom - tiles.get_y();
float l_collision = player_right - tiles.get_x();
float r_collision = tiles_right - player.get_x();

if (t_collision < b_collision && t_collision < l_collision && t_collision < r_collision )
{                           
//Top collision
}
if (b_collision < t_collision && b_collision < l_collision && b_collision < r_collision)                        
{
//bottom collision
}
if (l_collision < r_collision && l_collision < t_collision && l_collision < b_collision)
{
//Left collision
}
if (r_collision < l_collision && r_collision < t_collision && r_collision < b_collision )
{
//Right collision
}

当对象位于另一个对象内部时,这无法解决。但它确实适用于重叠

float player_bottom = player.get_y() + player.get_height();
float tiles_bottom = tiles.get_y() + tiles.get_height();
float player_right = player.get_x() + player.get_width();
float tiles_right = tiles.get_x() + tiles.get_width();

float b_collision = tiles_bottom - player.get_y();
float t_collision = player_bottom - tiles.get_y();
float l_collision = player_right - tiles.get_x();
float r_collision = tiles_right - player.get_x();

if (t_collision < b_collision && t_collision < l_collision && t_collision < r_collision )
{                           
//Top collision
}
if (b_collision < t_collision && b_collision < l_collision && b_collision < r_collision)                        
{
//bottom collision
}
if (l_collision < r_collision && l_collision < t_collision && l_collision < b_collision)
{
//Left collision
}
if (r_collision < l_collision && r_collision < t_collision && r_collision < b_collision )
{
//Right collision
}

This doesn't solve when object is inside one of the other. But it does work with overlapping

思念满溢 2024-10-25 03:16:37

给定 r1 ​​和 r2(r2 静止),首先找到 r2 与 r1 最近的角。
该点是 (c1.x,c1.y),想象一下现在您将其扩展到两个平面,一个平行于 x 轴,一个平行于 y 轴。

现在获取 r1 到 r2 最近的角(称为 c2)并将其用于以下公式中
y = mx + b
其中 b 是 c2.x
m 是你的向量。
x 是 c1.x

因此,如果 y 大于 c1.y 那么这意味着在 x 接触点(宽度)处您已经到达顶部。如果更少,那么你还没有达到目标。
反转底部/顶部。

Given r1 and r2 (r2 being stationary), first find the closest corner of r2 to r1.
This point is (c1.x,c1.y) and imagine now you extend this out into two planes, one parallel to the x axis and one to the y.

Now get the closest corner of r1 to r2 (call it c2) and use it in the following formula
y = mx + b
where b is c2.x
and m is your vector.
and x is c1.x

So if y is greater than c1.y then it means at the point of x contact (width) you've already hit the top. If it's less, then you haven't hit it yet.
Invert for bottom/top.

半枫 2024-10-25 03:16:37

假设您有一种方法来检测碰撞,那么了解哪一边发生碰撞就很简单了。您只需检查每个方块的 xy 位置即可。

Square1:(x1, y1)
Square2 : (x2, y2)

我将假设工作区域的左上角为 (0,0) 并且 x< /code> 值随着您向右移动而增加,y 值随着您向下移动而增加。

考虑到这一点:

如果 (x1 < x2),则方格 1 的右侧与方格 2 的左侧发生碰撞
如果 (x1 > x2),则方格 1 的左侧与方格 2 的右侧碰撞
如果 (y1 < y2),方块 1 的底边与方块 2 的顶边碰撞
如果 (y1 > y2),则方块 1 的顶边与方块 2 的底边碰撞

我建议你自己画几张图,你应该就清楚了。

Assuming you have a way to detect collisions, understanding which sides collided is straight forward. You simply need to examine the x and y positions of each square.

Square1 : (x1, y1)
Square2 : (x2, y2)

I'll work from the assumption that the top left corner of your work area is (0,0) and that x values increase as you move right, and y values increase as you move down.

With this in mind:

If (x1 < x2), the right side of square 1 collided with the left side of square 2
If (x1 > x2), the left side of square 1 collided with the right side of square 2
if (y1 < y2), the bottom side of square 1 collided with the top side of square 2
if (y1 > y2), the top side of square 1 collided with the bottom side of square 2

I suggest you draw yourself a few pictures, and it should become clear to you.

空心空情空意 2024-10-25 03:16:37

你也必须对你的正方形进行建模来捕获方向,只需使用正方形的(x,y,高度),方向我的意思是它是否在第四象限的第一,第二,第三个,可以通过对正方形的所有四个角顶点进行建模。

那么你需要确定,构成这个正方形的所有四个向量

在你拥有的正方形每条边的二维向量之间找到余弦
例如,如果给定边和 2D 矢量的余弦为 1 ,那么两者都是正交的,或者 0 它们是垂直的,任何其他值都将落在两者之间

,或者关于如何确定/定义碰撞的任何其他矢量代数技巧!

you have to model your square to capture the orientation too , just with (x,y,height) of the square , orientation i mean whether its in first ,second ,third of fourth quadrant , may be by modeling all four corner vertices of square .

then you need to determine , all the four vectors that make this square

find cosine between the 2D Vector you have with each side of square
for example if cosine of given side and 2D Vector is 1 , then both are orthogonal or 0 they are perpendicular , any other value would fall in between

or any other vector algebra tricks on how you want to determine/define your collision !

半边脸i 2024-10-25 03:16:37
  if(player.y <= wall.y + wall.height && player.y > wall.y || player.y + player.height  <= wall.y+wall.height && player.y + player.height > wall.y){
            if(player.x -2< wall.x + wall.width && player.x > wall.x){
                leftspeed = 0
            }else if(player.x + player.width +2 > wall.x && player.x + player.width < wall.x + wall.width){
                rightspeed = 0
            }
        }

        if(player.x <= wall.x + wall.width && player.x > wall.x || player.x + player.width  <= wall.x+wall.width && player.x + player.width > wall.x){
            if(player.y -2 < wall.y + wall.height && player.y > wall.y){
                upspeed = 0
            }else  if(player.y + player.height +2> wall.y && player.y + player.height < wall.y + wall.height){
                downspeed = 0
            }
        }

这应该可行,您可能想要删除的所有内容周围都有 2 像素边距

  if(player.y <= wall.y + wall.height && player.y > wall.y || player.y + player.height  <= wall.y+wall.height && player.y + player.height > wall.y){
            if(player.x -2< wall.x + wall.width && player.x > wall.x){
                leftspeed = 0
            }else if(player.x + player.width +2 > wall.x && player.x + player.width < wall.x + wall.width){
                rightspeed = 0
            }
        }

        if(player.x <= wall.x + wall.width && player.x > wall.x || player.x + player.width  <= wall.x+wall.width && player.x + player.width > wall.x){
            if(player.y -2 < wall.y + wall.height && player.y > wall.y){
                upspeed = 0
            }else  if(player.y + player.height +2> wall.y && player.y + player.height < wall.y + wall.height){
                downspeed = 0
            }
        }

this should work, there is a 2 pixel margin around everything that you may want to remove

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