78 lines
3.0 KiB
C++
78 lines
3.0 KiB
C++
#pragma once
|
|
|
|
#include <raylib.h>
|
|
#include <raymath.h>
|
|
|
|
class Coords {
|
|
public:
|
|
// Default world bounds (can be overridden)
|
|
static inline float WORLD_BOUNDS_X = 50.0f;
|
|
static inline float WORLD_BOUNDS_Z = 50.0f;
|
|
static inline float WORLD_MIN_HEIGHT = 0.0f;
|
|
static inline float WORLD_MAX_HEIGHT = 100.0f;
|
|
|
|
// Set world bounds from terrain data
|
|
static void setWorldBounds(float halfWidth, float halfHeight, float maxY) {
|
|
WORLD_BOUNDS_X = halfWidth;
|
|
WORLD_BOUNDS_Z = halfHeight;
|
|
WORLD_MAX_HEIGHT = maxY;
|
|
}
|
|
|
|
static void setWorldBounds(const Vector3& bounds) {
|
|
WORLD_BOUNDS_X = bounds.x * 0.5f;
|
|
WORLD_BOUNDS_Z = bounds.z * 0.5f;
|
|
WORLD_MAX_HEIGHT = bounds.y;
|
|
}
|
|
|
|
static Vector3 worldToLocal(const Vector3& worldPos, const Vector3& origin) {
|
|
return Vector3Subtract(worldPos, origin);
|
|
}
|
|
|
|
static Vector3 localToWorld(const Vector3& localPos, const Vector3& origin) {
|
|
return Vector3Add(localPos, origin);
|
|
}
|
|
|
|
static Vector3 clampToWorldBounds(const Vector3& pos) {
|
|
return {
|
|
Clamp(pos.x, -WORLD_BOUNDS_X, WORLD_BOUNDS_X),
|
|
Clamp(pos.y, WORLD_MIN_HEIGHT, WORLD_MAX_HEIGHT),
|
|
Clamp(pos.z, -WORLD_BOUNDS_Z, WORLD_BOUNDS_Z)
|
|
};
|
|
}
|
|
|
|
static bool isInWorldBounds(const Vector3& pos) {
|
|
return pos.x >= -WORLD_BOUNDS_X && pos.x <= WORLD_BOUNDS_X &&
|
|
pos.y >= WORLD_MIN_HEIGHT && pos.y <= WORLD_MAX_HEIGHT &&
|
|
pos.z >= -WORLD_BOUNDS_Z && pos.z <= WORLD_BOUNDS_Z;
|
|
}
|
|
|
|
static Matrix buildTransformMatrix(const Vector3& position, const Vector3& rotation, const Vector3& scale) {
|
|
Matrix matScale = MatrixScale(scale.x, scale.y, scale.z);
|
|
Matrix matRotation = MatrixRotateXYZ(rotation);
|
|
Matrix matTranslation = MatrixTranslate(position.x, position.y, position.z);
|
|
|
|
Matrix result = MatrixMultiply(matScale, matRotation);
|
|
result = MatrixMultiply(result, matTranslation);
|
|
return result;
|
|
}
|
|
|
|
static Vector3 forward() { return {0.0f, 0.0f, 1.0f}; }
|
|
static Vector3 backward() { return {0.0f, 0.0f, -1.0f}; }
|
|
static Vector3 right() { return {1.0f, 0.0f, 0.0f}; }
|
|
static Vector3 left() { return {-1.0f, 0.0f, 0.0f}; }
|
|
static Vector3 up() { return {0.0f, 1.0f, 0.0f}; }
|
|
static Vector3 down() { return {0.0f, -1.0f, 0.0f}; }
|
|
|
|
static void drawDebugAxes(const Vector3& position, float size = 1.0f) {
|
|
DrawLine3D(position, Vector3Add(position, Vector3Scale(right(), size)), RED);
|
|
DrawLine3D(position, Vector3Add(position, Vector3Scale(up(), size)), GREEN);
|
|
DrawLine3D(position, Vector3Add(position, Vector3Scale(forward(), size)), BLUE);
|
|
}
|
|
|
|
static void drawWorldBounds() {
|
|
Color boundsColor = {255, 255, 0, 100};
|
|
DrawCubeWires({0, WORLD_MAX_HEIGHT/2, 0},
|
|
WORLD_BOUNDS_X * 2, WORLD_MAX_HEIGHT, WORLD_BOUNDS_Z * 2,
|
|
boundsColor);
|
|
}
|
|
}; |