Sign in to follow this  

Cam rotation prob

Recommended Posts

Hi, I posted this same question in OpenGl (wrong place).. Given that the viewer / cam is at point x,y,z and that the center of gravity, of some rock in space, is at 0,0,0. I have a heading controlled by the mouse x. this heading is an angle around the axis created by 0,0,0 -> Cam x,y,z, and have the view aligned to that axis. Basically as if you were standing on the surface. So If I were to use glLookAt the cam is at x,y,z, lets say up is x*2,y*2,z*2 and the target is the vector perpendicular to cam x,y,z rotated by "Heading". I have a good FPS cam working no problem, but cant seem to get my head around the additional ground orientation, that is the cam being on the surface of a sphere. I think I need to do the following but am not sure of the math. If the vector made by CamX,Y,Z is normalized that that normal can be said to belong to a plane on witch I would find a point (via Sin,Cos) of my Heading angle this point will be translated by the cam position and become the lookat or eye point for the glLookAt function. Any thoughts.

Share this post

Link to post
Share on other sites
The problem may be that you need a frame of reference to find your direction vector via sin() and cos(). Perhaps the best way to solve this involves aligning your object to the surface normal, either instantaneously or via slerp(). However, I'll take a shot at a more quick-and-dirty solution.

Maintain a position vector and a forward direction vector. The position vector also serves as the surface normal and up vector, as you've described.

When updating, start by rotating the direction vector about the up vector in response to user input. Normalize the up vector to get a unit-length normal, and then perform an axis-angle rotation rotating the forward vector about this normal. The formula for axis-angle rotation should be easy to find online, either in direct or matrix form.

Now move your object along the direction vector. After the step the object will probably be a bit above the surface, so adjust its height accordingly.

At this point the forward vector will no longer be perpendicular to the up vector. This can be fixed as follows:

Vector3 n = up.GetNormalized();
forward -= forward.Dot(n) * n;

Anyway, that's just off the top of my head, and I may have missed something somewhere. But you might give it a try.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this