0

我正在制作一个简单的 3D OpenGL 游戏。目前我有四个边界墙,随机分布的内墙和一个简单的四边形立方体供我的播放器使用。

我想在玩家和所有墙壁之间设置碰撞检测。这对于边界墙很容易,因为我可以检查 x 或 z 坐标是否小于或大于一个值。问题在于内墙。我有一个 glGenList 包含小的矩形墙段,在初始设置中,我随机生成一个 x、z 坐标数组并将这些墙段转换到绘图场景中的这个位置。我还添加了一个旋转度数,45 度或 90 度,这会使碰撞检测复杂化。

任何人都可以帮助我如何在这里检测碰撞。我有每个墙部分的坐标,每个墙部分的大小以及播放器的坐标。

我会查看播放器和墙壁周围的有界框还是有更好的选择?

4

2 回答 2

0

I think your question is largely about detecting collision with a wall at an angle, which is essentially the same as "detecting if a point matches a line", which there is an answer for how you do here:

How can I tell if a point belongs to a certain line?

(The code may be C#, but the math behind it applies in any language). You just have to replace the Y in those formulas for Z, since Y appears to not be a factor in your current design.

There has been MANY articles and even books written on how to do "good" collision detection. Part of this, of course, comes down to a "do you want very accurate or very fast code" - for "perfect" simulations, you may sacrifice speed for accuarcy. In most games, of the players body "dents" the wall just a little bit because the player has gone past the wall intersection, that's perhaps acceptable.

It is also useful to "partition the space". The common way for this is "Binary space partitioning", which is nicely described and illustrated here: http://en.wikipedia.org/wiki/Binary_space_partition

Books on game programming should cover basic principles of collision detection. There is also PLENTY of articles on the web about it, including an entry in wikipedia: http://en.wikipedia.org/wiki/Collision_detection

于 2013-11-14T14:43:53.563 回答
0

如果没有制作刚体物理引擎,可以使用点到平面距离来查看是否有任何立方体角点与平面的距离小于 0.0f(我会使用 FLT_MIN,因此这些点对它们有一点半径)。您将需要存储一个标准化的 3d 矢量(长度为 1.0f 的矢量)来表示平面的法线。如果从平面中心到该点的向量与普通法线之间的点积小于半径,则表示发生碰撞。之后,您可以获取立方体的速度(向量的长度),将其乘以 0.7f 以吸收一些能量,并将其存储为立方体的新速度。然后将立方体的归一化速度矢量反映在平面法线上,然后将其乘以先前计算的立方体新速度。

如果您真的想进入游戏物理领域,请从这些人的 github中获取信息。我已经将他的书用于游戏物理课。书中有一些错误,所以请务必从 github 获取所有代码示例。他经历了制造质量聚合物理引擎和刚体引擎的过程。我还会复习矩阵和张量。

于 2013-11-14T14:45:27.323 回答