矩形之间的碰撞检测2D [英] Collision detection 2D between rectangles

查看:209
本文介绍了矩形之间的碰撞检测2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我书面方式对我的比赛碰撞检测引擎和我有一些问题。

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.

的固定矩形不在一个网格,以便它们可以在任意位置放置在地图上,他们可以有diffents大小。它们不旋转。

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.

你知道吗?

风筝

推荐答案

在一言以蔽之:
你会在边界矩形的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!

这篇关于矩形之间的碰撞检测2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆