# Creating a matrix using just it's forward vector

## Recommended Posts

I cant figure this out it's really hard for me. I need a 3x3 matrix and the vector would be the forward vector but I need to make a up and right vector from the forward vector. I know once I have the up or right one I can get the cross product to create the other vector can anyone help me please?

##### Share on other sites
You can't. There's an infinite number of possibilties.

However, you can pick an arbitrary vector for up (E.g. (0, 1, 0)), and just make sure it's not perpendicular to your forward vector. Then you can use that the get a right vector, and then you can use those two to regenerate a correct up vector.

I think anyway...

##### Share on other sites
input vector flet u and r be vectorsu.z = f.z;if(f.y == 0){  u.x = 0;  u.y = 1;}else{  u.x = 1;  u.y = -f.x / f.y;}normalize(u);r = cross(f,u);normalize(r);

##### Share on other sites
Quote:
 Original post by Evil SteveYou can't. There's an infinite number of possibilties.However, you can pick an arbitrary vector for up (E.g. (0, 1, 0)), and just make sure it's not perpendicular to your forward vector. Then you can use that the get a right vector, and then you can use those two to regenerate a correct up vector.I think anyway...

I understand what your saying but it would be quite a hack job. lol

Quote:
 Original post by dragongameinput vector flet u and r be vectorsu.z = f.z;if(f.y == 0){ u.x = 0; u.y = 1;}else{ u.x = 1; u.y = -f.x / f.y;}normalize(u);r = cross(f,u);normalize(r);

heres what I did:

VS_OUTPUT VS(float3 position : POSITION, float2 tex1 : TEXCOORD0){    VS_OUTPUT Out; float3 f = normalize(View[3]); float3 u; float3 r;u.z = f.z;if(f.y == 0){  u.x = 0;  u.y = 1;}else{  u.x = 1;  u.y = -f.x / f.y;}u = normalize(u);r = cross(f,u);r = normalize(r);    Out.position = mul(float4(mul(position,float3x3(r,u,f)),1), WorldViewProj);    Out.tex1 = tex1;    return Out;}

and it didnt work... makes it flat and it does crazy things. btw going to use it in a shader for a billboard and the object is at 0,0,0 so I just normalized the camera's position.

##### Share on other sites
They actually both work it was just that 3D max puts the z axis as the y but now I want to know witch is faster?

VS_OUTPUT VS(float3 position : POSITION, float2 tex1 : TEXCOORD0){    VS_OUTPUT Out; float3 f = normalize(View[3]); float3 u=float3(0,0,1); float3 r=normalize(cross(f,u)); u=cross(f,r);    Out.position = mul(float4(mul(position,float3x3(u,r,f)),1), WorldViewProj);    Out.tex1 = tex1;    return Out;}

VS_OUTPUT VS(float3 position : POSITION, float2 tex1 : TEXCOORD0){    VS_OUTPUT Out; float3 f = normalize(View[3]); float3 u; float3 r;u.z = f.z;if(f.y == 0){  u.x = 0;  u.y = 1;}else{  u.x = 1;  u.y = -f.x / f.y;}u = normalize(u);r = cross(f,u);r = normalize(r);    Out.position = mul(float4(mul(position,float3x3(r,u,f)),1), WorldViewProj);    Out.tex1 = tex1;    return Out;}

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628367
• Total Posts
2982284

• 10
• 9
• 13
• 24
• 11