• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Rotating a camera around a sphere

Hey all.

Does anyone know how to do this? I'm trying to do this currently and having limited success. Has anyone been able to successfully achieve being able to do this properly?

Thanks a lot!

Martin.
 

rytan451

Member
It depends on how you're defining the point on the sphere. I've detailed two methods below:

If you know the latitude and longitude of the camera on the sphere, a bit of basic trigonometry will give you the coordinates of the camera.

Let theta be the latitude (where 0 is the equator, +90 degrees is the north pole, and -90 degrees is the south pole).

Let phi be the longitude (with 0 being the prime meridian).

Let r be the radius of the sphere where the camera may be.

Assuming that the sphere is around the origin (you can translate the sphere by adding constants to the coordinates):

x = sin(theta) cos(phi)
y = sin(phi)
z = cos(theta) cos(phi)

(Assuming that the vertical axis, perpendicular to the equator, is the y axis).
Given a vector in the direction of the camera on the sphere, a bit of basic vector math will give you the coordinates of the camera.

Let V be the direction vector. Thus, V1, V2, and V3 are the x, y, and z components of the vector, and its magnitude |V| = sqrt(V1^2 + V2^2 + V3^2).

Let r be the radius of the sphere.

Assuming the sphere is around the origin (you can translate the sphere by adding constants to the coordinates):

x = rV1 ÷ |V|
y = rV2 ÷ |V|
z = rV3 ÷ |V|

Note that I've written all of this in a mathematical notation, and you'll have to convert it to code yourself.
 
Thanks so much for taking the time to detail all this, it's very much appreciated. I've been trying the trigonometry based with some success but haven't quite cracked it. I might well give the vector based approach a whirl too to see how I fare there. Thanks again. :)
 

rytan451

Member
Thanks so much for taking the time to detail all this, it's very much appreciated. I've been trying the trigonometry based with some success but haven't quite cracked it. I might well give the vector based approach a whirl too to see how I fare there. Thanks again. :)
Remember that if you're using degrees, you'd want to use the dcos, dsin functions, not the cos, sin functions, which work on an input of radians.
 
Top