Some time ago, I was trying to move a camera (in 3D space) around the player, which ultimately is about moving an object around a sphere.
Now, the equation for the position around a circle is pretty easy and is:
1 2 |
x = circleCenter.x + cos(angle) * circleRadius; y = circleCenter.y + sin(angle) * circleRadius; |
However, I needed equations to find the position over a sphere.
After some research and trial and error, I came up with this solution:
1 2 3 |
x = sphereCenter.x + sphereRadius * Cos(longitude) * Cos(latitude); y = sphereCenter.y + sphereRadius * Sin(longitude); z = sphereCenter.z + sphereRadius * Cos(longitude) * Sin(latitude); |
Observations:
- longitude is the angle around the Y axis.
- latitude is the angle around the X and Z axis.
- sphereCenter defines the position of the object we will be moving around.
I hope this will be handy to anyone trying to figure out how to move over a sphere.