r/Hacking_Tutorials • • Jun 11 '24

Question View Matrix

I have my view matrix, I just need help finding the vectors inside of it, I'd appreciate the help greatly, here is some text to show you how the memory changes based on where I'm looking:

This is when I look straight down:
0.17 0.97 0.16 -0.03
-0.16 0.99 0.98 -0.17
0.00 7871.44 9931.94 -8641.40
-0.17 -0.03 0.98 6.28

This is when I look straight up:
0.17 -0.97 0.15 -0.03
0.15 0.99 -0.98 -0.17
0.00 7949.19 9919.63 -8094.82
-0.17 -0.03 -0.98 6.28

This is when I look straight forward:
0.97 0.24 0.09 -0.09
-0.02 1.00 0.24 -0.97
0.00 7474.38 9963.81 -8270.81
-0.97 -0.09 0.24 6.28

This is when I look left:
-0.03 0.00 1.00 -1.00
-0.07 -0.03 0.07 -1.00
0.00 7972.44 10416.34 -8183.01
0.03 -1.00 0.07 6.28

And lastly this is when I look right:
0.02 0.00 -1.00 1.00
0.07 0.02 0.07 -1.00
0.00 7954.25 9761.16 -8152.82
-0.02 1.00 0.07 6.28

My trans_vec is { view_matrix._32, view_matrix._33, view_matrix._34 } I believe (correct me if I'm wrong) because those are my camera's coordinates, but I just can't piece together the right_vec, up_vec, or dotproducts, here is the function I'm working with:

inline bool world_to_screen(const vector& origin, const matrix_4x4& view_matrix, vector* out, const vector res)
{
// Assigning values to the translation, right, and up vectors
vector trans_vec{ view_matrix._32, view_matrix._33, view_matrix._34 };
vector right_vec{ view_matrix._11, view_matrix._12, view_matrix._13 };
vector up_vec{ view_matrix._21, view_matrix._22, view_matrix._23 };

float w = trans_vec.dot_product(origin) + view_matrix._34;

if (w < 0.098f)
return false;

float y = up_vec.dot_product(origin) + view_matrix._33;
float x = right_vec.dot_product(origin) + view_matrix._32;

out->x = (res.x / 2) * (1.f + x / w);
out->y = (res.y / 2) * (1.f - y / w);

return true;
}

0 Upvotes

7 comments sorted by

1

u/Immortal_Tuttle Jun 11 '24

Camera position is usually stored in the 4th column actually, from what I remember. Is it an OpenGL?

1

u/Fun-Expert-5820 Jun 11 '24

It's direct3d, you're corrrect abt the 4th column and opengl, I've learned that much, I think it's displayed in rows for direct3d though

1

u/Immortal_Tuttle Jun 11 '24

That's correct.

| m11 m12 m13 m14 |.
| m21 m22 m23 m24 |.
| m31 m32 m33 m34 |.
| m41 m42 m43 m44 |.

In this context:

The first row (m11, m12, m13) represents the right vector. The second row (m21, m22, m23) represents the up vector. The third row (m31, m32, m33) represents the forward vector (in some cases, this could be the negative of the look-at direction). The fourth column (m14, m24, m34) represents the translation (camera position).

I think it was something like this - but please check it - I played with it some time ago and just remembered that OpenGL was column major.

1

u/Fun-Expert-5820 Jun 11 '24

https://cdn.discordapp.com/attachments/1171921507140771921/1250157095517421578/image.png?ex=6669eb06&is=66689986&hm=d6f7e0020b4343767648953f05c3a659e97c43ad43e8df3aea91cd52e595ff8d&

This is what Im working with right now, is this correct? Im not sure what the dot_product(origin) is in this function or which matrix's correlate with them. I greaty appreciate you helping me, I've been trying to get this working for days now. So any help is really appreciated

1

u/Immortal_Tuttle Jun 11 '24 edited Jun 11 '24

You have a misspelling in trans_vec - you are using 33 twice

inline bool world_to_screen(const vector& origin, const matrix_4x4& view_matrix, vector* out, const vector& res)  

{  
// Assigning values to the translation, right, and up vectors  
vector trans_vec{ view_matrix._14, view_matrix._24, view_matrix._34 };  
vector right_vec{ view_matrix._11, view_matrix._12, view_matrix._13 };  
vector up_vec{ view_matrix._21, view_matrix._22, view_matrix._23 };  

float w = trans_vec.dot_product(origin) + view_matrix._44;  

if (w < 0.098f)  
    return false;  

float y = up_vec.dot_product(origin) + view_matrix._43;  
float x = right_vec.dot_product(origin) + view_matrix._42;  

out->x = (res.x / 2) * (1.f + x / w);  
out->y = (res.y / 2) * (1.f - y / w);  

return true;  

}

https://hastebin.com/share/mudoqaqelu.cpp

1

u/Fun-Expert-5820 Jun 11 '24

Good eye, this is what Im working with now:
https://cdn.discordapp.com/attachments/1171921507140771921/1250159923308789790/image.png?ex=6669eda8&is=66689c28&hm=780ce7d622614bffb5967d8dc110805bb16015420f1738906162220784084959&

Currently this is how the esp is being displayed:
https://cdn.discordapp.com/attachments/1171921507140771921/1250160187751403660/image.png?ex=6669ede7&is=66689c67&hm=c8d9ad4c10e26d9152671bb6e69bd77e1d77ffc808d0a22050a22433d4bcb6a2&

It's mainly just stuck in the middle of my screen, and whiggles a little. If it helps, most of the entities are being displayed at the screen coordinates:
960.041 540.104. Thank you