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

查看:272
本文介绍了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.

至于怎么可以这样做(伪code是罚款),以最小的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.

这里的方法也相当简单:有冲突,如果每个立方体的最低和最高在于内彼此。即如果碰撞:

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 = 95
     YMIN = -5
     ZMIN = 95
     XMAX = 105
     YMAX = 5
     ZMAX = 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天全站免登陆