Project 4: A Simple Physics Engine
Build a 2D physics simulator with gravity, collisions, and basic integration.
Project Overview
| Attribute | Value |
|---|---|
| Difficulty | Level 2: Intermediate |
| Time Estimate | 2-3 weeks |
| Main Language | Python |
| Alternative Languages | C++, Rust |
| Knowledge Area | Kinematics and calculus |
| Tools | Plotting tool or simple GUI |
| Main Book | “Physics for Scientists and Engineers” by Serway & Jewett |
What you’ll build: A simulator that updates positions and velocities over time, with simple collision response.
Why it teaches math: You apply derivatives and integrals to real motion and see errors when integration is wrong.
Core challenges you’ll face:
- Implementing time integration (Euler, semi-implicit)
- Handling collision detection and response
- Keeping the simulation stable
Real World Outcome
You will simulate bouncing balls or blocks under gravity, producing trajectories and collision events.
Example Output:
$ python physics.py --objects 3 --duration 10
Simulation steps: 600
Collisions: 14
Saved trajectory plot to physics.png
Verification steps:
- Check energy loss or conservation depending on settings
- Validate that objects fall at expected rates
The Core Question You’re Answering
“How do derivatives and integrals become a time-based simulation?”
This is calculus in motion.
Concepts You Must Understand First
Stop and research these before coding:
- Newton’s laws
- How does force relate to acceleration?
- Book Reference: “Physics for Scientists and Engineers” by Serway & Jewett, Ch. 4
- Numerical integration
- Why does Euler integration drift over time?
- Book Reference: “Numerical Methods for Engineers” by Chapra & Canale, Ch. 25
- Conservation
- What does energy conservation tell you about your simulator?
- Book Reference: “Physics for Scientists and Engineers” by Serway & Jewett, Ch. 7
Questions to Guide Your Design
- Integrator choice
- Will you use explicit Euler or semi-implicit Euler?
- How will you choose the time step size?
- Collision handling
- Will you use simple elastic collisions or add restitution?
- How will you prevent objects from sinking into each other?
Thinking Exercise
Euler Step
Given a velocity of 10 m/s and gravity -9.8 m/s^2, compute velocity after a 0.1s step using Euler integration.
Questions while working:
- How does step size affect accuracy?
- What happens if you use a much larger step?
The Interview Questions They’ll Ask
Prepare to answer these:
- “What is Euler integration and why is it unstable?”
- “How do you detect collisions in 2D?”
- “What does conservation of energy tell you about your model?”
- “Why does time step size matter?”
- “How would you improve simulation accuracy?”
Hints in Layers
Hint 1: Starting Point Start with a single object under gravity.
Hint 2: Next Level Add collision against the floor with a restitution coefficient.
Hint 3: Technical Details Use semi-implicit Euler for better stability.
Hint 4: Tools/Debugging Plot position vs time to see if motion matches expectation.
Books That Will Help
| Topic | Book | Chapter |
|---|---|---|
| Newton’s laws | “Physics for Scientists and Engineers” by Serway & Jewett | Ch. 4 |
| Numerical integration | “Numerical Methods for Engineers” by Chapra & Canale | Ch. 25 |
| Energy conservation | “Physics for Scientists and Engineers” by Serway & Jewett | Ch. 7 |
Implementation Hints
- Keep physics state in clear structures (position, velocity).
- Start with simple circles to simplify collisions.
- Add friction only after basic collisions work.
Learning Milestones
- First milestone: You can simulate motion under gravity.
- Second milestone: You can detect and resolve simple collisions.
- Final milestone: You can explain stability and integration errors.