简体   繁体   English

矩形之间的碰撞检测2D

[英]Collision detection 2D between rectangles

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 风筝

The name is "Axis-Aligned Bounding Box" collision detection. 名称为“轴对齐边界框”碰撞检测。

Now you know the name, you can Google for the rest. 现在您知道了名称,剩下的可以用Google了。

In a nutshell: 简而言之:
You'll compare the Y and X components of the bounding rectangles to eachother to check for a collision. 您将相互比较边界矩形的Y和X分量以检查是否存在碰撞。 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. 如果玩家的最高(Y)小于敌人的最低,则您不再需要检查,因为他们不可能发生碰撞。 If the right(X) side of the player is less than the left side of the enemy then they can't be colliding. 如果玩家的右侧(X)小于敌人的左侧,那么他们将无法碰撞。 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! 玩得开心!

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). 我听说过AABB,但乍一看似乎不符合我的需求(因为我不太了解)。

But after writing down everything on papper, the solution I found appeared to be exactly the same as AABB ! 但是在将所有内容写下来后,我发现的解决方案似乎与AABB完全相同!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM