Camera Face-Towards Target

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

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 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
setPitch(.....);
....
...
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 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 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.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• Forum Statistics

• Total Topics
631710
• Total Posts
3001845
×