Overview
The physics system handles gravity, movement, friction, drag, and collision resolution for all entities in MC-CPP. The core implementation is in theEntity class.
Location: src/entity/entity.h, src/entity/entity.cpp
Entity Class
Physics Constants
All constants are defined atentity.cpp:8-23:
- Gravity:
-32 blocks/s²on Y axis (standard Minecraft) - Ground friction: High (
20) for quick stopping - Air resistance varies by movement state
Entity Dimensions
Default size atentity.h:17-18:
Jumping Mechanics
Implementation atentity.cpp:31-37:
- Uses kinematic equation:
v = sqrt(2 * h * g) - Jump height:
1.25 blocks - Initial jump velocity:
sqrt(2 * 1.25 * 32) ≈ 8 blocks/s - Only works when
grounded == true
Friction Selection
Helper function atentity.cpp:40-45 chooses friction based on state:
- Flying → constant drag in all directions
- Grounded → high friction to stop movement
- Airborne & rising → horizontal drag, no vertical drag
- Airborne & falling → horizontal drag + slight vertical drag
Physics Update Loop
Theupdate() method at entity.cpp:47-208 runs every frame:
1. Save Previous Position
2. Step Offset Smoothing
Atentity.cpp:51-56, smoothly lowers camera after climbing stairs:
3. Apply Input Acceleration
Atentity.cpp:59-62:
- Multiplies acceleration by friction/drag before applying
- Resets
accelso it must be set each frame by input code
4. Collision Resolution
Atentity.cpp:65-176, performs 3 passes of collision detection:
- Runs 3 iterations to resolve multi-axis collisions
- Finds earliest collision time using swept AABB (see Collision Detection)
- Moves entity to collision point minus small epsilon
- Sets
grounded = truewhen hitting upward-facing surface (normal.y == 1) - Zeros velocity on collision axes
5. Step Assist (Stair Climbing)
Atentity.cpp:121-159, allows stepping up blocks smoothly:
- Only active when grounded and hitting horizontal obstacle
- Maximum step height:
0.6 blocks - Checks if stepped-up position is free of collisions
- Uses
step_offsetto smoothly animate camera rising - Updates
old_positionto avoid interpolation artifacts
6. Apply Remaining Velocity
Atentity.cpp:182:
7. Apply Gravity
Atentity.cpp:186-187:
- Normal mode:
-32 blocks/s²vertical acceleration - Flying mode: No gravity
8. Apply Velocity Decay
Atentity.cpp:190-205, applies friction/drag to slow down:
- Exponential decay proportional to current velocity
- Prevents velocity from oscillating around zero
9. Update Collider
Atentity.cpp:207:
Collider Update
Atentity.cpp:27-29:
- Centered on X/Z axes around
position - Bottom at
position.y, top atposition.y + height - Forms AABB for collision detection