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
|
/*
* ============================================================
* FILE: physics.h
*
* PURPOSE: Public interface for generic physical motion: a `Body` that
* falls, moves, lands on floors, slides along walls and bumps its
* head on ceilings. It knows nothing about players or controllers,
* so enemies and objects can reuse it later.
*
* FITS IN: player.c owns a Body, sets its horizontal velocity from input,
* then calls physics_step() once per tick. physics.c asks world.h
* about floors, ceilings, overlapping boxes and the bounds.
*
* INTERFACE: Use these exact names:
*
* typedef struct Body {
* Vec3 pos; position of the body's FEET (bottom center), meters
* Vec3 vel; velocity, meters/second
* bool on_ground; true if the body ended the last step standing on
* something (the ground or a platform top)
* float half_width; collision box half-size in x and z, meters
* float height; collision box height above the feet, meters
* float gravity_scale; multiplier on GRAVITY (1 = normal). The owner can
* set 0 to take over vertical motion itself (the
* player does while clinging to a wall).
* bool hit_wall; true if the last step pushed the body out of a box's
* SIDE (not a top or bottom)
* Vec3 wall_normal; that side's outward normal (+-x or +-z, y = 0);
* only meaningful if hit_wall
* Box wall; the box that side belongs to; only meaningful if hit_wall
* float step_dy; how far a stair step moved the body up (+) or down (-)
* during the last step (0 if it didn't step), so the
* owner can smooth the pop out of what it draws
* int ground_box; index (world_box) of the box the body stands on, or -1
* for the ground / in the air. Used to ride moving boxes.
* } Body;
*
* void physics_step(Body *b, float dt);
*
* Include <stdbool.h>, "math3d.h" and "world.h" (for Box).
*
* CONVENTIONS: Include guard: PHYSICS_H. The collision shape is an axis-aligned
* box that does not rotate (a spinning box would snag on walls).
*
* NOT IN SCOPE: Rigid bodies, rotation, mass, forces/impulses API, collision
* between bodies, keeping a moving box's momentum after leaving it.
*
* DONE WHEN: player.c can move, land, and collide using only Body + physics_step.
* ============================================================
*/
#ifndef PHYSICS_H
#define PHYSICS_H
#include <stdbool.h>
#include "math3d.h"
#include "world.h"
typedef struct Body {
Vec3 pos; /* position of the body's FEET (bottom center), meters */
Vec3 vel; /* velocity, meters/second */
bool on_ground; /* true if the body ended the last step standing on something */
float half_width; /* collision box half-size in x and z, meters */
float height; /* collision box height above the feet, meters */
float gravity_scale; /* multiplier on GRAVITY; 1 = normal, 0 = the owner drives vel.y itself */
bool hit_wall; /* the last step pushed the body out of a box's side */
Vec3 wall_normal; /* that side's outward normal (+-x or +-z); only if hit_wall */
Box wall; /* the box that side belongs to; only if hit_wall */
int ground_box; /* world_box index stood on (-1 = ground or airborne); for riding moving boxes */
float step_dy; /* m a stair step moved the body this step (+ up, - down; 0 if none), for smoothing visuals */
} Body;
void physics_step(Body *b, float dt);
#endif /* PHYSICS_H */
|