summaryrefslogtreecommitdiff
path: root/platformer/src/math3d.h
diff options
context:
space:
mode:
Diffstat (limited to 'platformer/src/math3d.h')
-rw-r--r--platformer/src/math3d.h273
1 files changed, 273 insertions, 0 deletions
diff --git a/platformer/src/math3d.h b/platformer/src/math3d.h
new file mode 100644
index 0000000..e0f89a5
--- /dev/null
+++ b/platformer/src/math3d.h
@@ -0,0 +1,273 @@
+/*
+ * ============================================================
+ * FILE: math3d.h
+ *
+ * PURPOSE: The small amount of vector/matrix math the game needs, written
+ * by hand so we don't pull in a math library.
+ *
+ * FITS IN: Header-only (every function is `static inline`). Included by
+ * any file that does math. Depends only on <math.h>.
+ *
+ * INTERFACE: Use these exact names:
+ *
+ * Constants
+ * M3D_PI 3.14159265358979f (C99 doesn't guarantee M_PI)
+ *
+ * Types (plain structs, passed and returned BY VALUE since they're small)
+ * typedef struct { float x, y; } Vec2;
+ * typedef struct { float x, y, z; } Vec3;
+ * typedef struct { float m[16]; } Mat4; COLUMN-MAJOR, element (row r,
+ * col c) is m[c*4 + r], so it can
+ * go straight to glUniformMatrix4fv
+ * with transpose = GL_FALSE.
+ *
+ * Scalar helpers
+ * float clampf(float v, float lo, float hi);
+ * float approachf(float current, float target, float max_step);
+ * moves current toward target by at most max_step, never overshoots.
+ * (Used for acceleration/friction.)
+ * float wrap_angle(float a);
+ * returns a in (-PI, PI].
+ * float approach_angle(float current, float target, float max_step);
+ * like approachf but takes the SHORT way around the circle.
+ * (Used for turning the player.)
+ *
+ * Vec2
+ * Vec2 vec2_make(float x, float y);
+ * float vec2_length(Vec2 v);
+ *
+ * Vec3
+ * Vec3 vec3_make(float x, float y, float z);
+ * Vec3 vec3_add(Vec3 a, Vec3 b);
+ * Vec3 vec3_sub(Vec3 a, Vec3 b);
+ * Vec3 vec3_scale(Vec3 v, float s);
+ * float vec3_dot(Vec3 a, Vec3 b);
+ * Vec3 vec3_cross(Vec3 a, Vec3 b);
+ * float vec3_length(Vec3 v);
+ * Vec3 vec3_normalize(Vec3 v); returns (0,0,0) for zero-length input
+ * Vec3 vec3_lerp(Vec3 a, Vec3 b, float t);
+ *
+ * Mat4
+ * Mat4 mat4_identity(void);
+ * Mat4 mat4_mul(Mat4 a, Mat4 b); result = a * b (b is applied first)
+ * Mat4 mat4_translate(Vec3 t);
+ * Mat4 mat4_scale(Vec3 s);
+ * Mat4 mat4_rotate_y(float angle); rotates +Z toward +X, i.e.
+ * (0,0,1) -> (sin a, 0, cos a)
+ * Mat4 mat4_perspective(float fovy_rad, float aspect, float near, float far);
+ * standard OpenGL (gluPerspective) form
+ * Mat4 mat4_look_at(Vec3 eye, Vec3 target, Vec3 up);
+ * standard OpenGL (gluLookAt) form
+ *
+ * CONVENTIONS: Right-handed, Y up. Radians everywhere. Keep each function a
+ * few lines with a short comment; this is a learning resource.
+ * Include guard: MATH3D_H.
+ *
+ * NOT IN SCOPE: Quaternions, matrix inverse, SIMD, Vec4, generic N-dim math.
+ * Add a function only when a caller needs it.
+ *
+ * DONE WHEN: All functions above compile cleanly under -std=c99 -Wall
+ * -Wextra, and mat4_perspective * mat4_look_at produces a
+ * correct view of the ground grid in render.c.
+ * ============================================================
+ */
+#ifndef MATH3D_H
+#define MATH3D_H
+
+#include <math.h>
+
+#define M3D_PI 3.14159265358979f /* C99 doesn't guarantee M_PI */
+
+typedef struct { float x, y; } Vec2;
+typedef struct { float x, y, z; } Vec3;
+typedef struct { float m[16]; } Mat4; /* column-major: (row r, col c) = m[c*4 + r] */
+
+/* ---- Scalar helpers ---------------------------------------------------- */
+
+static inline float clampf(float v, float lo, float hi)
+{
+ if (v < lo) return lo;
+ if (v > hi) return hi;
+ return v;
+}
+
+/* Moves current toward target by at most max_step, never overshooting.
+ * This is how speeds accelerate and brake at a fixed rate per tick. */
+static inline float approachf(float current, float target, float max_step)
+{
+ if (current < target) {
+ current += max_step;
+ return current > target ? target : current;
+ }
+ current -= max_step;
+ return current < target ? target : current;
+}
+
+/* Returns a in (-PI, PI]. */
+static inline float wrap_angle(float a)
+{
+ a = fmodf(a + M3D_PI, 2.0f * M3D_PI); /* now in (-2PI, 2PI) */
+ if (a <= 0.0f) a += 2.0f * M3D_PI; /* now in (0, 2PI] */
+ return a - M3D_PI;
+}
+
+/* Like approachf, but for angles: turns the SHORT way around the circle,
+ * so facing 170 deg and turning to -170 deg is a 20 deg turn, not 340. */
+static inline float approach_angle(float current, float target, float max_step)
+{
+ float diff = wrap_angle(target - current);
+ return wrap_angle(current + clampf(diff, -max_step, max_step));
+}
+
+/* ---- Vec2 -------------------------------------------------------------- */
+
+static inline Vec2 vec2_make(float x, float y)
+{
+ Vec2 v = { x, y };
+ return v;
+}
+
+static inline float vec2_length(Vec2 v)
+{
+ return sqrtf(v.x * v.x + v.y * v.y);
+}
+
+/* ---- Vec3 -------------------------------------------------------------- */
+
+static inline Vec3 vec3_make(float x, float y, float z)
+{
+ Vec3 v = { x, y, z };
+ return v;
+}
+
+static inline Vec3 vec3_add(Vec3 a, Vec3 b)
+{
+ return vec3_make(a.x + b.x, a.y + b.y, a.z + b.z);
+}
+
+static inline Vec3 vec3_sub(Vec3 a, Vec3 b)
+{
+ return vec3_make(a.x - b.x, a.y - b.y, a.z - b.z);
+}
+
+static inline Vec3 vec3_scale(Vec3 v, float s)
+{
+ return vec3_make(v.x * s, v.y * s, v.z * s);
+}
+
+static inline float vec3_dot(Vec3 a, Vec3 b)
+{
+ return a.x * b.x + a.y * b.y + a.z * b.z;
+}
+
+static inline Vec3 vec3_cross(Vec3 a, Vec3 b)
+{
+ return vec3_make(a.y * b.z - a.z * b.y,
+ a.z * b.x - a.x * b.z,
+ a.x * b.y - a.y * b.x);
+}
+
+static inline float vec3_length(Vec3 v)
+{
+ return sqrtf(vec3_dot(v, v));
+}
+
+/* Returns (0,0,0) for a zero-length input instead of dividing by zero. */
+static inline Vec3 vec3_normalize(Vec3 v)
+{
+ float len = vec3_length(v);
+ if (len <= 0.0f) return vec3_make(0.0f, 0.0f, 0.0f);
+ return vec3_scale(v, 1.0f / len);
+}
+
+/* t = 0 gives a, t = 1 gives b. */
+static inline Vec3 vec3_lerp(Vec3 a, Vec3 b, float t)
+{
+ return vec3_add(a, vec3_scale(vec3_sub(b, a), t));
+}
+
+/* ---- Mat4 -------------------------------------------------------------- */
+
+static inline Mat4 mat4_identity(void)
+{
+ Mat4 r = {{ 0 }};
+ r.m[0] = r.m[5] = r.m[10] = r.m[15] = 1.0f;
+ return r;
+}
+
+/* result = a * b, so b is applied to a point first, then a. */
+static inline Mat4 mat4_mul(Mat4 a, Mat4 b)
+{
+ Mat4 r;
+ for (int col = 0; col < 4; col++) {
+ for (int row = 0; row < 4; row++) {
+ float sum = 0.0f;
+ for (int k = 0; k < 4; k++)
+ sum += a.m[k * 4 + row] * b.m[col * 4 + k];
+ r.m[col * 4 + row] = sum;
+ }
+ }
+ return r;
+}
+
+/* The translation lives in the last column. */
+static inline Mat4 mat4_translate(Vec3 t)
+{
+ Mat4 r = mat4_identity();
+ r.m[12] = t.x;
+ r.m[13] = t.y;
+ r.m[14] = t.z;
+ return r;
+}
+
+static inline Mat4 mat4_scale(Vec3 s)
+{
+ Mat4 r = mat4_identity();
+ r.m[0] = s.x;
+ r.m[5] = s.y;
+ r.m[10] = s.z;
+ return r;
+}
+
+/* Rotates around +Y, turning +Z toward +X: (0,0,1) -> (sin a, 0, cos a).
+ * This matches the player's facing vector (sin(yaw), 0, cos(yaw)). */
+static inline Mat4 mat4_rotate_y(float angle)
+{
+ float c = cosf(angle), s = sinf(angle);
+ Mat4 r = mat4_identity();
+ r.m[0] = c; /* row 0, col 0 */
+ r.m[8] = s; /* row 0, col 2 */
+ r.m[2] = -s; /* row 2, col 0 */
+ r.m[10] = c; /* row 2, col 2 */
+ return r;
+}
+
+/* Standard OpenGL (gluPerspective) projection. Maps the view frustum to
+ * clip space, with the camera looking down -Z. */
+static inline Mat4 mat4_perspective(float fovy_rad, float aspect, float near, float far)
+{
+ float f = 1.0f / tanf(fovy_rad * 0.5f);
+ Mat4 r = {{ 0 }};
+ r.m[0] = f / aspect;
+ r.m[5] = f;
+ r.m[10] = (far + near) / (near - far);
+ r.m[11] = -1.0f; /* row 3, col 2: w = -z */
+ r.m[14] = 2.0f * far * near / (near - far);
+ return r;
+}
+
+/* Standard OpenGL (gluLookAt) view matrix: moves the world so the eye sits
+ * at the origin looking down -Z, with `up` roughly +Y on screen. */
+static inline Mat4 mat4_look_at(Vec3 eye, Vec3 target, Vec3 up)
+{
+ Vec3 f = vec3_normalize(vec3_sub(target, eye)); /* forward */
+ Vec3 s = vec3_normalize(vec3_cross(f, up)); /* right */
+ Vec3 u = vec3_cross(s, f); /* true up */
+ Mat4 r = mat4_identity();
+ r.m[0] = s.x; r.m[4] = s.y; r.m[8] = s.z; r.m[12] = -vec3_dot(s, eye);
+ r.m[1] = u.x; r.m[5] = u.y; r.m[9] = u.z; r.m[13] = -vec3_dot(u, eye);
+ r.m[2] = -f.x; r.m[6] = -f.y; r.m[10] = -f.z; r.m[14] = vec3_dot(f, eye);
+ return r;
+}
+
+#endif /* MATH3D_H */