Coding a Camera

DarkYoda42

New member
I've been having some problems with my camera lately and figured I'd see if any of you guys can figure it out. What I need is for the camera to be able to traverse a sphere around a focus object and be able to move along that sphere when the right mouse button is held in. (Much like in the game Homeworld if anyone has played it). To do so I've been trying to use sphereical coordinates but i'm getting very odd effects from trying to do this, here is the code that i've been using.
r is the radius of the sphere that i would like the camera to be on. m_vPosition.x,y and z are the coords of the camera which i pass to gluLookAt when i'm done.

if (m_vPosition.x == 0) //keeps from divide by 0 error
m_vPosition.x += .01;

r = (float)sqrt(m_vPosition.x * m_vPosition.x + m_vPosition.y * m_vPosition.y + m_vPosition.z * m_vPosition.z);

if (r == 0)
r += .01; // another divide by zero check

theta = acosf((m_vPosition.z/r)); //convert to sphereical
phi = atanf((m_vPosition.y/m_vPosition.x));

theta += idy * .01; //idx and idy are relative mouse movements
phi += idx * .01; //gotten with DirectInput. I KNOW they work
//not sure that adding them is the correct
// thing to do though.

m_vPosition.x = r * sinf(theta) * cosf(phi); //convert back to
m_vPosition.y = r * sinf(theta) * cosf(theta);// x,y,z
m_vPosition.z = r * cosf(theta);*/


so basically i convert my camera position to sphereical, move it by so much as given by mouse input, then tranlate back to regular coords. Then i pass it to gluLookAt. I can't find anything wrong with this but is does not work as I envisioned it. Please help. :)
 
I don't know about some other coders here, but the way the camera works in my program is by translating and rotating the world, while not actually moving or rotating the camera itself at all. According to some people I asked, this is actually the more efficient way to go.
 
Back
Top