I have been working on a game hack for a game called Nexuiz v2.5.2, i have a world to screen script that seems to mainly work except some of the math is wrong in the world to screen and i have tried everything to try to fix it or find the right order but i can't come up with anything nor know how to differentiate forward right and up matrix's, here's the code:
Code: Select all
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
#include <iomanip>
#define PI 3.1415927f
struct Vector2 { float x, y; };
struct Vector3 { float x, y, z; };
struct Vector4 { float x, y, z, w; };
float viewMatrix[16];
DWORD GetProcessId(const char* processName) {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 processEntry;
processEntry.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(snapshot, &processEntry)) {
do {
if (!_stricmp(processEntry.szExeFile, processName)) {
std::cout << "Process found: " << processEntry.szExeFile
<< " (PID: " << processEntry.th32ProcessID << ")" << std::endl;
CloseHandle(snapshot);
return processEntry.th32ProcessID;
}
} while (Process32Next(snapshot, &processEntry));
}
std::cerr << "Process not found: " << processName << std::endl;
CloseHandle(snapshot);
return 0;
}
Vector2 WorldToScreen(const Vector3& playerPos, int screenWidth, int screenHeight) {
float x = playerPos.x * viewMatrix[0] + playerPos.y * viewMatrix[4] + playerPos.z * viewMatrix[8] + viewMatrix[12];
float y = playerPos.x * viewMatrix[1] + playerPos.y * viewMatrix[5] + playerPos.z * viewMatrix[9] + viewMatrix[13];
float z = playerPos.x * viewMatrix[2] + playerPos.y * viewMatrix[6] + playerPos.z * viewMatrix[10] + viewMatrix[14];
float w = playerPos.x * viewMatrix[3] + playerPos.y * viewMatrix[7] + playerPos.z * viewMatrix[11] + viewMatrix[15];
if (z < 0.1f) return { -1, -1 };
x /= z;
y /= z;
float screenX = (1.0f - x) * 0.5f * screenWidth;
float screenY = (1.0f - y) * 0.5f * screenHeight;
return { screenX, screenY };
}
void DrawESP(HDC hdc, Vector3 screenPos) {
HPEN pen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
HGDIOBJ oldPen = SelectObject(hdc, pen);
Rectangle(hdc,
screenPos.x - 25,
screenPos.y - 25,
screenPos.x + 25,
screenPos.y + 25
);
SelectObject(hdc, oldPen);
DeleteObject(pen);
}
int main() {
const char* processName = "nexuiz-sdl.exe";
DWORD processId = GetProcessId(processName);
if (!processId) {
std::cerr << "Failed to get process ID" << std::endl;
return 1;
}
HANDLE processHandle = OpenProcess(PROCESS_VM_READ, FALSE, processId);
if (!processHandle) {
std::cerr << "Failed to open process. Error: " << GetLastError() << std::endl;
return 1;
}
HWND gameWindow = FindWindowA(NULL, "Nexuiz");
if (!gameWindow) {
std::cerr << "Game window not found" << std::endl;
CloseHandle(processHandle);
return 1;
}
HDC gameDC = GetDC(gameWindow);
if (!gameDC) {
std::cerr << "Failed to get DC. Error: " << GetLastError() << std::endl;
CloseHandle(processHandle);
return 1;
}
DWORD viewMatrixAddress = 0xC7CA50;
while (true) {
ReadProcessMemory(processHandle, (LPVOID)0xC6A8F0, &viewMatrix, sizeof(viewMatrix), NULL);
Vector2 ScreenPos = WorldToScreen({ 100,100,100 }, 664, 1176);
std::cout << ScreenPos.x << " : " << ScreenPos.y << "\n";
DrawESP(gameDC, {ScreenPos.x, ScreenPos.y, 0});
Sleep(1);
}
ReleaseDC(gameWindow, gameDC);
CloseHandle(processHandle);
return 0;
}
Any help or explanations as to how to differentiate forward up and right or just a solution is appreciate.
All code should run as the base address is set to 0x400000 (older game)