# Moving Along a Sphere... my head splode!

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

## Recommended Posts

Ok. I have a camera that focuses on an object. Now, I can get the camera to move along a circle that circles the y-axis (ie the cameras x and z coordinates change). I can also get it so it circles around the x-axis or z-axis, but I am at a loss on how to get it to move along all three at once. Essentially, I need a camera to move along a sphere. The controls would be: Right/Left=move left/right, Up/Down=move forward/backwards on the sphere. I have no idea how to do this. I don't want code, because I want to do it :) but any pointers, links, suggestions would be extrememely helpful. I know this is guaranteed to involve lotsa math... the more the merrier :D Oh, and this is my code for moving the camera along a 2D-plane:
void moveCamera(float dist)        // Dist is a the distance to move along the circle
{
posOnCircle+=dist;                      // Position on the circle... starts at 0.0f

float deltaX = radius * sin(angle);     // The new base coordinate (not really a change (delta) but w/e)
float deltaZ = radius * cos(angle);

cx = center+(int)deltaX;                // Update camera position (center is just an offset)
cz = center+(int)deltaZ;
}
Thanks!

##### Share on other sites
Not sure if its possible for what you are doing, but have u considered rotating the sphere around itself ( with the camera at a stationary spot, offset from the center of the sphere), instead of rotating the camera around the sphere? Might be a little easier to pull off.

Matt Hughson

##### Share on other sites
Sounds like you want the equation of a sphere. A cirlce is usually given by the formulas

x = r*cos(theta)
y = r*sin(theta)

For a sphere you need an additional angle

x = r*cos(theta)*sin(thi)
y = r*sin(theta)*sin(thi)
z = r*cos(theta)

You would then vary the two angles with the up/down and left/right input.

##### Share on other sites
Use a quanternion to rotate about a the vector <1,1,1>.

##### Share on other sites
rotate camera and camera position relatively to sphere around camera's right axis for forward/backward, and around up axis for left/right. If camera is pointing not precisely onto object, you might need to make these vectors be orthogonal to vector from object to camera. (that's simple, A=A-A*(A dot B)/(A.Length()*B.Length()) makes A be orthogonal to B )

Or, that's pretty much the same, rotate object/sphere around it's center. Actually just matter of system of reference.

##### Share on other sites
Wow, thanks Jingo, that worked perfectly :D

Thanks to everyone else too...

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015656
×