Collision detection 2D between rectangles

放肆的年华 提交于 2019-12-02 03:14:46

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!

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

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

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 !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!