1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
/*
* ============================================================
* FILE: physics.c
*
* PURPOSE: Moves a Body forward one fixed tick: gravity, integration,
* landing on the ground, and staying inside the world.
*
* FITS IN: Implements physics.h. Called by player.c inside player_update(),
* AFTER the player has chosen its velocity for this tick. Uses
* world_ground_height() and world_clamp_to_bounds() from world.h.
*
* IMPLEMENT physics_step(b, dt), in this order:
* 1. Gravity: if !b->on_ground, vel.y -= GRAVITY * dt, then clamp vel.y to
* no lower than -TERMINAL_VELOCITY.
* (When on the ground, vel.y is left alone. A jump sets it positive
* before this call, and that's what lifts the body off the ground.)
* 2. Integrate (semi-implicit Euler: velocity was updated first, then position):
* pos += vel * dt
* Add a comment explaining why this order is stable enough for a
* fixed timestep.
* 3. Ground: ground = world_ground_height(pos.x, pos.z).
* if pos.y <= ground: pos.y = ground; if vel.y < 0, vel.y = 0;
* on_ground = true
* else: on_ground = false
* Note: on_ground must become false the moment a jump lifts the body,
* and walking off a ledge will also set it false once terrain exists.
* 4. Bounds: pos = world_clamp_to_bounds(pos). If x or z got clamped,
* zero that velocity component so the body doesn't "stick" against
* the edge while pushing into it.
*
* CONVENTIONS: dt is always TICK_DT in practice, but use the parameter rather
* than the constant so the function stays general.
*
* NOT IN SCOPE: Friction/acceleration (that's player movement *intent*, in
* player.c), wall/ceiling collision, slopes, sub-stepping.
*
* DONE WHEN: A body placed at y = 10 falls, speeds up, lands exactly on
* y = 0 with on_ground = true, and stays there.
* ============================================================
*/
|