PDA

View Full Version : Mapping Points, Zones, etc on a Sphere



WILL
18-06-2007, 02:23 PM
Hey I was hoping that I could get some quick pointers on how I would be able to do some simple functions for my next game project. (Will start after GQ is completed!)

I'm planning to have the Earth rendered as a single 3D sphere and will need to plot locations on and around the surface. I'll also need to plot and track movement as well as determine specific zones (countries, regions, fallout areas) and day/night.

I can think of how to do it in 2D no problem, but somehow I can't wrap the idea around my head using vectors or 3D maths. :?

JSoftware
18-06-2007, 03:34 PM
How do you want the mapping? Polar, longtitude/latitude, something else?

WILL
18-06-2007, 03:36 PM
I think Long and Lat would be easiest for me. (My brain still works in 2D ;))

Also note that I'm a newcomer to 3D. This will be my first 3D project. (and I'll be using OpenGL if thats relevant)

JSoftware
18-06-2007, 03:46 PM
function LatlongToVec3(latitude, longtitude, sphereRadius: single): TVector3;
begin
result.x := cos(longtitude)*sin(latitude)*sphereRadius;
result.y := sin(longtitude)*sphereRadius;
result.z := cos(longtitude)*cos(latitude)*sphereRadius;
end;
I think that'll do

WILL
18-06-2007, 04:06 PM
Nice! Thanks. :)

Now I'm guessing that I'll just have to plot all my movement in 2D then translate it for display into vectors?

ie. a Plane or craft that is going from say:

Latitude = 43.5167, Longitude = -78.4863

to

Latitude = 53.4880, Longitude = -1.6699

Not bothering with altitude...

JSoftware
18-06-2007, 08:32 PM
Well that's a bit of a problem as you can't linearly interpolate the latlong coordinates to get a straight path. AFAIK you have to use some kind of scheme like spherical interpolation using quaternions.

(PS. And remember that my latlongtovec3 function needs coordinates in radians :P )

WILL
18-06-2007, 09:36 PM
Wow... ok this might not be as super easy as I thought it would be.

Do you think you could give me a simple example of how I'd use this? (maybe a poor-man's diagram just so I get the idea in my head a bit clearer.)

JSoftware
18-06-2007, 11:51 PM
quaternions are not an easy subject. Writing the code for a quaternion spherical linear interpolation here would take too much space. You could have my library if you want it.

I just discovered that there's a way to do it with vectors

function slerp2(src, dest: tvec3; t: single): tvec3;
var d,s1,s2,s3,ld,l: single;
begin
d := arccos(src.x*dest.x+src.y*dest.y+src.z*dest.z);

s1 := sin((1-t)*d);
s2 := sin(t*d);
s3 := sin(d);

result.x := (s1/s3)*src.x+(s2/s3)*dest.x;
result.y := (s1/s3)*src.y+(s2/s3)*dest.y;
result.z := (s1/s3)*src.z+(s2/s3)*dest.z;
{l := sqrt(result.x*result.x+result.y*result.y+result.z* result.z);
result.x := result.x/l;
result.y := result.y/l;
result.z := result.z/l; }//uncomment this to normalize the resulting vector
end;

User137
19-06-2007, 12:16 AM
Ok i might throw some ideas:
- First thought came that sphere can consist of spherified cube; say a cube with NxN grid on each of 6 sides and vertices moved so they are from equal length out of center. This divides sphere more evenly into "tiles". Each side is 90 degrees away from each other.
- Day night calculation. 2 parameters needed: Earth rotation angle, Earth angle towards sun.
- Positioning, i'd place every object using 3D coordinates instead of 2D even when it's surface of sphere. That way it is easier to calculate distances and pathing. Note that paths still aren't linear; earth isn't flat :P Path creates an arc instead of line.. not bothered to draw it, formula might be easy to make.
- Zones/continents could propably be made of arrays of 3D points forming the outlines. Bit tricky checking if object is in area. Only other choice i can think of would be marking each "tile" with zone info, however this is very rough solution. Even as rough solution it could still be used as pre-calculation, narrowing down the choices quickly.

PS. Please somebody make a Starcraft like 3D game that features several planet like battlefields :D

WILL
24-06-2007, 07:05 AM
Thanks a lot for the great information guys. I'm sort of wrapping all the issues that I'm going to be facing in my mind a little clearer now.

However, I must admit that some of the maths that you are trying to explain is going over my head a bit. To be completely honest, I'm a bit of a dumby when it comes to anything much further than basic trig and algebra right now. Though I do want to learn, I'm just unsure where to focus my efforts.

I have an alternative idea to creating a 3D model of the map. I could instead use a 2D version (same texture) and calculate the distance using a parabolic calculation of sorts. Doesn't have to be 100% accurate. It would be plausible solution should I just not be able to get things grooving the other way.

Actually this isn't too far off my original idea of just doing it in 2D (distances, movement, zones, etc) and simply plotting visually in 3D

My end goal is to do this at a level that I can learn rather quickly, yet retain some similarity to the real thing.

As I've said, I'm NEW to 3D. So I would like to dip my toe in to get a feel for it, not jump right into the Atlantic just yet. ;)

WILL
24-06-2007, 07:19 AM
Ok i might throw some ideas:
- First thought came that sphere can consist of spherified cube; say a cube with NxN grid on each of 6 sides and vertices moved so they are from equal length out of center. This divides sphere more evenly into "tiles". Each side is 90 degrees away from each other.

I'm not sure I quite grasp the purpose of creating a cube for the sake of simulating a sphere... :scratch:


- Day night calculation. 2 parameters needed: Earth rotation angle, Earth angle towards sun.

I assumed that day/night would probably be the easiest of them all, heck if I went 2D then it would be a simple matter of using the day/night texture too if I wanted. :)


- Positioning, i'd place every object using 3D coordinates instead of 2D even when it's surface of sphere. That way it is easier to calculate distances and pathing. Note that paths still aren't linear; earth isn't flat :P Path creates an arc instead of line.. not bothered to draw it, formula might be easy to make.

This seems like it might be the toughest way to do this. Nevermind that my math is weak in this area, but the fact that determaning zones would be a royal pain, no?

But you have a very good point about the non-flat earth. :) I know the kind of arch that a path of that size would make having looked at a few charts that plot stuff like that in the Navy, etc... I do believe that there must be a way to calculate a path in 2D though.


- Zones/continents could propably be made of arrays of 3D points forming the outlines. Bit tricky checking if object is in area. Only other choice i can think of would be marking each "tile" with zone info, however this is very rough solution. Even as rough solution it could still be used as pre-calculation, narrowing down the choices quickly.

I think this is what kills me wanting to do 3D entirely. It's already my weak area and this blows what I know out of the water. So it looks like full calculations 3D are out.


I do want to have a 3D representation if I can manage it though. So I guess the only 3D calculations I'll need at from 2 to 3. This does leave the open question of how does one calculate a path simulating the curvature of the earth, given the radius/diameter of the earth across a 2D plane. Gives me something to Google for I guess. ;)

User137
24-06-2007, 09:55 PM
Here's image explaining the spherified cube:
http://i8.tinypic.com/67qxyjb.png

Sounds like you got some interesting 3D maths to study :) Vectors and matrices are much fun when you get to know them better.

WILL
24-06-2007, 10:36 PM
Ah ha! looky what I found :)


Distance = Radius * acos( cos(Lat1)*cos(Lat2)*cos((Long1-Long2)) + sin(Lat1)*sin(Lat2) )

Ok I've got my distance function... Put simply it uses the great-circle formula to figure it out.

Now... I think what I'm aiming for is to figure out how to get a 2D vector from my current point to the destination point. I'd obviously be updating this vector each iteration of my main game loop to get an accurate simulated curve.


btw, I think that I'm going to stick with knots and lat/long for my co-ordinate system. It'll be easiest for both development and the players to wrap their heads around. (Has worked for hundreds of years so far, right? ;))

WILL
25-06-2007, 04:28 AM
Ok this is really killing me. I can't find a direct answer anywhere, is this such a rare thing to look for?

Best I could find is: this (http://kartoweb.itc.nl/geometrics/Map%20projections/body.htm) and this (http://www.progonos.com/furuti/MapProj/Dither/CartDef/cartDef.html#Graticule). :?


What I need to know is how do I plot on an orthographic map of the 'real' shortest movement from one point to another on the surface of a sphere in the form of a simple 2D vector?

I imagine that I'll need an equation that takes the current point, the destination point, the radius of the earth and speed to provide me with a pair of 2D VelX and VelY values. So I guess 2 equations then. :)

Anyone know how to do this?

I should be able to create results similar to something like this...

http://www.progonos.com/furuti/MapProj/Dither/ProjAppl/Img/satpatheqc.png

jdarling
26-06-2007, 02:43 PM
http://upload9.postimage.org/3250/CalculateArconSphere.jpg (http://upload9.postimage.org/3250/photo_hosting.html)