2y ago · Mar 5, 2024 11:20 AM
A common stumbling block for beginners when writing their first ESP overlay is getting the View Matrix calculation wrong due to Row-Major vs Column-Major matrix layout differences.
The Mathematical Formula:
Given a 4x4 View-Projection Matrix and a 3D World position :
Pro-tip: If your ESP is rendering inverted (left is right, or top is bottom), simply check whether the game's matrix is column-major vs row-major .
The Mathematical Formula:
Given a 4x4 View-Projection Matrix
CPP
M CPP
v CPP
struct Vector3 { float x, y, z; };
struct Vector2 { float x, y; };
bool WorldToScreen(const Vector3& worldPos, Vector2& screenPos, float viewMatrix[16], int screenWidth, int screenHeight) {
// Matrix transformation (Row-major indexing)
float clipW = worldPos.x * viewMatrix[3] + worldPos.y * viewMatrix[7] + worldPos.z * viewMatrix[11] + viewMatrix[15];
// Behind camera check
if (clipW < 0.001f) return false;
float clipX = worldPos.x * viewMatrix[0] + worldPos.y * viewMatrix[4] + worldPos.z * viewMatrix[8] + viewMatrix[12];
float clipY = worldPos.x * viewMatrix[1] + worldPos.y * viewMatrix[5] + worldPos.z * viewMatrix[9] + viewMatrix[13];
// Normalized Device Coordinates (NDC) [-1, 1]
float ndcX = clipX / clipW;
float ndcY = clipY / clipW;
// Map NDC to screen pixel coordinates
screenPos.x = (screenWidth / 2.0f) * (1.0f + ndcX);
screenPos.y = (screenHeight / 2.0f) * (1.0f - ndcY); // Invert Y for screen space
return true;
}Pro-tip: If your ESP is rendering inverted (left is right, or top is bottom), simply check whether the game's matrix is column-major
CPP
viewMatrix[0][i] CPP
viewMatrix[i][0]
MatrixRecon · 3D Mathematics & View Matrix Calculations
The following users thanked MatrixRecon for this post: