Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3990930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:33:13+00:00 2026-05-20T06:33:13+00:00

please help, this is my 4th question about this, I trying so hard I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:33:14+00:00Added an answer on May 20, 2026 at 6:33 am

    I cant’t be sure, but It seems that the coordinate system is wrong. I use (for 2D) something like this:

    - (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;
    }
    

    Note this: size.height-point.y where size contains the size of 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

please help me i am this question again dont block this question i want
Please help me about this issue... In my application i have calender where user
Could you please help me converting this c++ code into python: I am trying
I hope someone can help with this please. I am trying to query an
please help me this problem. I want to split -action=1 to action and 1.
Please help with this issue of using NSFetchedResultsController. I created an object of NSFetchedResultsController
I'm having issues with my noggin... Please help me answer this question: is it
Please help this would be my last problem in dealing with access database with
Please help this beginner here... I have a SQL Server 2008 R2 running on
Please help me with this update query. I COMPLETELY understand how redundant this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.