Physics simulation models position with velocity plus acceleration, where each value updates the next
Simple physics in code separates position, velocity, and acceleration: velocity += acceleration; position += velocity. Positive acceleration increases velocity each frame, producing non-linear speeding up. Negative acceleration decelerates. This two-step update (Euler integration) approximates Newtonian motion. Friction is modelled by multiplying velocity by a factor just below 1.0 (e.g., 0.99) each frame, causing gradual slowdown. Bounce is modelled by reversing the sign of velocity when an object hits a boundary.
Examples
float y=50, velocity=0, acceleration=0.01; void draw() { velocity += acceleration; y += velocity; if (y > height) { y = 0; velocity = 0; } }
Assessment
Write a sketch where a circle falls under gravity, bounces realistically off the bottom (reducing speed each bounce via a restitution factor), and eventually comes to rest.