请帮忙,这是我的第四个问题,我很努力,我尝试了一切!
我想做的就是检测 3D 世界(创建的 3D 世界)中对象(立方体)的点击。
这样就可以了http://blog.nova-box.com/2010/05/iphone-ray-picking-glunproject-sample.html http://blog.nova-box.com/2010/05/iphone-ray-picking-glunproject-sample.html但有一个完全不同的应用程序结构,并且在渲染缓冲区等方面做了很多工作。
我正在尝试使用 gluUnProject (有人已经移植了它)。
触摸时
CGPoint pos = [[touches anyObject] locationInView:self.view];
glGetIntegerv( GL_VIEWPORT, __viewport );
glGetFloatv( GL_MODELVIEW_MATRIX, __modelview );
glGetFloatv( GL_PROJECTION_MATRIX, __projection );
int i = 0;
for (NSDictionary *item in self.players) {
IMPoint3D playerPos;
playerPos.x = [[item objectForKey:@"posX"] floatValue];
playerPos.z = [[item objectForKey:@"posZ"] floatValue];
playerPos.y = 1.0f;
if([self checkCollission:pos object:playerPos])
{
NSLog(@"FIRE I LOVE YOU MAN %i", i);
}
i ++;
}
碰撞函数取自其他项目
#define RAY_ITERATIONS 1000
#define COLLISION_RADIUS 0.1f
-(Boolean) checkCollission:(CGPoint)winPos object:(IMPoint3D) _object {
winPos.y = (float)__viewport[3] - winPos.y;
Point3D nearPoint;
Point3D farPoint;
Point3D rayVector;
//Retreiving position projected on near plan
gluUnProject( winPos.x, winPos.y , 0, __modelview, __projection, __viewport, &nearPoint.x, &nearPoint.y, &nearPoint.z);
//Retreiving position projected on far plan
gluUnProject( winPos.x, winPos.y, 1, __modelview, __projection, __viewport, &farPoint.x, &farPoint.y, &farPoint.z);
//Processing ray vector
rayVector.x = farPoint.x - nearPoint.x;
rayVector.y = farPoint.y - nearPoint.y;
rayVector.z = farPoint.z - nearPoint.z;
float rayLength = sqrtf(POW2(rayVector.x) + POW2(rayVector.y) + POW2(rayVector.z));
//normalizing ray vector
rayVector.x /= rayLength;
rayVector.y /= rayLength;
rayVector.z /= rayLength;
Point3D collisionPoint;
Point3D objectCenter = {_object.x, _object.y, _object.z};
//Iterating over ray vector to check collisions
for(int i = 0; i < RAY_ITERATIONS; i++)
{
collisionPoint.x = rayVector.x * rayLength/RAY_ITERATIONS*i;
collisionPoint.y = rayVector.y * rayLength/RAY_ITERATIONS*i;
collisionPoint.z = rayVector.z * rayLength/RAY_ITERATIONS*i;
//Checking collision
if([Tools poinSphereCollision:collisionPoint center:objectCenter radius:COLLISION_RADIUS])
{
return TRUE;
}
}
return FALSE;
}
如果有人能解决这个错误,我什至会支付一些现金(如果允许的话),这让我头疼了好几天。我认为当我得到投影和模型视图矩阵时需要做一些事情