Sign in to follow this  
No_Germs

calculating position on a sphere

Recommended Posts

the game i'm programming has a man running on planet. now, i thought that using XYZ coordinate system would'nt be right, and that it would be much more easy to use angles describing to position of the man on the planet. i used two angles, one(alpha) is the angle between the man to the center of the planet on the XY plain, and the other (beta) is the angle between the man to the XY plain. so: X=cos(alpha)*cos(beta) Y=sin(alpha)*cos(beta) Z=sin(beta) hope i got thrrough to you :) now comes the question: when the man runs around the planet, i have to calculate how those two angles change, and this envolves a new angle- the angle of the person round it's axis (is he facing north pole, south pole etc...). this got a little too complex on my behalf... can someone help me? either by showing me how to calculate the new alpha and beta after the person had move, or by proposing an easier method... thanks :)

Share this post


Link to post
Share on other sites
working in spherical coordinates is generally a bad idea.

you can always convert back to spherical if you need to do so later on, although usually if you need to go to spherical its an indication of poor design imho. usually there are more elegant solutions. all physic-y stuff is most easily done in a simple linear space in any case. simply enforce your charater to stay on the sphere by normalizing its position vector and multiplying it by the radius of the sphere you want him to walk on.

Share this post


Link to post
Share on other sites
can you explain why is it an indication of poor design\bad idea?

about you second suggestion- i already thought about that solution, but:
1.the maximum distance my charecter can move by using this method is a quarter of the sphere, and the change in location isn't linear to the speed(with a very large speed the travel distance will approximate to 2*PI*r/4).
2.it still doesn't solve my problem, cause now i'll have to calculate the direction of the charecter.
I know the common approach is matrices, but i feel that for this game it's much more simple to use spherical coordinates since the charecter won't go off the planet...

Share this post


Link to post
Share on other sites
Quote:
Original post by No_Germs
can you explain why is it an indication of poor design\bad idea?

because spherical coordinates always compicate things in the end, and besides the shperical space isnt even continious, meaning almost all algorithms will run in some sort of trouble at the poles.

Quote:

about you second suggestion- i already thought about that solution, but:
1.the maximum distance my charecter can move by using this method is a quarter of the sphere, and the change in location isn't linear to the speed(with a very large speed the travel distance will approximate to 2*PI*r/4).

yes, its an approximation. so is using finite precision to store your variables. the question is, does it matter? chances are big it doesnt.

Quote:

2.it still doesn't solve my problem, cause now i'll have to calculate the direction of the charecter.

which you somehow wouldnt have to do otherwise? besides handling orientations shouldnt have to be very complex.

Quote:

I know the common approach is matrices, but i feel that for this game it's much more simple to use spherical coordinates since the charecter won't go off the planet...

it isnt. just use a vector for position you constrain to be on the spheres surface, and a speedvector you constrain to be tangent to the sphere.

Share this post


Link to post
Share on other sites
ok, i agree with everything you said, but...
"a speedvector you constrain to be tangent to the sphere"
i dont know how to make the speedvector tangent to the sphere... :(

Share this post


Link to post
Share on other sites
Quote:
Original post by No_Germs
ok, i agree with everything you said, but...
"a speedvector you constrain to be tangent to the sphere"
i dont know how to make the speedvector tangent to the sphere... :(

thats not too hard. just substract all parts of the speedvector that are normal to the sphere.
to get the part normal to the sphere, simply do:

vector normalcomponent = speedvector.dot(position.normalize()) * speedvector;

so:

speedvector -= normalcomponent;

Share this post


Link to post
Share on other sites
Thanks. in the meantime i read about matrices and decided ti'll be best i i used them. Where can i find a good link on yaw-pitch-roll angles?

Share this post


Link to post
Share on other sites
You can find physics equations in spherical coordinates if you search the net. Just look for "metric tensor in spherical coordinates", which is what you would want to have a good foundation for working with them. Although if you're just working on the surface of the sphere, probably you can get by with what Eelco says about ignoring normal components of velocity. Another thing to consider is a way to get a north pointing vector and an east pointing vector as a function of your position on the sphere, so your man can carry a compass.

Let a = alpha and b = beta. If X(a,b),Y(a,b),Z(b) is the system you described, then I think that your north pointing vector is:

n(a,b) = <(dX/db),(dY/db),(dZ/db)>

and your east pointing vector is

e(a,b) = <(dX/da),(dY/da),(dZ/da)>

C.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This can be done very easily using just normal cartesian cordinates. All you have to know is how to add two vectors and how to do a cross product. Let me explain.

Assume you have a vector that tells the position of the man on the sphere (p). You also need a vector to store the direction (d). Clearly, there is a problem with possible corrupt vectors but we'll deal with that later.

Assume you man moves 'forward' a radians. (This equals a length of a/(2Pi)*R .) All you have to do is modify the position and forward vectors. Look at the following speudo code and see if it makes some sense.

pn=cos(a)*p+sin(a)*d; // new position
dn=cos(a)*d-sin(a)*p; // new direction
p=pn; // set 'em
d=dn;

How do you change the direction of the man then? We'll, you might want calculate the vector that points 'right' (or why not left) using the vector cross product. Then you modify the direction vector in basically the same way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this