矩形之间的二维碰撞检测

发布于 2024-09-06 04:29:40 字数 198 浏览 0 评论 0原文

我正在为我的游戏编写碰撞检测引擎,但遇到一些问题。

事实上,由于我有几个固定的矩形和一个移动的(玩家),我需要知道固定矩形的哪一侧首先被玩家碰撞,以正确地替换他。

固定矩形不在网格中,因此它们可以放置在地图上的任何位置,并且可以具有不同的大小。它们不旋转。

玩家类存储其方向向量。

有什么想法吗?

风筝

I am writting a collision detection engine for my game and I have some problems.

Indeed, since I have several fixed rectangle and one moving (the player), I need to know which side of the fixed one was collided at first by the player, to replace him correctly.

The fixed rectangle are NOT in a grid so they can be placed anywhere on the map and they can have diffents size. They are not rotated.

The player class stores it's direction vector.

Any idea?

KiTe

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

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

发布评论

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

评论(3

爱你是孤单的心事 2024-09-13 04:29:40

简而言之:
您将相互比较边界矩形的 Y 和 X 分量以检查是否发生碰撞。如果玩家的顶部(Y)小于敌人的底部,那么您不需要再检查,因为它们不可能发生碰撞。如果玩家的右侧(X)小于敌人的左侧,那么他们就不会发生碰撞。这将有助于定义您打算在类内检查的每个对象的顶部、右侧、底部、左侧。这将使您知道哪一侧也被击中。这应该足以让您思考和尝试。
玩得开心!

In a nutshell:
You'll compare the Y and X components of the bounding rectangles to eachother to check for a collision. If the top(Y) of the player is less than the bottom of an enemy then you don't need to check anymore because it's not possible that they're colliding. If the right(X) side of the player is less than the left side of the enemy then they can't be colliding. It would help to define top, right, bottom, left of each object you intend to check inside the class. This will allow you to know which side is hit also. This should be enough to get you thinking and experimenting.
Have fun!

汐鸠 2024-09-13 04:29:40

名称为“轴对齐边界框”碰撞检测。

现在你知道了名字,你可以用谷歌搜索其余的内容。

The name is "Axis-Aligned Bounding Box" collision detection.

Now you know the name, you can Google for the rest.

听不够的曲调 2024-09-13 04:29:40

感谢你们俩的帮助。

我听说过AABB,但乍一看它似乎不符合我的需求(因为我不太了解它)。

但在纸上写下所有内容后,我发现的解决方案似乎与 AABB 完全相同!

thanks to both of you for your help.

I've heard about AABB, but at first sight it didnt seem to fit to my needs (since I didn't understand it well).

But after writing down everything on papper, the solution I found appeared to be exactly the same as AABB !

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