# calculating position on a sphere

This topic is 4905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
can you explain why is it an indication of poor design\bad idea?

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 on other sites
Quote:
 Original post by No_Germscan 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 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 on other sites
Quote:
 Original post by No_Germsok, 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 on other sites
ummm, i'm really just a beginner to 3d, so the term dot product isn't clear to me...

##### Share on other sites
http://mathworld.wolfram.com/DotProduct.html

given to vectors (a,b,c) and (d,e,f) the dot product would be ad+be+cf.

##### 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 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.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 22
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631733
• Total Posts
3001927
×