From b47571e7f1b4348b526c23a6ab6b88dbcb49ecf1 Mon Sep 17 00:00:00 2001 From: Schark Date: Thu, 24 Sep 2026 14:43:21 -0400 Subject: Add random platforms into game world for testing --- platformer/src/world.h | 63 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 14 deletions(-) (limited to 'platformer/src/world.h') diff --git a/platformer/src/world.h b/platformer/src/world.h index af60feb..9da16c1 100644 --- a/platformer/src/world.h +++ b/platformer/src/world.h @@ -2,34 +2,57 @@ * ============================================================ * FILE: world.h * - * PURPOSE: Public interface for "the level". Today the level is a blank, - * flat square of ground, and this API is how other modules ask - * questions about it. + * PURPOSE: Public interface for "the level": a flat square of ground with + * a scatter of box-shaped platforms on it. This API is how other + * modules ask questions about the level's geometry. * - * FITS IN: physics.c asks for the ground height and bounds. main.c asks - * for the spawn point. render.c asks for the extent so it knows - * how big a grid to draw. + * FITS IN: physics.c asks what's under the feet, what's above the head, + * and what box a body overlaps. render.c asks for the extent and + * the boxes so it can draw them, and for the floor under the + * player for the shadow. main.c asks for the spawn point. * - * INTERFACE: Use these exact signatures: + * INTERFACE: Use these exact names: + * + * typedef struct Box { + * Vec3 min, max; axis-aligned corners, meters (min.y = bottom, max.y = top) + * } Box; * * void world_init(void); - * float world_ground_height(float x, float z); - * Height of the ground at (x, z). Returns 0.0f for now. This is - * the one hook where terrain/platforms will plug in later. + * Builds the level. Platforms are placed "randomly" from + * WORLD_SEED, so the same seed always gives the same layout. + * int world_box_count(void); + * Box world_box(int i); 0 <= i < world_box_count() + * + * A "footprint" below is the square [x +- half_width] x [z +- half_width] + * around a body's center. Touching edges don't count as overlapping. + * + * float world_floor_below(Vec3 feet, float half_width); + * The highest surface under the footprint whose top is at or just + * below feet.y (a box top, or the ground at 0). This is what the + * feet land on, and where the shadow goes. + * float world_ceiling_above(Vec3 head, float half_width); + * The lowest box bottom over the footprint that is at or just above + * head.y, or WORLD_NO_CEILING if there's none. + * bool world_find_overlap(Vec3 feet, float half_width, float height, Box *hit); + * True (and *hit = that box) if a body standing at `feet` with this + * size overlaps any box. physics.c uses it to push bodies out of walls. * Vec3 world_clamp_to_bounds(Vec3 p); * Returns p with x and z clamped to [-WORLD_HALF_EXTENT, * +WORLD_HALF_EXTENT]. y is untouched. * Vec3 world_spawn_point(void); - * Where the player starts / resets to: (0, 0, 0) for now. + * Where the player starts / resets to: (0, 0, 0). The area around + * it is kept clear of platforms. * float world_half_extent(void); * Returns WORLD_HALF_EXTENT (so render.c doesn't need config * knowledge of the world's shape). * - * Include "math3d.h". + * #define WORLD_NO_CEILING 1e9f (far above anything) + * + * Include and "math3d.h". * * CONVENTIONS: Include guard: WORLD_H. * - * NOT IN SCOPE: Level files, platforms, walls, slopes, objects. + * NOT IN SCOPE: Level files, slopes, moving platforms, non-box shapes. * * DONE WHEN: physics.c and render.c can be written using only this header. * ============================================================ @@ -37,10 +60,22 @@ #ifndef WORLD_H #define WORLD_H +#include + #include "math3d.h" +#define WORLD_NO_CEILING 1e9f /* returned when nothing is overhead */ + +typedef struct Box { + Vec3 min, max; /* axis-aligned corners, meters (min.y = bottom, max.y = top) */ +} Box; + void world_init(void); -float world_ground_height(float x, float z); +int world_box_count(void); +Box world_box(int i); +float world_floor_below(Vec3 feet, float half_width); +float world_ceiling_above(Vec3 head, float half_width); +bool world_find_overlap(Vec3 feet, float half_width, float height, Box *hit); Vec3 world_clamp_to_bounds(Vec3 p); Vec3 world_spawn_point(void); float world_half_extent(void); -- cgit v1.2.3-18-g5258