Entity
TheEntity class provides physics-based entity simulation with AABB collision detection, gravity, friction, and step assist.
Class Definition
Constructor
Entity(World* w)
Creates a new entity in the given world. Parameters:w- Pointer to the World instance
- Position:
(0, 80, 0) - Rotation:
(-1.57, 0)(pitch, yaw) - Velocity and acceleration: zero vectors
Methods
void update(float dt)
Updates entity physics for the current frame. Parameters:dt- Delta time in seconds
- Saves old position for interpolation
- Applies smooth step offset decay
- Applies input acceleration with friction
- Performs 3-pass collision detection and resolution
- Applies gravity (configurable for flying mode)
- Applies velocity decay based on movement state
- Broad-phase: searches blocks in movement direction
- Sweep test: uses AABB collision with velocity vector
- Step assist: automatically steps up blocks ≤0.6 units high when grounded
- Early exit offset: -0.001 to prevent edge sticking
void update_collider()
Recalculates the entity’s AABB collider based on current position and dimensions. Collider bounds:- Min:
position - vec3(width/2, 0, width/2) - Max:
position + vec3(width/2, height, width/2)
void jump()
Applies upward velocity for jumping (only when grounded). Jump mechanics:- Jump height: 1.25 blocks
- Initial velocity:
sqrt(2 * height * gravity)≈ 8.0 m/s - Only activates when
grounded == true
Physics Constants
Step Assist Feature
When the entity collides horizontally while grounded and not flying, the system automatically lifts the entity by up to 0.6 blocks if there’s clearance above:step_offset field smoothly animates the camera back down at 8 units/second to hide the instant physics jump.
Collision Resolution
The entity performs 3 collision passes per frame to resolve complex geometry:- Broad-phase search: Scans blocks in a bounding box around movement path
- Sweep test: For each block collider, performs AABB sweep with
Collider::collide() - Normal-based resolution: Zeroes velocity component along collision normal