Falling ball with adjustable gravity
Constant acceleration under gravity, the exchange between potential and kinetic energy, and how a coefficient of restitution turns an impact into energy loss.
What it demonstrates
Constant acceleration under gravity, the exchange between potential and kinetic energy, and how a coefficient of restitution turns an impact into energy loss.
How it works
The ball is a point mass. Every 1 ms the simulator applies gravity with semi-implicit Euler: velocity first, then position with the new velocity. Before the first bounce the computed height differs from the exact y₀ − ½gt² by exactly ½·g·t·Δt, about 5 mm after one second on Earth.
When the ball passes below the floor, the penetration is removed and the downward velocity is replaced by e times the impact speed, pointing up. This is an impulse-based contact. The readout shows the impulse divided by the step length as an average force. Once the rebound speed drops below 5 cm/s, the ball is treated as resting and the floor supplies exactly m·g.
Adjustable parameters
- Gravity g (0–25 m/s², presets Moon 1.62, Mars 3.71, Earth 9.81, Jupiter 24.79)
- Coefficient of restitution e (0–1)
- Drop height (0.5–20 m)
- Mass (0.1–5 kg): affects energy and force, not the trajectory
Known limitations
- No air drag. A real ball in air reaches a terminal velocity.
- The ball is a point. Spin, deformation and rolling are not modelled.
- Removing the penetration by clamping the position slightly changes energy at each bounce.
Equations
Core update rule
// lib/sim/ch02-mechanics.ts — stepBall
v -= g * dt; // semi-implicit Euler
y += v * dt;
if (y < 0) {
y = 0; // remove penetration
if (v < 0) {
const vin = -v;
v = e * vin; // restitution
contactForce = m * (1 + e) * vin / dt;
if (v < REST_SPEED) { v = 0; resting = true; }
}
}Explained in Chapter 2: Physics Engines