Angles, trigonometry, pendulums, and springs — the mathematics of things that swing back and forth.
Chapters 1 and 2 gave us vectors and forces — objects that move in straight lines and curves. But so much of the natural world moves in cycles: a pendulum swings, a heartbeat pulses, a buoy bobs on waves, a planet orbits a star.
To simulate these patterns, we need trigonometry — the math of triangles, angles, and periodic functions. Don't panic: we only need a handful of trig concepts, and they'll unlock an enormous range of motion patterns.
| Linear motion (Ch 1-2) | Angular motion (Ch 3) |
|---|---|
| location, velocity, acceleration | angle, angular velocity, angular acceleration |
| Position in x,y space | Rotation around a point |
| Forces push/pull in a direction | Torque rotates around a pivot |
You probably think of angles in degrees: a full circle is 360, a right angle is 90. But computers (and mathematicians) prefer radians.
One radian is the angle where the arc length equals the radius. A full circle is 2π radians (about 6.28). Half a circle is π (about 3.14).
| Degrees | Radians | Meaning |
|---|---|---|
| 0 | 0 | No rotation |
| 90 | π/2 ≈ 1.57 | Quarter turn |
| 180 | π ≈ 3.14 | Half turn |
| 360 | 2π ≈ 6.28 | Full circle |
Remember the motion algorithm from Chapter 1?
The exact same pattern applies to rotation:
It's simpler, actually, because angle is a single number (a scalar), not a vector. One variable for the angle, one for angular velocity, one for angular acceleration.
pseudocode angle = 0 aVelocity = 0 aAcceleration = 0.001 each frame: aVelocity = aVelocity + aAcceleration angle = angle + aVelocity
Time for SOHCAHTOA — the mnemonic for the three basic trig functions applied to a right triangle:
| Function | Mnemonic | Formula |
|---|---|---|
| sine | SOH | opposite / hypotenuse |
| cosine | CAH | adjacent / hypotenuse |
| tangent | TOA | opposite / adjacent |
Think of a vector as the hypotenuse of a right triangle. Its x-component is the adjacent side, its y-component is the opposite side, and the angle is measured from the positive x-axis.
x = r * cos(θ) and y = r * sin(θ) (polar to Cartesian). If you know x,y and want the angle: θ = atan2(y, x) (Cartesian to polar). These two conversions are everywhere in graphics.The atan2 function is better than plain atan because it handles all four quadrants correctly. Always use atan2.
The sine function produces a smooth wave that oscillates between −1 and +1. If you plot sin(θ) as θ goes from 0 to 2π, you get one complete cycle: up, back to zero, down, back to zero.
Where A is the amplitude (how tall the wave is). The period is how much θ changes before the wave repeats (2π by default). The frequency is how many cycles occur per unit.
| Property | Controls | Formula |
|---|---|---|
| Amplitude | Height of wave | A in A × sin(θ) |
| Period | Length of one cycle | 2π / frequency |
| Frequency | How fast it oscillates | 2π / period |
pseudocode angle = 0 each frame: y = amplitude * sin(angle) angle = angle + 0.05 # angular velocity
Simple harmonic motion is what you get when an object is pulled back toward a center point by a force proportional to its displacement. Think of a weight hanging from a spring — the farther you pull it from rest, the harder the spring pulls it back.
We can describe the position of an oscillating object entirely with sine:
Or, equivalently, by tracking an angle that increments each frame:
pseudocode angle = 0 aVelocity = 0.05 amplitude = 100 each frame: x = amplitude * sin(angle) angle = angle + aVelocity
To oscillate in 2D, use sine for one axis and cosine for the other. That traces a circle (or an ellipse if the amplitudes differ).
A pendulum is one of the most beautiful examples of oscillation driven by forces. A ball hangs from a rod attached to a pivot. Gravity pulls it down, but the rod constrains it to swing in an arc.
The key insight is that only the tangential component of gravity matters. The component along the rod is cancelled by the rod's tension. What remains is:
Where g is gravitational acceleration, L is the pendulum's length, and θ is the current angle from vertical.
gravity × sin(θ). When θ is small, sin(θ) ≈ θ, which is why small oscillations are nearly perfect sine waves.pseudocode angle = PI/4 # start at 45 degrees aVelocity = 0 damping = 0.995 # slight energy loss gravity = 0.4 length = 200 each frame: aAcceleration = -(gravity / length) * sin(angle) aVelocity = aVelocity + aAcceleration aVelocity = aVelocity * damping angle = angle + aVelocity # Convert angle to position bobX = length * sin(angle) bobY = length * cos(angle)
The damping factor (multiplying angular velocity by 0.995) simulates air resistance. Without it, the pendulum would swing forever. With it, the swings gradually shrink until the pendulum hangs still.
A spring is another classic oscillating system. Hooke's Law describes the force exerted by a spring:
Where k is the spring constant (stiffness) and x is the displacement from the spring's rest length. The negative sign means the force always pulls back toward equilibrium.
| Property | Effect |
|---|---|
| Higher k (stiffer spring) | Stronger restoring force, faster oscillation |
| Lower k (softer spring) | Weaker restoring force, slower oscillation |
| Larger displacement | Stronger force (proportional) |
In code, implementing a spring between an anchor point and a bob:
pseudocode dir = bob.location - anchor # vector from anchor to bob currentLen = dir.mag() stretch = currentLen - restLength # displacement dir.normalize() springForce = dir * (-k * stretch) bob.applyForce(springForce)
Here's a full pendulum simulation using the angular acceleration formula from Chapter 6. Drag the bob to release it from a new angle. Watch how gravity and the constraint of the rod produce smooth oscillation that gradually dampens.
Drag the bob to set a starting angle, then release. The pendulum swings under gravity with slight damping. Watch the amplitude shrink over time.
This chapter gave us the tools to handle rotation, periodic motion, and oscillation.
| Concept | Key Formula | Use Case |
|---|---|---|
| Radians | degrees × π/180 | All rotation in code |
| Angular motion | angle += aVel; aVel += aAcc | Spinning objects |
| Polar-to-Cartesian | x = r cos(θ), y = r sin(θ) | Circular motion, waves |
| atan2 | θ = atan2(y, x) | Pointing in direction of movement |
| Sine wave | y = A sin(θ) | Oscillation, easing, waves |
| Pendulum | aAcc = -(g/L)sin(θ) | Swinging objects |
| Spring (Hooke's) | F = -kx | Elastic connections |
What we covered:
• Radians and degrees
• Angular velocity & acceleration
• SOHCAHTOA and atan2
• Sine waves (amplitude, period, frequency)
• Simple harmonic motion
• Pendulums (force-driven oscillation)
• Springs (Hooke's law)
What comes next:
Chapter 4: Particle Systems. One ball is fun. Thousands of balls is spectacular. We'll learn to manage large collections of short-lived objects — sparks, smoke, fireworks — using emitters, lifespans, and dynamic arrays.