gpt4 book ai didi

iPhone OpenGL : Using gluUnProject (port) and detecting clicking on an object

转载 作者:行者123 更新时间:2023-12-03 21:21:15 25 4
gpt4 key购买 nike

请帮忙,这是我的第四个问题,我很努力,我尝试了一切!

我想做的就是检测 3D 世界(创建的 3D 世界)中对象(立方体)的点击。
这样就可以了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;
}

如果有人能解决这个错误,我什至会支付一些现金(如果允许的话),这让我头疼了好几天。我认为当我得到投影和模型 View 矩阵时需要做一些事情

最佳答案

我不能确定,但​​坐标系似乎是错误的。我使用(对于 2D)这样的东西:

- (CGPoint) unProject: (CGPoint) point
{
GLfloat x=0, y=0, z=0;
GLfloat modelMatrix[16];
GLfloat projMatrix[16];
GLint viewport[4];
glGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix);
glGetFloatv(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);

CGPoint result = { 0.0f/0.0f, 0.0f/0.0f };
if(gluUnProject(point.x, size.height-point.y, 0, modelMatrix, projMatrix, viewport, &x, &y, &z) == GL_TRUE) {
result.x = x;
result.y = y;
//Since we're using an ortho projection, we can ignore z.
UNUSED(z);
}
return result;
}

请注意:size.height-point.y,其中 size 包含屏幕的尺寸。你必须记住,Core Animation for iOS 中屏幕的默认坐标系与 OpenGL 使用的不同。

如果您应用了翻译,则很难弄清楚发生了什么。

我使用 gluUnProject 的实现: http://www.codng.com/2011/02/gluunproject-for-iphoneios.html(免责声明:这是我的个人博客)。

我知道这并不能完全回答您的问题,但可能会有所帮助。

关于iPhone OpenGL : Using gluUnProject (port) and detecting clicking on an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4403228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com