Unify coordinate systems with canonical WoW world coordinates

Centralizes all coordinate conversions in core/coordinates.hpp with
proper canonical WoW coords (+X=North, +Y=West, +Z=Up). Fixes critical
tile calculation bug that was loading wrong surrounding tiles during
terrain streaming, and fixes position sync sending ADT-raw format
instead of canonical coordinates to the server.
This commit is contained in:
Kelsi 2026-02-04 17:37:28 -08:00
parent ee9efa3478
commit 6690910712
11 changed files with 367 additions and 93 deletions

View file

@ -9,6 +9,7 @@
#include <glm/glm.hpp>
#include <imgui.h>
#include <cmath>
#include <limits>
namespace wowee {
namespace rendering {
@ -1010,32 +1011,137 @@ void CameraController::reset() {
glm::vec3 spawnPos = defaultPosition;
// Snap spawn to a nearby valid floor, but reject outliers so we don't
// respawn under the city when collision data is noisy at this location.
std::optional<float> terrainH;
std::optional<float> wmoH;
std::optional<float> m2H;
if (terrainManager) {
terrainH = terrainManager->getHeightAt(spawnPos.x, spawnPos.y);
}
float floorProbeZ = terrainH.value_or(spawnPos.z);
if (wmoRenderer) {
wmoH = wmoRenderer->getFloorHeight(spawnPos.x, spawnPos.y, floorProbeZ + 2.0f);
}
if (m2Renderer) {
m2H = m2Renderer->getFloorHeight(spawnPos.x, spawnPos.y, floorProbeZ + 2.0f);
}
auto evalFloorAt = [&](float x, float y, float refZ) -> std::optional<float> {
std::optional<float> terrainH;
std::optional<float> wmoH;
std::optional<float> m2H;
if (terrainManager) {
terrainH = terrainManager->getHeightAt(x, y);
}
float floorProbeZ = terrainH.value_or(refZ);
if (wmoRenderer) {
wmoH = wmoRenderer->getFloorHeight(x, y, floorProbeZ + 2.0f);
}
if (m2Renderer) {
m2H = m2Renderer->getFloorHeight(x, y, floorProbeZ + 2.0f);
}
auto h = selectReachableFloor(terrainH, wmoH, refZ, 16.0f);
if (!h) {
h = selectHighestFloor(terrainH, wmoH, m2H);
}
return h;
};
std::optional<float> h = selectReachableFloor(terrainH, wmoH, spawnPos.z, 16.0f);
if (!h) {
h = selectHighestFloor(terrainH, wmoH, m2H);
// Search nearby for a stable, non-steep spawn floor to avoid waterfall/ledge spawns.
float bestScore = std::numeric_limits<float>::max();
glm::vec3 bestPos = spawnPos;
bool foundBest = false;
constexpr float radii[] = {0.0f, 6.0f, 12.0f, 18.0f, 24.0f, 32.0f};
constexpr int ANGLES = 16;
constexpr float PI = 3.14159265f;
for (float r : radii) {
int steps = (r <= 0.01f) ? 1 : ANGLES;
for (int i = 0; i < steps; i++) {
float a = (2.0f * PI * static_cast<float>(i)) / static_cast<float>(steps);
float x = defaultPosition.x + r * std::cos(a);
float y = defaultPosition.y + r * std::sin(a);
auto h = evalFloorAt(x, y, defaultPosition.z);
if (!h) continue;
// Allow large downward snaps, but avoid snapping onto high roofs/odd geometry.
constexpr float MAX_SPAWN_SNAP_UP = 16.0f;
if (*h > defaultPosition.z + MAX_SPAWN_SNAP_UP) continue;
float score = r * 0.02f;
if (terrainManager) {
// Penalize steep/unstable spots.
int slopeSamples = 0;
float slopeAccum = 0.0f;
constexpr float off = 2.5f;
const float dx[4] = {off, -off, 0.0f, 0.0f};
const float dy[4] = {0.0f, 0.0f, off, -off};
for (int s = 0; s < 4; s++) {
auto hn = terrainManager->getHeightAt(x + dx[s], y + dy[s]);
if (!hn) continue;
slopeAccum += std::abs(*hn - *h);
slopeSamples++;
}
if (slopeSamples > 0) {
score += (slopeAccum / static_cast<float>(slopeSamples)) * 2.0f;
}
}
if (waterRenderer) {
auto wh = waterRenderer->getWaterHeightAt(x, y);
if (wh && *h < *wh - 0.2f) {
score += 8.0f;
}
}
if (wmoRenderer) {
const glm::vec3 from(x, y, *h + 0.20f);
const bool insideWMO = wmoRenderer->isInsideWMO(x, y, *h + 1.5f, nullptr);
// Prefer outdoors for default hearth-like spawn points.
if (insideWMO) {
score += 120.0f;
}
// Reject points embedded in nearby walls by probing tiny cardinal moves.
int wallHits = 0;
constexpr float probeStep = 0.85f;
const glm::vec3 probes[4] = {
glm::vec3(x + probeStep, y, *h + 0.20f),
glm::vec3(x - probeStep, y, *h + 0.20f),
glm::vec3(x, y + probeStep, *h + 0.20f),
glm::vec3(x, y - probeStep, *h + 0.20f),
};
for (const auto& to : probes) {
glm::vec3 adjusted;
if (wmoRenderer->checkWallCollision(from, to, adjusted)) {
wallHits++;
}
}
if (wallHits >= 2) {
continue; // Likely wedged in geometry.
}
if (wallHits == 1) {
score += 30.0f;
}
// If the point is inside a WMO, ensure there is an easy escape path.
// If almost all directions are blocked, treat it as invalid spawn.
if (insideWMO) {
int blocked = 0;
constexpr int radialChecks = 12;
constexpr float radialDist = 2.2f;
for (int ri = 0; ri < radialChecks; ri++) {
float ang = (2.0f * PI * static_cast<float>(ri)) / static_cast<float>(radialChecks);
glm::vec3 to(
x + std::cos(ang) * radialDist,
y + std::sin(ang) * radialDist,
*h + 0.20f
);
glm::vec3 adjusted;
if (wmoRenderer->checkWallCollision(from, to, adjusted)) {
blocked++;
}
}
if (blocked >= 9) {
continue; // Enclosed by interior/wall geometry.
}
score += static_cast<float>(blocked) * 3.0f;
}
}
if (score < bestScore) {
bestScore = score;
bestPos = glm::vec3(x, y, *h + 0.05f);
foundBest = true;
}
}
}
// Allow large downward snaps (prevents sky-fall spawns), but don't snap up
// onto distant roofs when a bad hit appears above us.
constexpr float MAX_SPAWN_SNAP_UP = 16.0f;
if (h && *h <= spawnPos.z + MAX_SPAWN_SNAP_UP) {
lastGroundZ = *h;
spawnPos.z = *h + 0.05f;
if (foundBest) {
spawnPos = bestPos;
lastGroundZ = spawnPos.z - 0.05f;
}
camera->setRotation(yaw, pitch);
@ -1055,7 +1161,7 @@ void CameraController::reset() {
camera->setPosition(camPos);
} else {
// Free-fly mode keeps camera eye-height above ground.
if (h) {
if (foundBest) {
spawnPos.z += eyeHeight;
}
smoothedCamPos = spawnPos;

View file

@ -137,11 +137,12 @@ void Minimap::shutdown() {
quadShader.reset();
}
void Minimap::render(const Camera& playerCamera, int screenWidth, int screenHeight) {
void Minimap::render(const Camera& playerCamera, const glm::vec3& centerWorldPos,
int screenWidth, int screenHeight) {
if (!enabled || !terrainRenderer || !fbo) return;
const auto now = std::chrono::steady_clock::now();
glm::vec3 playerPos = playerCamera.getPosition();
glm::vec3 playerPos = centerWorldPos;
bool needsRefresh = !hasCachedFrame;
if (!needsRefresh) {
float moved = glm::length(glm::vec2(playerPos.x - lastUpdatePos.x, playerPos.y - lastUpdatePos.y));
@ -151,7 +152,7 @@ void Minimap::render(const Camera& playerCamera, int screenWidth, int screenHeig
// 1. Render terrain from top-down into FBO (throttled)
if (needsRefresh) {
renderTerrainToFBO(playerCamera);
renderTerrainToFBO(playerCamera, centerWorldPos);
lastUpdateTime = now;
lastUpdatePos = playerPos;
hasCachedFrame = true;
@ -161,7 +162,7 @@ void Minimap::render(const Camera& playerCamera, int screenWidth, int screenHeig
renderQuad(screenWidth, screenHeight);
}
void Minimap::renderTerrainToFBO(const Camera& playerCamera) {
void Minimap::renderTerrainToFBO(const Camera& /*playerCamera*/, const glm::vec3& centerWorldPos) {
// Save current viewport
GLint prevViewport[4];
glGetIntegerv(GL_VIEWPORT, prevViewport);
@ -173,7 +174,7 @@ void Minimap::renderTerrainToFBO(const Camera& playerCamera) {
// Create a top-down camera at the player's XY position
Camera topDownCamera;
glm::vec3 playerPos = playerCamera.getPosition();
glm::vec3 playerPos = centerWorldPos;
topDownCamera.setPosition(glm::vec3(playerPos.x, playerPos.y, playerPos.z + 5000.0f));
topDownCamera.setRotation(0.0f, -89.9f); // Look straight down
topDownCamera.setAspectRatio(1.0f);

View file

@ -1066,7 +1066,11 @@ void Renderer::renderWorld(game::World* world) {
// Render minimap overlay
if (minimap && camera && window) {
minimap->render(*camera, window->getWidth(), window->getHeight());
glm::vec3 minimapCenter = camera->getPosition();
if (cameraController && cameraController->isThirdPerson()) {
minimapCenter = characterPosition;
}
minimap->render(*camera, minimapCenter, window->getWidth(), window->getHeight());
}
// --- Resolve MSAA → non-MSAA texture ---

View file

@ -4,6 +4,7 @@
#include "rendering/m2_renderer.hpp"
#include "rendering/wmo_renderer.hpp"
#include "rendering/camera.hpp"
#include "core/coordinates.hpp"
#include "pipeline/asset_manager.hpp"
#include "pipeline/adt_loader.hpp"
#include "pipeline/m2_loader.hpp"
@ -147,11 +148,9 @@ void TerrainManager::update(const Camera& camera, float deltaTime) {
timeSinceLastUpdate = 0.0f;
// Get current tile from camera position
// GL coordinate mapping: GL Y = -(wowX - ZEROPOINT), GL X = -(wowZ - ZEROPOINT), GL Z = height
// worldToTile expects: worldX = -glY (maps to tileX), worldY = glX (maps to tileY)
// Get current tile from camera position.
glm::vec3 camPos = camera.getPosition();
TileCoord newTile = worldToTile(-camPos.y, camPos.x);
TileCoord newTile = worldToTile(camPos.x, camPos.y);
// Check if we've moved to a different tile
if (newTile.x != currentTile.x || newTile.y != currentTile.y) {
@ -293,20 +292,15 @@ std::unique_ptr<PendingTile> TerrainManager::prepareTile(int x, int y) {
// Store placement data for instance creation on main thread
if (preparedModelIds.count(modelId)) {
const float ZEROPOINT = 32.0f * 533.33333f;
float wowX = placement.position[0];
float wowY = placement.position[1];
float wowZ = placement.position[2];
glm::vec3 glPos = core::coords::adtToWorld(wowX, wowY, wowZ);
PendingTile::M2Placement p;
p.modelId = modelId;
p.uniqueId = placement.uniqueId;
p.position = glm::vec3(
-(wowZ - ZEROPOINT),
-(wowX - ZEROPOINT),
wowY
);
p.position = glPos;
p.rotation = glm::vec3(
-placement.rotation[2] * 3.14159f / 180.0f,
-placement.rotation[0] * 3.14159f / 180.0f,
@ -368,13 +362,9 @@ std::unique_ptr<PendingTile> TerrainManager::prepareTile(int x, int y) {
}
if (!wmoModel.groups.empty()) {
const float ZEROPOINT = 32.0f * 533.33333f;
glm::vec3 pos(
-(placement.position[2] - ZEROPOINT),
-(placement.position[0] - ZEROPOINT),
placement.position[1]
);
glm::vec3 pos = core::coords::adtToWorld(placement.position[0],
placement.position[1],
placement.position[2]);
glm::vec3 rot(
-placement.rotation[2] * 3.14159f / 180.0f,
@ -769,19 +759,8 @@ void TerrainManager::unloadAll() {
}
}
TileCoord TerrainManager::worldToTile(float worldX, float worldY) const {
// WoW world coordinate system:
// - Tiles are 8533.33 units wide (TILE_SIZE)
// - Tile (32, 32) is roughly at world origin for continents
// - Coordinates increase going east (X) and south (Y)
int tileX = 32 + static_cast<int>(std::floor(worldX / TILE_SIZE));
int tileY = 32 - static_cast<int>(std::floor(worldY / TILE_SIZE));
// Clamp to valid range (0-63)
tileX = std::max(0, std::min(63, tileX));
tileY = std::max(0, std::min(63, tileY));
TileCoord TerrainManager::worldToTile(float glX, float glY) const {
auto [tileX, tileY] = core::coords::worldToTile(glX, glY);
return {tileX, tileY};
}