Project 31: Physics Engine and Numerical Dynamics Lab
A self-contained deep-dive from the canonical Math from Foundations to Machine Learning curriculum.
- Difficulty: Advanced
- Time: 2-3 weeks
- Language: Python with Pygame or Matplotlib
- Prerequisites: Projects 16, 21, 26, and 29-30
- Merged from:
Prog P04;ML-Found P08
What You Will Build
Build a 2D particle physics engine where forces produce acceleration, numerical integration updates velocity and position, and collisions and springs make stability visible. Implement fixed-step explicit Euler, semi-implicit Euler, and Verlet integration behind the same interface. Support gravity, drag, Hooke-law springs, circles colliding with boundaries and one another, and a pause/step inspector. Track kinetic, gravitational, and spring potential energy, momentum, constraint error, and simulation time. Provide controlled experiments that vary time step, spring stiffness, and integrator so the learner can see Euler’s energy drift and Verlet’s different behavior. Keep rendering decoupled from state updates so frame-rate changes do not silently alter physics.
Real World Outcome
An interactive window lets the user add balls and springs, toggle gravity, choose an integrator, and slow or single-step the simulation. Live plots compare total energy and momentum. A benchmark scene exports a table showing when each integrator remains stable or explodes as time step and stiffness change.
Core Question
How can continuous laws of motion be approximated by discrete updates without allowing numerical error to overwhelm the physical behavior?
Concepts You Must Understand First
- Newton’s second law: net force gives acceleration
a=F/m; force accumulation must reset every step. See Serway and Jewett, Physics for Scientists and Engineers. - State derivatives: velocity is position’s derivative and acceleration is velocity’s derivative.
- Numerical integration: Euler and Verlet make different assumptions about how state changes during a step. See Millington, Game Physics Engine Development, ch. 3.
- Hooke’s law and damping: spring force depends on displacement; damping opposes relative motion.
- Impulse collision response: project relative velocity onto the collision normal and apply restitution while correcting overlap carefully.
Build Milestones
- Separate vector math, bodies, force generators, integrators, collision detection, and rendering.
- Simulate free fall with fixed-step Euler variants and compare against the analytic trajectory.
- Add springs and Verlet integration; plot energy drift across integrators and time steps.
- Detect and resolve circle-wall and circle-circle collisions with restitution and overlap correction.
- Build parameter-sweep scenarios and an inspector for forces, velocities, contacts, and energy.
Hints in Layers
- Validate one falling particle with no rendering; its position should approach
y0+v0t+½at². - Accumulate forces first, integrate all bodies second, and resolve contacts in a clearly documented order.
- Use an accumulator for a fixed physics step independent of display FPS; cap catch-up work to avoid a spiral of death.
Common Pitfalls and Debugging
- Symptom: motion changes with monitor frame rate. Cause: variable render delta drives integration. Fix: use a fixed simulation step and interpolate rendering if needed.
- Symptom: springs gain energy and explode. Cause: explicit Euler plus a stiff spring or large dt. Fix: compare semi-implicit/Verlet and reduce step size.
- Symptom: objects stick or jitter at boundaries. Cause: repeated penetration and impulse application. Fix: separate positional correction from velocity impulse and inspect contact normals.
Definition of Done
- At least three integrators share one tested state-update interface.
- Free-fall error is measured against an analytic reference.
- Gravity, drag, springs, boundaries, and circle collisions work interactively.
- Energy and momentum diagnostics expose drift and collision behavior.
- Simulation results are independent of rendering frame rate.
- A parameter sweep documents stable and unstable time-step regimes.
Navigation
Previous: Project 30 · Complete learning path · Next: Project 32