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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/*
* ============================================================
* 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.
* ============================================================
*/
#include "config.h"
#include "physics.h"
#include "world.h"
void physics_step(Body *b, float dt)
{
/* 1. Gravity. 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. */
if (!b->on_ground) {
b->vel.y -= GRAVITY * dt;
if (b->vel.y < -TERMINAL_VELOCITY)
b->vel.y = -TERMINAL_VELOCITY;
}
/* 2. Integrate with semi-implicit Euler: velocity was updated above, and
* position now moves with that NEW velocity. Unlike plain Euler (which
* uses the old velocity) this doesn't gain energy over time, and with a
* fixed dt the result is identical every run, so a jump always
* reaches the same height. */
b->pos = vec3_add(b->pos, vec3_scale(b->vel, dt));
/* 3. Ground. Landing snaps the feet to the surface and kills the downward
* speed. on_ground goes false the moment a jump lifts the body (and,
* once terrain exists, when walking off a ledge). */
float ground = world_ground_height(b->pos.x, b->pos.z);
if (b->pos.y <= ground) {
b->pos.y = ground;
if (b->vel.y < 0.0f)
b->vel.y = 0.0f;
b->on_ground = true;
} else {
b->on_ground = false;
}
/* 4. Bounds. Zero the velocity into an edge we got clamped against, so
* the body doesn't "stick" to it while pushing into it. */
Vec3 clamped = world_clamp_to_bounds(b->pos);
if (clamped.x != b->pos.x) b->vel.x = 0.0f;
if (clamped.z != b->pos.z) b->vel.z = 0.0f;
b->pos = clamped;
}
|