please help, this is my 4th question about this, I trying so hard I tried everything!
All I want to do is detect clicking on a object(cube) in a 3D World (3D world created).
This does it http://blog.nova-box.com/2010/05/iphone-ray-picking-glunproject-sample.html but has a completely different app structure and does a lot with render buffers etc.
I am trying to use using gluUnProject (someone has ported it).
On touch
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 ++;
}
Collision function taken from other project
#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;
}
If someone can work out the error, I will even paypal some cash over (if that’s allowed), this has given me a headache for days. I think its something to do when I get the projection and modelview matrix
I cant’t be sure, but It seems that the coordinate system is wrong. I use (for 2D) something like this:
Note this:
size.height-point.ywhere size contains thesizeof the screen. You have to remember that the default coordinate system of the screen in Core Animation for iOS is different from the one used by OpenGL.If you have a translation applied, it is very difficult to figure out what’s going on.
I use this implementation of gluUnProject:
http://www.codng.com/2011/02/gluunproject-for-iphoneios.html
(disclaimer: it is my personal blog).
I know this doesn’t fully answer your question, but it might help.