矩形上的碰撞检测,如何知道哪边被击中

发布于 2024-09-29 09:29:11 字数 85 浏览 0 评论 0原文

我想为矩形创建一个碰撞检测系统以使用一些物理原理,因此每条边都会有一个法线。如何确定物体击中了正方形的哪一侧。另一个物体是一个圆。

谢谢。;

I want to do a collision detection system for a rectangle to use some physics, so there will be a normal for each of the sides. How do I determine which side of the square was hit by the object. The other object would be a circle.

Thank you.;

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

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

发布评论

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

评论(2

压抑⊿情绪 2024-10-06 09:29:11

如果不知道您正在使用的库(或者查看您的代码,如果您自己编写代码),则很难准确地知道,但大概您只是在寻找两个对象主体中的重叠部分。

该算法必须进行某种数学运算,以确定一个对象的坐标是否位于另一个对象的边界内。假设您只是在 xy 平面上工作,您可以将一个对象的中心与重叠区域进行比较。正 delta x,右;负 delta x,左。 y 轴的工作方式相同。如果拐角发生碰撞,这不一定能给出有保证的答案。

It's hard to know exactly without knowing the library you're using (or seeing your code, if you wrote it yourself), but presumably you're simply looking for overlap in the bodies of the two objects.

The algorithm has to do some sort of math to find out if there are coordinates of one object inside the boundaries of the other object. Assuming that you're just working on an x-y plane, you can compare the center of one object to the overlapping area. Positive delta x, right; negative delta x, left. The y axis would work the same way. This wouldn't necessarily give a guaranteed answer if the corners collided.

浅忆 2024-10-06 09:29:11

我知道这已经晚了,但可以将碰撞到矩形的点,从与其碰撞的矩形的中心减去它。从减法中取出新向量,并将其与矩形的四个法线进行比较,以最接近被击中的一侧为准。当你只有几条边时,这是一种快速方法,但当你有很多边来比较时,你会放慢速度。我同意这个问题与代码无关,他没有要求我们为他编码。他要求一个算法。

I know this is late but it is possible to take the point colliding into the rectangle, subtract it from the the center of the of the rectangle it collided with. Take the new vector from the subtraction and compare it to the four normals of the rectangle, whichever its closest to is the side that was hit. A quick method when you only have a few sides on the but when you have lots of sides to compare it with you will slow down. And I agree this question has nothing to do with code, he didn't ask us to code it for him. He asked for an algorithm.

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