diff options
| author | Schark <jordan@schark.online> | 2026-09-29 22:28:37 -0400 |
|---|---|---|
| committer | Schark <jordan@schark.online> | 2026-09-29 22:28:37 -0400 |
| commit | fdc8c951fc72d9dc4c8faa58a21b2b9de0599698 (patch) | |
| tree | edd9934a43429508acb9ec37da71eb172dc788e9 /platformer/src | |
| parent | adb6a882870464490a8026aed1f66b21b0e72477 (diff) | |
| download | gamedev-fdc8c951fc72d9dc4c8faa58a21b2b9de0599698.tar.gz gamedev-fdc8c951fc72d9dc4c8faa58a21b2b9de0599698.zip | |
Implemented no-panning zones
Diffstat (limited to 'platformer/src')
| -rw-r--r-- | platformer/src/camera.c | 19 | ||||
| -rw-r--r-- | platformer/src/camera.h | 9 | ||||
| -rw-r--r-- | platformer/src/level.c | 25 | ||||
| -rw-r--r-- | platformer/src/level.h | 6 | ||||
| -rw-r--r-- | platformer/src/main.c | 20 | ||||
| -rw-r--r-- | platformer/src/render.c | 84 | ||||
| -rw-r--r-- | platformer/src/world.c | 31 | ||||
| -rw-r--r-- | platformer/src/world.h | 27 |
8 files changed, 202 insertions, 19 deletions
diff --git a/platformer/src/camera.c b/platformer/src/camera.c index 4511d47..7663003 100644 --- a/platformer/src/camera.c +++ b/platformer/src/camera.c @@ -73,7 +73,7 @@ * countdown and stops the tilt at once, and a pitch you set by hand is * respected until the countdown runs out again. * - * - camera_update(c, in, follow_point, follow_facing, aspect, dt): + * - camera_update(c, in, follow_point, follow_facing, pan_locked, aspect, dt): * 1. Movement check: the subject counts as "moving" if it changed since * last tick (this includes slowing to a stop and jumping in place). * On recenter_pressed, start a recenter (see above). @@ -83,9 +83,11 @@ * so the player stays at the same screen spot while orbiting. While * recentering, apply the same yaw change to recenter_yaw, then ease yaw * toward recenter_yaw (the short way around) at CAMERA_RECENTER_RATE. - * Otherwise, while moving, apply the back-quarter swing: approach its - * goal yaw at CAMERA_SWING_SPEED * |lateral share| rad/s (constant rate, - * so the swing is slow and even). Then apply the auto-pitch (see + * Otherwise, while moving and NOT pan_locked, apply the back-quarter + * swing: approach its goal yaw at CAMERA_SWING_SPEED * |lateral share| + * rad/s (constant rate, so the swing is slow and even). In a + * locked-pan zone the camera never turns behind the player on its + * own; everything else below still applies. Then apply the auto-pitch (see * above). Framing is kept through all of these. * 3. Hold still (except while recentering): the camera does NOT follow * vertically or in depth by default. Project how far the subject moved @@ -395,7 +397,7 @@ void camera_init(Camera *c, Vec3 follow_point) } void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_facing, - float aspect, float dt) + bool pan_locked, float aspect, float dt) { /* 1. Did the player move since last tick? And start a recenter glide * toward the shot from behind the player (opposite their facing). */ @@ -410,8 +412,9 @@ void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_f /* 2. Orbit around the player. Mid-recenter, the stick turns the glide's * goal along with the view, so the two never fight. Otherwise a - * sideways run slowly swings the camera toward the back quarter. After - * a while hands-off, the pitch also tilts to its preferred angle. */ + * sideways run slowly swings the camera toward the back quarter, + * except in a locked-pan zone. After a while hands-off, the pitch also + * tilts to its preferred angle (in a locked-pan zone too). */ float yaw_before = c->yaw; orbit(c, in, dt); float lateral = 0.0f; /* sideways effects are off during a recenter glide */ @@ -420,7 +423,7 @@ void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_f if (c->recentering) { c->recenter_yaw = wrap_angle(c->recenter_yaw + wrap_angle(c->yaw - yaw_before)); c->yaw = ease_toward_angle(c->yaw, c->recenter_yaw, CAMERA_RECENTER_RATE, dt); - } else { + } else if (!pan_locked) { swing_toward_back_quarter(c, moved_by, lateral, dt); } auto_pitch(c, in, dt); diff --git a/platformer/src/camera.h b/platformer/src/camera.h index 60134ed..7dc2cc1 100644 --- a/platformer/src/camera.h +++ b/platformer/src/camera.h @@ -36,10 +36,13 @@ * * void camera_init(Camera *c, Vec3 follow_point); * void camera_update(Camera *c, const Input *in, Vec3 follow_point, - * float follow_facing, float aspect, float dt); + * float follow_facing, bool pan_locked, float aspect, float dt); * follow_point = the player's feet position; the camera adds * CAMERA_TARGET_HEIGHT itself. follow_facing = the player's - * facing_yaw, used by recenter to swing behind them. aspect = + * facing_yaw, used by recenter to swing behind them. pan_locked = + * the player is in a locked-pan zone (main.c asks the world): the + * camera doesn't swing toward the back quarter on its own (the + * stick, recenter, framing and auto-tilt all still work). aspect = * framebuffer width/height, needed because the framing zone is * defined in screen space. * float camera_yaw(const Camera *c); @@ -89,7 +92,7 @@ typedef struct Camera { void camera_init(Camera *c, Vec3 follow_point); void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_facing, - float aspect, float dt); + bool pan_locked, float aspect, float dt); float camera_yaw(const Camera *c); Mat4 camera_view(const Camera *c); Mat4 camera_projection(const Camera *c, float aspect); diff --git a/platformer/src/level.c b/platformer/src/level.c index 12a0862..3ae45be 100644 --- a/platformer/src/level.c +++ b/platformer/src/level.c @@ -89,6 +89,11 @@ * choice ("how do I get up there?") instead of one correct jump after * another, and the route makes sure a way up always exists. * + * - level_build_zones(): TEST_ZONES, hand-placed. One locked-pan zone covers the + * whole cliff and its climb band (south and east faces, out to where the + * climb platforms reach), from the ground up to exactly CLIFF_TOP, so the + * camera stops swinging behind the player while they climb and swings + * again once they stand on the summit. * - level_motion_offset(m, time): see level.h. * - level_spawn_point(): (0, 0, 0). * @@ -895,6 +900,18 @@ static void add_crossing(void) } } +/* ---- Zones ------------------------------------------------------------- */ + +/* Non-solid regions (see world.h). Bounds: min corner, max corner; the Box's + * wall-hang and style fields mean nothing for a zone. */ +static const Zone TEST_ZONES[] = { + /* One box round the whole cliff: from the climb's start west of the south + * face, out ~20 m past both faces, to past the east face's north end, up + * to exactly the top. It also takes in some of the river, which is fine. */ + { { { -42.0f, 0.0f, 21.0f }, { 40.0f, CLIFF_TOP, 94.0f }, false, BOX_STYLE_PLATFORM }, ZONE_LOCKED_PAN }, +}; +#define TEST_ZONE_COUNT ((int)(sizeof TEST_ZONES / sizeof TEST_ZONES[0])) + /* ---- Public interface -------------------------------------------------- */ int level_build(LevelBox *out, int capacity) @@ -918,6 +935,14 @@ int level_build(LevelBox *out, int capacity) return s_count; } +int level_build_zones(Zone *out, int capacity) +{ + int count = 0; + for (int i = 0; i < TEST_ZONE_COUNT && count < capacity; i++) + out[count++] = TEST_ZONES[i]; + return count; +} + Vec3 level_motion_offset(BoxMotion m, float time) { float angle = 2.0f * M3D_PI * (time / m.period + m.phase); diff --git a/platformer/src/level.h b/platformer/src/level.h index ca706a1..b66414e 100644 --- a/platformer/src/level.h +++ b/platformer/src/level.h @@ -33,9 +33,13 @@ * box is where the box sits at offset 0. * * #define LEVEL_MAX_BOXES 256 + * #define LEVEL_MAX_ZONES 16 * int level_build(LevelBox *out, int capacity); * Fills out[] with every box in the level and returns how many. * The same seeds always build the same level. + * int level_build_zones(Zone *out, int capacity); + * Fills out[] with the level's zones (non-solid regions such as + * locked-pan zones; see world.h) and returns how many. * Vec3 level_motion_offset(BoxMotion m, float time); * How far a moving box is from its built position at `time`. * Vec3 level_spawn_point(void); @@ -74,8 +78,10 @@ typedef struct LevelBox { } LevelBox; #define LEVEL_MAX_BOXES 256 +#define LEVEL_MAX_ZONES 16 int level_build(LevelBox *out, int capacity); +int level_build_zones(Zone *out, int capacity); Vec3 level_motion_offset(BoxMotion m, float time); Vec3 level_spawn_point(void); diff --git a/platformer/src/main.c b/platformer/src/main.c index 23f06d2..8e1d139 100644 --- a/platformer/src/main.c +++ b/platformer/src/main.c @@ -38,8 +38,9 @@ * player_reset(&player, world_spawn_point()); * camera_init(&camera, player.body.pos); // see WHY below * player_update(&player, &input, camera_yaw(&camera), TICK_DT); + * pan_locked = world_in_zone(player's body center, ZONE_LOCKED_PAN); * camera_update(&camera, &input, player.body.pos, player.facing_yaw, - * window_aspect(), TICK_DT); + * pan_locked, window_aspect(), TICK_DT); * accumulator -= TICK_DT; * * render_frame(&camera, &player, window_aspect(), fps); @@ -98,17 +99,18 @@ /* Our "HUD" until text rendering exists: live state in the title bar. * Updating a few times per second keeps it readable (and cheap). */ -static void update_debug_title(const Player *player, const Input *input) +static void update_debug_title(const Player *player, const Input *input, bool pan_locked) { - char title[128]; - snprintf(title, sizeof title, "%s | %s | speed %.2f m/s | y %.2f | %s%s%s", + char title[160]; + snprintf(title, sizeof title, "%s | %s | speed %.2f m/s | y %.2f | %s%s%s%s", WINDOW_TITLE, player_state_name(player->state), player->forward_speed, player->body.pos.y, input->using_gamepad ? "pad" : "keyboard", player->heightened_movement ? " | heightened" : "", - player->retreating_movement ? " | retreating" : ""); + player->retreating_movement ? " | retreating" : "", + pan_locked ? " | pan locked" : ""); window_set_title(title); } @@ -138,6 +140,7 @@ int main(void) * frame, which is exactly what you need when tuning movement. SM64 ran its * logic at 30 Hz; we use TICK_HZ. */ Input input = { 0 }; + bool pan_locked = false; /* the player is in a locked-pan zone (for the camera and the title) */ double previous = window_time(); double accumulator = 0.0; double last_title_time = 0.0; @@ -172,8 +175,11 @@ int main(void) /* Player before camera, so the camera follows where the player IS, * not where it was a tick ago. SM64 updates Mario before the camera too. */ player_update(&player, &input, camera_yaw(&camera), TICK_DT); + /* In a locked-pan zone the camera doesn't swing behind the player. */ + Vec3 body_center = vec3_add(player.body.pos, vec3_make(0.0f, 0.5f * PLAYER_HEIGHT, 0.0f)); + pan_locked = world_in_zone(body_center, ZONE_LOCKED_PAN); camera_update(&camera, &input, player.body.pos, player.facing_yaw, - window_aspect(), TICK_DT); + pan_locked, window_aspect(), TICK_DT); accumulator -= TICK_DT; } @@ -190,7 +196,7 @@ int main(void) } if (now - last_title_time >= TITLE_UPDATE_INTERVAL) { - update_debug_title(&player, &input); + update_debug_title(&player, &input, pan_locked); last_title_time = now; } } diff --git a/platformer/src/render.c b/platformer/src/render.c index 782c653..65237f5 100644 --- a/platformer/src/render.c +++ b/platformer/src/render.c @@ -65,6 +65,11 @@ * Bright color (e.g. red). Also draw a small second box as a "nose" on * the front face (+Z in model space) so the facing direction is visible. * That makes the turn rate tunable by eye. + * 7b. Zones (world_zone): no fill at all (a player sees straight through), + * just short black dashes spaced sparsely over the sides and top + * (not the bottom, which would z-fight the ground), so a test zone's + * extent is visible without hiding anything. Built once in + * render_init into one mesh; zones don't move. * 8. FPS counter (skipped while fps <= 0, i.e. not measured yet): the text * "<fps> FPS", right-aligned in the top-right corner, white with a * dark drop shadow so it reads over both sky and ground. Glyphs come @@ -113,6 +118,9 @@ #define NOSE_SIZE 0.3f /* m, edge length of the facing-direction "nose" box */ #define NOSE_HEIGHT 0.6f /* nose height as a fraction of PLAYER_HEIGHT */ #define GROUND_THICKNESS 1.0f /* m, the ground slab under the grid */ +#define ZONE_DASH_SPACING 3.0f /* m between dashes (and between rows of them) on a zone's faces */ +#define ZONE_DASH_LENGTH 0.8f /* m, each dash */ +#define ZONE_DASH_WIDTH 0.08f /* m, each dash's thickness */ #define HUD_PIXEL 0.006f /* size of one font pixel, as a fraction of the window height */ #define HUD_MARGIN 0.02f /* gap between the FPS text and the window corner, in window heights */ #define HUD_SHADOW_OFFSET 0.5f /* text shadow offset down and right, in font pixels */ @@ -132,6 +140,7 @@ static const float COLOR_WALL_HANG[3] = { 0.58f, 0.46f, 0.78f }; /* violet: s static const float COLOR_CLIFF[3] = { 0.52f, 0.47f, 0.42f }; /* warm grey rock */ static const float COLOR_CLIFF_HANG[3] = { 0.56f, 0.48f, 0.62f }; /* violet-tinted rock: hangable terrain */ static const float COLOR_WATER[3] = { 0.22f, 0.45f, 0.72f }; /* river blue */ +static const float COLOR_ZONE_DASH[4] = { 0.00f, 0.00f, 0.00f, 1.0f }; static const float COLOR_HUD_TEXT[4] = { 1.00f, 1.00f, 1.00f, 1.0f }; static const float COLOR_HUD_SHADOW[4] = { 0.00f, 0.00f, 0.00f, 1.0f }; @@ -184,6 +193,7 @@ static GLuint s_program; static GLint s_u_mvp, s_u_color; static Mesh s_grid, s_cube, s_disc; static GLsizei s_grid_minor_count; /* grid VBO holds minor lines first, then major */ +static Mesh s_zone_dashes; /* every zone's dashes, in world space */ static Mesh s_hud_text; /* the FPS text, in font pixels; rebuilt when the number changes */ static int s_hud_fps = -1; /* the number s_hud_text currently spells (-1 = none yet) */ @@ -462,6 +472,74 @@ static void draw_player(Mat4 vp, const Player *player) draw_mesh(&s_cube, GL_TRIANGLES, mat4_mul(vp, nose), COLOR_NOSE); } +/* ---- Zones ------------------------------------------------------------- */ + +/* Writes (if out isn't NULL) the dashes on one rectangular face: the face + * starts at `origin` and spans u_len along `u` and v_len along `v`. Rows of + * dashes every ZONE_DASH_SPACING, every other row shifted half a spacing. + * Returns how many vertices that is (6 per dash). */ +static int face_dashes(float *out, Vec3 origin, Vec3 u, float u_len, Vec3 v, float v_len) +{ + int count = 0; + int row = 0; + for (float vpos = 0.5f * ZONE_DASH_SPACING; vpos < v_len; vpos += ZONE_DASH_SPACING, row++) { + float shift = (row % 2) ? 0.5f * ZONE_DASH_SPACING : 0.0f; + for (float upos = 0.25f * ZONE_DASH_SPACING + shift; upos < u_len; upos += ZONE_DASH_SPACING) { + float u1 = fminf(upos + ZONE_DASH_LENGTH, u_len); + Vec3 corners[4] = { + vec3_add(origin, vec3_add(vec3_scale(u, upos), vec3_scale(v, vpos - 0.5f * ZONE_DASH_WIDTH))), + vec3_add(origin, vec3_add(vec3_scale(u, u1), vec3_scale(v, vpos - 0.5f * ZONE_DASH_WIDTH))), + vec3_add(origin, vec3_add(vec3_scale(u, u1), vec3_scale(v, vpos + 0.5f * ZONE_DASH_WIDTH))), + vec3_add(origin, vec3_add(vec3_scale(u, upos), vec3_scale(v, vpos + 0.5f * ZONE_DASH_WIDTH))), + }; + static const int ORDER[6] = { 0, 1, 2, 0, 2, 3 }; + for (int k = 0; k < 6 && out; k++) { + out[(count + k) * 3 + 0] = corners[ORDER[k]].x; + out[(count + k) * 3 + 1] = corners[ORDER[k]].y; + out[(count + k) * 3 + 2] = corners[ORDER[k]].z; + } + count += 6; + } + } + return count; +} + +/* All five dashed faces (four sides and the top) of one zone. */ +static int zone_dashes(float *out, Box b) +{ + Vec3 x = vec3_make(1.0f, 0.0f, 0.0f), y = vec3_make(0.0f, 1.0f, 0.0f), z = vec3_make(0.0f, 0.0f, 1.0f); + Vec3 size = vec3_sub(b.max, b.min); + Vec3 faces[5] = { b.min, vec3_make(b.min.x, b.min.y, b.max.z), b.min, + vec3_make(b.max.x, b.min.y, b.min.z), vec3_make(b.min.x, b.max.y, b.min.z) }; + Vec3 us[5] = { x, x, z, z, x }; + Vec3 vs[5] = { y, y, y, y, z }; + float ul[5] = { size.x, size.x, size.z, size.z, size.x }; + float vl[5] = { size.y, size.y, size.y, size.y, size.z }; + int count = 0; + for (int f = 0; f < 5; f++) + count += face_dashes(out ? out + count * 3 : NULL, faces[f], us[f], ul[f], vs[f], vl[f]); + return count; +} + +/* One mesh with every zone's dashes: count them, then fill them in. */ +static bool build_zone_dashes(void) +{ + int count = 0; + for (int i = 0; i < world_zone_count(); i++) + count += zone_dashes(NULL, world_zone(i).bounds); + float *verts = malloc((size_t)(count > 0 ? count : 1) * 3 * sizeof(float)); + if (!verts) { + fprintf(stderr, "render: out of memory building zone dashes\n"); + return false; + } + int filled = 0; + for (int i = 0; i < world_zone_count(); i++) + filled += zone_dashes(verts + filled * 3, world_zone(i).bounds); + s_zone_dashes = make_mesh(verts, filled); + free(verts); + return true; +} + /* ---- FPS counter ------------------------------------------------------- */ /* Returns the font's glyph for ch, or NULL (e.g. a space) to leave a gap. */ @@ -560,6 +638,10 @@ bool render_init(void) } build_cube(); build_disc(); + if (!build_zone_dashes()) { + glDeleteProgram(s_program); + return false; + } s_hud_text = make_mesh(NULL, 0); /* filled in by build_hud_text() once FPS is known */ return true; } @@ -577,6 +659,7 @@ void render_frame(const Camera *cam, const Player *player, float aspect, int fps draw_platforms(vp); draw_shadow(vp, player); /* before the player, so the player is drawn over it */ draw_player(vp, player); + draw_mesh(&s_zone_dashes, GL_TRIANGLES, vp, COLOR_ZONE_DASH); /* zones: dashes only, see-through */ draw_fps(fps, aspect); /* last: it sits on top of everything */ glBindVertexArray(0); @@ -587,6 +670,7 @@ void render_shutdown(void) delete_mesh(&s_grid); delete_mesh(&s_cube); delete_mesh(&s_disc); + delete_mesh(&s_zone_dashes); delete_mesh(&s_hud_text); glDeleteProgram(s_program); } diff --git a/platformer/src/world.c b/platformer/src/world.c index 7bbde8e..c999f41 100644 --- a/platformer/src/world.c +++ b/platformer/src/world.c @@ -29,6 +29,9 @@ * WORLD_NO_CEILING. * world_find_overlap: the first box that overlaps the body's full volume * (footprint AND height range). + * - Zones (level_build_zones()): stored separately from the boxes, so no + * collision query ever sees them. world_in_zone(point, kind): is the point + * inside (edges count) any zone of that kind. * - world_clamp_to_bounds, world_spawn_point (level_spawn_point()), * world_half_extent. * @@ -54,6 +57,8 @@ static Box s_boxes[LEVEL_MAX_BOXES]; /* where each box is now */ static Vec3 s_moved[LEVEL_MAX_BOXES]; /* how far each box moved in the last world_update */ static int s_box_count; static float s_time; /* s since world_init */ +static Zone s_zones[LEVEL_MAX_ZONES]; /* non-solid regions, kept apart from the boxes */ +static int s_zone_count; /* Where box i is at the current time. */ static Box box_now(int i) @@ -67,7 +72,8 @@ static Box box_now(int i) void world_init(void) { - s_box_count = level_build(s_level, LEVEL_MAX_BOXES); + s_box_count = level_build(s_level, LEVEL_MAX_BOXES); + s_zone_count = level_build_zones(s_zones, LEVEL_MAX_ZONES); s_time = 0.0f; for (int i = 0; i < s_box_count; i++) { s_boxes[i] = box_now(i); @@ -170,6 +176,29 @@ Vec3 world_spawn_point(void) return level_spawn_point(); } +int world_zone_count(void) +{ + return s_zone_count; +} + +Zone world_zone(int i) +{ + return s_zones[i]; +} + +bool world_in_zone(Vec3 point, ZoneKind kind) +{ + for (int i = 0; i < s_zone_count; i++) { + Box b = s_zones[i].bounds; + if (s_zones[i].kind == kind && + point.x >= b.min.x && point.x <= b.max.x && + point.y >= b.min.y && point.y <= b.max.y && + point.z >= b.min.z && point.z <= b.max.z) + return true; + } + return false; +} + float world_half_extent(void) { return WORLD_HALF_EXTENT; diff --git a/platformer/src/world.h b/platformer/src/world.h index 6cd1ce0..4cfcf71 100644 --- a/platformer/src/world.h +++ b/platformer/src/world.h @@ -29,6 +29,14 @@ * BoxStyle style; how it's drawn; BOX_STYLE_PLATFORM (0) by default * } Box; * + * typedef enum ZoneKind { + * ZONE_LOCKED_PAN the camera doesn't swing behind a player in it + * } ZoneKind; + * typedef struct Zone { Box bounds; ZoneKind kind; } Zone; + * A zone is NOT solid: the player walks in and out freely, and no + * collision query ever sees it. It only changes how things behave + * while the player is inside. + * * void world_init(void); * Builds the level (level_build() in level.c). * void world_update(float dt); @@ -60,6 +68,10 @@ * Vec3 world_spawn_point(void); * Where the player starts / resets to (level_spawn_point()). The * area around it is kept clear of platforms. + * int world_zone_count(void); + * Zone world_zone(int i); 0 <= i < world_zone_count() (render.c draws them) + * bool world_in_zone(Vec3 point, ZoneKind kind); + * True if the point is inside any zone of that kind (edges count). * float world_half_extent(void); * Returns WORLD_HALF_EXTENT (so render.c doesn't need config * knowledge of the world's shape). @@ -97,6 +109,18 @@ typedef struct Box { BoxStyle style; /* how it's drawn; BOX_STYLE_PLATFORM unless the level says otherwise */ } Box; +/* What a zone does to a player inside it. */ +typedef enum ZoneKind { + ZONE_LOCKED_PAN /* the camera doesn't swing behind the player (it still tilts) */ +} ZoneKind; + +/* A region of the level that changes behavior inside it. NOT solid: the + * player passes in and out freely, and collision never sees it. */ +typedef struct Zone { + Box bounds; + ZoneKind kind; +} Zone; + void world_init(void); void world_update(float dt); Vec3 world_box_moved(int i); @@ -107,6 +131,9 @@ 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); +int world_zone_count(void); +Zone world_zone(int i); +bool world_in_zone(Vec3 point, ZoneKind kind); float world_half_extent(void); #endif /* WORLD_H */ |
