Public Group

# Finding Up and Right vectors from Look for view matrix?

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

## Recommended Posts

I want to be able to set the direction the camera is facing but I can't figure out how to determine the other vectors from the view direction.

##### Share on other sites

Assuming you can't roll your camera, and can't ever look directly upwards:

Get the cross-product of forward and world-up (0,1,0) - use that as right.

Get the cross-product of forward and right - use that as up.

##### Share on other sites

What if I want to be able to set it upwards?

##### Share on other sites

What if I want to be able to set it upwards?

Then you have an infinite amount of possibilities without knowing which one to use (from the targeted direction alone). So either you choose a heading heuristically or have historical information (about previous heading) how the upward direction was reached. By the way, the same is true for looking straight downwards.

##### Share on other sites
I prefer to rebuild my direction vectors from tracked rotations. (One time per frame(when dirty) without restriction)
But that's just me, and my apologies, because I understand the OP asked for a method from a view direction.

// tracked axis rotation method
float rotX;
float rotZ;

vec3 forward = vec3( cos(rotX) * sin(rotY), sin(rotX), cos(rotX) * cos(rotY) );
vec3 right   = normalize( vec3( cos(rotZ) * sin(rotY + half_pi), sin(rotZ), cos(rotZ) * cos(rotY + half_pi) ) );
vec3 up      = normalize( cross(right, forward) );

Then there is the inverse transpose and dotted translation part to build the view matrix from here.

no, he says "from" look for view matrix.
V
V Edited by Goliath Forge

##### Share on other sites
You wrote "from" the look matrix, you have a matrix already?

If so, you can use the basis vectors for the matrix.

A transformation matrix has values that can be used like this:

A A A E
B B B E
C C C E
D D D F

(or transposed, running in the columns if you have a column-major ordering)

The three A values are a vector pointing in the X direction. The B values are the Y axis direction, the C values are the Z direction. D is the translation, or position relative to the origin. E is the shear of the world. F is best as a flag, 0 for vector, 1 for a point.

So in the identity matrix:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

You have an X axis direction of (1,0,0), a Y axis direction of (0,1,0), and a z axis direction of (0,0,1).

Or using fake values from something that isn't a real affine transformation matrix:

1 2 3 0
4 5 6 0
7 8 9 0
0 0 0 0

X axis is (1,2,3), Y axis is (4,5,6), Z axis is (7,8,9)

From there, finding which axis maps to forward, up, and right should be straightforward. :-)

1. 1
2. 2
3. 3
Rutin
16
4. 4
JoeJ
13
5. 5

• 10
• 9
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632646
• Total Posts
3007637
• ### Who's Online (See full list)

There are no registered users currently online

×