Developer knowledge network · moderated exchange

UnreliableCode қауымдастығы

Әзірлеушілерді зерттеу, кері инженерия және кодтау қауымдастығы

Knowledge indexТірі
4Categories
919Threads
2.8KЖазбалар
Guide

CS2 World-to-Screen matrix reading & 3D view projection in Source 2

matrix_math_guy
Math Specialist
MEMBER
Өкіл: 105
Қосылу күні: Aug 2018
Хабарламалар: 52
Рахмет: 41
1 ай бұрын · Jun 27, 2026 3:09 PM
#1

In Source 2, the ViewMatrix is a 4x4 row-major floating point matrix located at client.dll + dwViewMatrix.

Here is the updated W2S function with behind-camera projection check:

CPP
bool WorldToScreen(const Vector3& world, Vector2& screen, const float matrix[4][4], int width, int height) {
    float w = matrix[3][0] * world.x + matrix[3][1] * world.y + matrix[3][2] * world.z + matrix[3][3];
    if (w < 0.001f) return false; // Target is behind camera view plane

    float x = matrix[0][0] * world.x + matrix[0][1] * world.y + matrix[0][2] * world.z + matrix[0][3];
    float y = matrix[1][0] * world.x + matrix[1][1] * world.y + matrix[1][2] * world.z + matrix[1][3];

    float invW = 1.0f / w;
    screen.x = (width / 2.0f) + (0.5f * x * invW * width + 0.5f);
    screen.y = (height / 2.0f) - (0.5f * y * invW * height + 0.5f);
    return true;
}
vtable_slayer
Senior Reverser
MEMBER
Өкіл: 215
Қосылу күні: Mar 2018
Хабарламалар: 86
Рахмет: 61
1 ай бұрын · Jun 27, 2026 9:57 PM
#2

Make sure you read the full 64 bytes (sizeof(float) * 16) in a single read call to avoid tearing across render frames.

imgui_artisan
UI Designer
MEMBER
Өкіл: 202
Қосылу күні: Apr 2019
Хабарламалар: 54
Рахмет: 28
1 ай бұрын · Jun 28, 2026 8:41 PM
#3

Tested at 1440p resolution. Bounding boxes align pixel-perfect over head bones.