Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a sphere that represents the Earth textured with real pictures. It's rotating around the x axis, and when user click down it has to show me the exact place he clicked on. For example if he clicked on Singapore the system should be able to:

  • understand that user clicked on the sphere (OK, I'll do it with unProject)
  • understand where user clicked on the sphere (ray-sphere collision?) and take into account the rotation
  • transform sphere-coordinate to some coordinate system good for some web-api service
  • ask to api (OK, this is the simpler thing for me ;-)

some advice?

share|improve this question
Did you mean "represents the EARTH"? – Bill Mar 7 '11 at 0:53
ohu, thanks!n edited – nkint Mar 7 '11 at 11:23
up vote 0 down vote accepted

Will's answer is perfect until you want to do some displacement (which you want propably if simulating planet).

In my opinion the color picking would be the best solution. It is fast to implement and works with o(log(n)) complexity (+ time to render). Every triangle on the sphere will have unique color. Render planet with only colors, no lighting in same resolution as screen. Get clicked point and at the same position as on the screen, check color. Find object with the color. Objects can be simply ordered (color is number) so binary search with log(n) complexity can be used.

We used this simple and old fashion technique for our game Save Us! for geting areas on planet with no performance loss. So i can tell you, that it is suitable for realtime application.

share|improve this answer

Open simple approach is colour picking.

Create two versions of your world map texture that you texture your sphere with. The first is for the user to see, the second for the selection. On the second, each country is drawn with a unique colour.

When the user clicks, render the scene without lighting or other effects, and using this second colour-coded texture. Don't swap-buffers - don't show this version to the user. But then read the colour value using glReadPixels as in the article above.

share|improve this answer

Your approach looks good. Some links for each step, assuming OpenGL:

  1. gluUnproject the x,y of the click (flipping the y if necessary; screen coordinates for clicks are typically 0,0 is top-left of screen, whereas OpenGL 0,0 is bottom-left) for near (0.0) and far (1.0). This gives you a line segment which is the ray projecting from your mouse cursor through the scene. (gluUnproject is deprecated, so you might want to copy-n-paste it into your code from here.)

  2. find line-sphere intersection code e.g. from this excellent page.

  3. inverse the rotation of the nearest intersection point to account for the sphere's rotation e.g. adapt this.

share|improve this answer
thanks! but with line-sphere intersection give me 2 points, and thet're in world coordinate. how to convert them into texture coordinate? – nkint Mar 7 '11 at 11:22
The point nearest the screen is the interesting one – Will Mar 7 '11 at 12:02
great resource bytheway! – nkint Mar 11 '11 at 20:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.