Sign in to follow this  
mattbbangin

Camera Face-Towards Target

Recommended Posts

I know this question has probably been answered dozens of times throughout the internet; but for some reason, I can't seem to find the info I need. Perhaps the words I am limited to are too vague. Anyways.. I have a Quaternion-based Camera. It works very nicely: typedef struct { double heading; // degrees double pitch; double roll; double velocity; Quaternion qHeading, qPitch, qRoll; Vector3 position, direction; } tQuatCamera; Just set the heading, pitch, and roll members and it rotates the camera. Very simple. Now I would like to rotate the camera so that it faces towards a target position. I've made several attempts, but none have worked out. Could someone please explain or point me to an explanation of how to do this?

Share this post


Link to post
Share on other sites
ohh, man. you're doing something strange.

Quaternion cameras usually done, and i think should be done so:
I have one quaternion to store orientation, and vector to store position.
I can make methods
setHeading(.....);
setPitch(.....);
....
...
getHeading();
getPitch();


To turn camera to face certain direction, you need to compute camera's lookat vector(transform 0,0,-1 by camera's quaternion), and find shortest ark rotation quaternion that turn lookat to direction you want, and multiply camera's quaternion with it.

Share this post


Link to post
Share on other sites
What he's doing is okay. I believe he is simply applying rotations in seperate quats, then combining them all when they are needed. You are not going to be able to do much of the bonuses that a quaternion gives you doing it this way. Also, with a quaternion, your heading, pitch, and roll double members are practically worthless. By the time you multiply all of those quats, you'll have something completely different (roll may be doing what pitch should do, pitch may be twisting, etc). So stopping one from going so high or low won't make much sense.

In any case, can you get the forward direction of your camera? Get a direction vector for the forward in your cam, then get a direction vector from the camera to the direction you want to face. If you cross those, you have a rotation axis. If you dot them ( acos on the dot ), you have an angle. So do an axis-angle rotation to reach the new direction. But since the trip will be instant, you'll need to interpolate to do this smoothly over frames. Or I suppose you could reduce angle to a certain limit. I've never tried that [smile]

Clarification:
Axis = CROSS( DirCamForward, DirToTarget )
Angle = acos( DOT( DirCamForward, DirToTarget ) )
AxisRotation( SomeQuat, Axis, Angle )
MyCamera.Quat *= SomeQuat
MyCamera.Quat.Normalize()

Share this post


Link to post
Share on other sites
I'm horribly new to quaternions. My original camera was completely vector-based and it sucked. Something about gimbal-lock or along those lines. This quaternion camera works very nicely, and is actually easier for me to use (along the lines of generic camera functionality). I do agree that doing a quaternion camera this way isn't the best way, and I'm even deciding whether I should go Dmytry's way of doing it - since it seems like I can't do this face-towards with my current camera.

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