Android opengl-es 碰撞检测 [英] Android opengl-es collision detection

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

问题描述

这让我头疼了几天,所以我终于接受了失败,我要在这里问.

This has been giving me a headache for a couple of days so I have finally accepted defeat and am going to ask here.

我的游戏由不同大小的立方体组成,必须在一个小区域内四处移动.我的立方体运动完美无缺,唯一让我无法理解的是碰撞检测.

My game consists of cubes of varying sizes which have to be moved around in a small area. I have the cube movement working perfectly, the only thing I can't seem to get my head around is collision detection.

地面是完全平坦的,所以我不需要担心 Y 轴,立方体的坐标是相对于地面立方体中心的.我可能还应该补充一点,每一层都由多个彼此相邻的矩形地平面组成.

The ground is completely flat so I don't need to worry about the Y-Axis, the coordinates of the cube are relative to the centre of the cube at ground level. I should probably also add that each level is made up of multiple rectangular ground planes which are positioned adjacent to each other.

欢迎任何关于如何以最少的 cpu 使用率完成此操作(伪代码很好)的建议.非常感谢.

Any suggestions as to how this could be done (pseudo code is fine) with minimal cpu usage are welcome. Thanks very much.

推荐答案

对于立方体或球体之类的碰撞检测非常简单.

Collision detection is very simple with things like cubes or spheres.

您可以使用任一球体到球体检测,这基本上是在立方体周围绘制一个假想球体,然后检查球体的两个中心点之间的距离是否小于球体半径.像这样:-

you can use either sphere to sphere detection which is basically drawing an imaginary sphere around the cubes and then checking if distance between the two centre points of the spheres is less than the sphere radius. like so:-

float x, y, z;
x = SmallModel->GetX() - LargeModel->GetX();    
y = SmallModel->GetY() - LargeModel->GetY();    
z = SmallModel->GetZ() - LargeModel->GetZ();    

float collisionDist = sqrt( x*x + y*y + z*z );  

if (collisionDist < sphereRadius)
{
    // Collision occurred…
}

或者您可以使用更适合此处的边界框,因为上述情况不如边界框准确,因为您使用的是立方体并且它们是轴对齐的.

or you can use bounding boxes which is more suited here as the above is not as accurate where as bounding boxes will be exact since you are using cubes and they are axis aligned.

这里的过程也相当简单:如果每个立方体的最小值和最大值位于彼此之内,则会发生碰撞.IE.如果发生碰撞:

The process here is also fairly simple: there is a collision if the min and max of each cube lies within each other. I.e. collision if:

Cube1XMin > Cube2XMin && Cube1XMax < Cube2XMax &&
Cube1YMin > Cube2YMin && Cube1YMax < Cube2YMax &&
Cube1ZMin > Cube2ZMin && Cube1ZMax < Cube2ZMax

因此您需要为每个立方体声明 6 个变量,如上所述,这些最小值和最大值是通过首先知道立方体的大小然后知道立方体的位置来找到的.

so you will need to declare 6 variables for each cube as above and these min and max values are found by first knowing the size of the cube and secondly knowing the position of the cubes.

所以如果一个立方体是 10 * 10 * 10 并且定位在 100,0,100

So if a cube is 10 * 10 * 10 and positioned at 100,0,100

那么xmin = 95ymin = -5zmin = 95xmax = 105ymax = 5zmax = 105

then xmin = 95 ymin = -5 zmin = 95 xmax = 105 ymax = 5 zmax = 105

找到其他立方体的值并运行上面的方程,你应该知道你是否有碰撞.

find the values for the other cubes and run the above equation and you should find out if you have a collision.

为了处理碰撞,您需要先将立方体移回框架,然后可能让它们弹回,我认为如果您不先将它们移回框架,则可能会卡在彼此内部.

For handling a collision you need to move the cubes back to the frame before and then maybe have them bounce back, i think it is possible to get stuck inside each other if you do not move them back by a frame first.

为了节省 CPU 使用率,您可能可以使用着色器将计算发送到 GPU,但这是一个不同的主题

To save on CPU usage you can probably send the calculations to the GPU using shaders but thats a different topic

这篇关于Android opengl-es 碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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