Physics

Resources:

Shiffman - Nature of Code - Chapter 2: Forces

Physics

We have previously made some simple representations of motion, like:
yPos = yPos += ySpeed;
The code does not take into account other forces that might be exerted on the circle. For example, the circle might have a large mass, or gravity may apply a strong force, or it might be moving across a rough surface so that high friction slows it down.

These forces are omnipresent in the physical world, but they affect a software simulation only if they are included as parts of the design. They need to be calculated at each frame to exert their influence.

Velocity defines the speed and direction as one number. For example, a velocity of -5 moves the position in a negative direction at a speed of 5. A velocity of 12 moves the position in a positive direction at a speed of 12. Speed is defined as the magnitude (absolute value) of the velocity.

Acceleration defines the rate of change in the velocity. An acceleration value greater than zero means the velocity will increase each frame, and an acceleration value less than zero means the velocity will decrease each frame.

We need to update both each frame:
velocity = velocity + acceleration
yPos = yPos + velocity

In the following example, add gravity and friction to the bouncing spot.

Solution:

The following example shows two Box objects that bounce and collide with each other.