1
\$\begingroup\$

I have a camera class that is missing some functionality.

I need to give it the ability to, given a direction, or a point to look at, will rotate the camera left/right and up/down to look at this point or along this direction, without causing gimbal lock.

lets, say I move the camera to (10,10,10).

I want the camera to point at (0,0,0).

By normalizing the vector , I find that the direction I want to look along is (-0.577,-0.577,-0.577). We'll call this "Forward".

What I want to be able to do, is compute the local "Up" and "Right" vectors which are perpendicular to Forward.

How do I do this?

\$\endgroup\$
2
  • \$\begingroup\$ You should use quaternions to avoid gimbal lock \$\endgroup\$
    – Bálint
    Commented Apr 8, 2017 at 19:42
  • \$\begingroup\$ gamedev.net/topic/… use the cross product \$\endgroup\$
    – CobaltHex
    Commented Apr 8, 2017 at 20:01

1 Answer 1

4
\$\begingroup\$

First, keep in mind that you need some other piece of information. A direction by itself has no intrinsic "up" direction. You'll typically have some kind of global "up" defined by your game, usually (0,+1,0) or the like.

From this, you can use cross products to calculate the "right" vector (or "left" vector, depending on handedness in use). The cross product of your normalized forward/view vector and the normalized world up vector gives you the right vector.

From there, you can now calculate the view's "up" vector, by taking the cross product of the right vector and the normalized forward/view vector.

\$\endgroup\$
2
  • \$\begingroup\$ The only thing I would add to this is that to get the "right" unit vector, you have to normalize the result of the cross product. \$\endgroup\$
    – Ian Young
    Commented Apr 8, 2017 at 20:43
  • \$\begingroup\$ Watch out that there's a singularity in the event that your look direction is parallel to your up vector, so if your camera has no limits on its pitch then you'll need a backup solution for these cases. \$\endgroup\$
    – DMGregory
    Commented Apr 8, 2017 at 21:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .