See what's going on with flipcode!


Clicking A Sphere
Question submitted by (18 October 1999)

Return to The Archives
  My problem: I have to implement an earth-like menu. This means the user must be able to rotate the earth, click anywhere he wishes and got information and/or actions about the clicked country (!!!). I can imagine some solutions: a)each country has a unique texture ( I dont know if my artists would be able to build something nice with so many textures); b)save the information about "what is the country of this specific face" in every face; c) move to another less-crazy career. :( Do you know if any of this solutions is good enough? Is there any other better solution? Has anyone already did something like this? Should I go to a mental institution?  

  I think the solution is quite simple. First, you'll need to decide how to texture the sphere since this is directly related to solving your 'picking' problem. There are many (and I do mean many) different projections of the Earth maps. You want to stay away from the projections that look like a football that's been cut up and laid out flat (like the Goode Homolosine.) These projections are great for "flattening" the Earth and balancing spatial relationships of each continent, but they're no good for texturing. The two you'll probably be most interested in are the Cylindrical Projection or the Mercator Projection.

To help explain, consider the following example. If you were to project the Earth's surface onto a virtual mapping cylinder (using the Earth's axis as the center axis for the mapping cylinder) you'll notice something odd about the resulting projected texture. At the tops and bottoms of the texture, the image would appear to be compressed vertically. Near the equator, however, there would be no compression. The reason is because of the increasing angle of the Earth's surface as it approaches the poles. This result is a cylindrical projection map. This projection has a drawback. If you're mapping a perfect sphere (i.e. infinite number of polygons) your texture will be warped at the poles, unless you have infinite precision in your texture map. As polygonal spheres aren't so accurate, this may work for you. But consider this before you decide.

The Mercator projection map is easier to work with and it doesn't stretch across the poles, but it requires a slight modification to your UV mapping. A Mercator projection-map is a curvature-adjusted (i.e. non-warped) version of the Cylindrical projection-map. This is easier for your artists to use. It's also a no-brainer to map. Simply use sine while picking your latitudinal UVs (0 at the equator, 1 at the poles) and you'll have a properly mapped sphere using the Mercator projection.

Once you have your sphere mapped using a projection that is mathematically sane, your point-picking becomes much easier. You can even use a sphere-ray intersection to pick the exact point on the surface. From that, you can get the U/V coordinates (this stuff is documented in lots of places -- it's how ray tracers texture map spheres.) Just make sure to adjust your UVs based on the projection map you've chosen.

Now you've got the pixel in the texture map, what then? You could have your artists create two textures. The first is the image, the second is the database, where each continent and/or country has its own specific color. When you find your UVs by reverse projection, you use those to perform a lookup into the database image, pick the color (read: "database key") and provide the information.

Or, you could always seek psychological council. It's your choice. :-)

Response provided by Paul Nettle

This article was originally an entry in flipCode's Fountain of Knowledge, an open Question and Answer column that no longer exists.


Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.