Math 255- Math Fundamentals for Game

What Math Do Games Need?

Every moving object in a game is a position, a direction, and a rule for changing them. Vectors describe positions and directions, trigonometry converts between angles and directions, physics updates motion each frame, rotations orient objects in 3D, and collision tests decide when objects touch.

A useful approach: Draw the situation → name the vectors → choose the formula → substitute numbers → check units and sign → test against the game (does it look right?).

1. Vectors

A vector stores a magnitude and a direction as components. In a game it can be a position, a velocity, or a direction.

v=(vx,vy,vz)

Core Vector Operations

OperationFormulaGame Use
Additiona+b=(ax+bx,ay+by)Combine movement and wind
Scalingkv=(kvx,kvy)Change speed
Magnitude|v|=vx2+vy2+vz2Speed or distance
Normalizev^=v|v|Direction only, length 1
Movepnew=p+vΔtUpdate position each frame
Example: Normalize v=(3,4).
Step 1: Find the magnitude.
|v|=32+42=25=5
Step 2: Divide each component by 5.
v^=(35,45)=(0.6,0.8)
Step 3: Check: 0.36+0.64=1.

Conclusion: The unit direction is (0.6,0.8).

Example: A player is at p=(2,1) with velocity v=(4,−2) units/s. Find the position after Δt=0.5 s.
Step 1: Scale the velocity.
vΔt=0.5(4,−2)=(2,−1)
Step 2: Add to the position.
pnew=(2,1)+(2,−1)=(4,0)

Conclusion: The new position is (4,0).

Always normalize a direction before multiplying by speed. Otherwise diagonal movement is faster than straight movement: (1,1) has length 2≈1.41.
Practice: Find the magnitude of v=(2,3,6).

|v|=4+9+36=49=7
Practice: Normalize v=(0,5).

|v|=5, so v^=(0,1).

2. Dot Product and Cross Product

These two products measure how vectors relate: the dot product returns a number about alignment, and the cross product returns a vector perpendicular to both inputs.

a⋅b=axbx+ayby+azbz=|a||b|cosθ
a×b=(aybz−azby,azbx−axbz,axby−aybx)
ResultDot Product MeaningGame Use
a⋅b>0Vectors point roughly the same wayTarget is in front
a⋅b=0Vectors are perpendicularRight-angle check
a⋅b<0Vectors point roughly oppositeTarget is behind
Example: Find the angle between a=(2,0) and b=(3,3).
Step 1: Dot product: 2⋅3+0⋅3=6.
Step 2: Magnitudes: |a|=2 and |b|=18≈4.243.
Step 3: Solve for the angle.
cosθ=62(4.243)≈0.7071→θ=45°

Conclusion: The angle is 45°.

Example: Find the surface normal of a triangle with edge vectors a=(2,3,4) and b=(5,6,7).
Step 1: First component: 3⋅7−4⋅6=−3.
Step 2: Second component: 4⋅5−2⋅7=6.
Step 3: Third component: 2⋅6−3⋅5=−3.

Conclusion: a×b=(−3,6,−3). Normalize it to use it as a lighting or collision normal.

Example: Project a=(3,4) onto the x-axis direction b=(1,0).
Step 1: Use projba=a⋅bb⋅bb.
Step 2: Substitute: 31(1,0)=(3,0).

Conclusion: The projection is (3,0). Projection is how you slide along walls and ramps.

Practice: Compute a⋅b for a=(1,2,3) and b=(4,−5,6).

4−10+18=12
Practice: Compute (1,0,0)×(0,1,0).

(0⋅0−0⋅1,0⋅0−1⋅0,1⋅1−0⋅0)=(0,0,1)

3. Trigonometry

Trigonometry connects an angle to a direction. For a right triangle with hypotenuse r:

sinθ=yrcosθ=xrtanθ=yx

Rearranged, a direction from an angle is (x,y)=(rcosθ,rsinθ), and radians relate to degrees by rad=deg⋅π180.

AngleRadiansSineCosine
0°001
30°π61232
45°π42222
60°π33212
90°π210
Example: A projectile launches at speed 10 m/s and angle 30°. Find its velocity components.
Step 1: Horizontal: vx=10cos30°=10⋅32≈8.66.
Step 2: Vertical: vy=10sin30°=10⋅12=5.

Conclusion: v=(8.66,5) m/s.

Example: An enemy is at offset (x,y)=(−1,1) from the player. Find the facing angle.
Step 1: Use the two-argument arctangent, which respects the quadrant: θ=atan2(y,x).
Step 2: The point is in quadrant II, so θ=atan2(1,−1)=135°=3π4.

Conclusion: The enemy is at 135°. Plain arctan(y/x)=−45° would point the wrong way.

Most game engines use radians in their trig functions. Convert degrees before calling sin or cos.
Practice: Convert 60° to radians.

60⋅π180=π3≈1.047
Practice: Find the direction vector for a heading of 90° with speed 4.

(4cos90°,4sin90°)=(0,4)

4. Physics for Movement

Physics in games is a loop: compute forces, update velocity, update position. Constant acceleration a (such as gravity g=9.8 m/s²) follows these kinematic equations:

QuantityFormulaMeaning
Velocityv=v0+atSpeed after time t
Positiony=y0+v0t+12at2Position after time t
Speed–distancev2=v02+2aΔyNo time needed
RangeR=v2sin2θgFlat-ground projectile
ForceF=maAcceleration from force
Example: A character jumps upward at v0=6 m/s with g=9.8 m/s². Find the time to the peak and the jump height.
Step 1: At the peak v=0, so 0=6−9.8t and t=69.8≈0.61 s.
Step 2: Use v2=v02−2gh with v=0:
h=v022g=3619.6≈1.84m

Conclusion: The peak occurs at 0.61 s at a height of about 1.84 m.

Example: Use semi-implicit Euler for a ball at y=10, v=0, a=−9.8, Δt=0.1.
Step 1: Update velocity first: v=0+(−9.8)(0.1)=−0.98.
Step 2: Then update position with the new velocity: y=10+(−0.98)(0.1)=9.902.

Conclusion: After one frame, v=−0.98 and y=9.902. Repeat every frame.

Example: Find the range of a projectile with v=20 m/s at 45°.
Step 1: sin2θ=sin90°=1.
Step 2: R=202(1)9.8=4009.8≈40.8.

Conclusion: The range is about 40.8 m.

Practice: How fast is a dropped object moving after 2 s of free fall (g=9.8)?

v=0+9.8(2)=19.6 m/s downward.
Practice: A 2 kg object receives a force of 10 N. Find its acceleration.

a=Fm=102=5 m/s².

5. Rotations in 2D and 3D

Rotating a point is multiplying it by a rotation matrix. In 2D, a counter-clockwise rotation by θ is:

(x‘y‘)=(cosθ−sinθsinθcosθ)(xy)

In 3D, there is one matrix for each axis. The rotation about the x-axis is:

Rx(θ)=(1000cosθ−sinθ0sinθcosθ)
Example: Rotate (1,1) by 90° in 2D.
Step 1: cos90°=0 and sin90°=1.
Step 2: x‘=0⋅1−1⋅1=−1.
Step 3: y‘=1⋅1+0⋅1=1.

Conclusion: The result is (−1,1).

Example: Rotate (0,1,0) by 90° about the x-axis.
Step 1: x‘=0.
Step 2: y‘=cos90°(1)−sin90°(0)=0.
Step 3: z‘=sin90°(1)+cos90°(0)=1.

Conclusion: The result is (0,0,1): up turns into forward.

Quaternions

Chaining Euler-angle rotations can cause gimbal lock, where two axes align and a degree of freedom is lost. Engines use quaternions to avoid this. A rotation of θ about a unit axis n is:

q=(cosθ2,nsinθ2)
Example: Build the quaternion for 90° about the z-axis.
Step 1: Half angle: θ2=45°.
Step 2: w=cos45°≈0.707 and nsin45°=(0,0,0.707).

Conclusion: q≈(0.707,0,0,0.707).

Practice: Rotate (2,0) by 90°.

x‘=2⋅0−0=0, y‘=2⋅1+0=2, so the result is (0,2).
Practice: Why is θ2 used in a quaternion?

A quaternion rotates by sandwiching a vector, qvq−1, which applies the rotation twice in effect, so each side carries half the angle.

6. Collision Detection

Collision tests are chosen to be cheap. Compare squared distances to avoid square roots.

TestConditionNotes
Circle / Sphere(x2−x1)2+(y2−y1)2≤(r1+r2)2Very fast
AABB (box)amax≥bmin and bmax≥amin on every axisCheck x, y (and z)
Ray / Planet=(p0−o)⋅nd⋅nHit if t≥0
Reflectionv‘=v−2(v⋅n)nBounce off a surface
Example: Do circles at (0,0) with r=2 and (3,4) with r=3 collide?
Step 1: Squared distance: 32+42=25.
Step 2: Squared radius sum: (2+3)2=25.
Step 3: Compare: 25≤25.

Conclusion: The circles are exactly touching, which counts as a collision.

Example: A ball with velocity v=(3,−4) hits a floor with normal n=(0,1). Find the bounce velocity.
Step 1: v⋅n=−4.
Step 2: v‘=(3,−4)−2(−4)(0,1)=(3,−4)+(0,8).

Conclusion: v‘=(3,4): the vertical part flips and the horizontal part is kept.

Example: A ray starts at o=(0,5,0) heading d=(0,−1,0). The floor plane passes through p0=(0,0,0) with n=(0,1,0). Where does it hit?
Step 1: (p0−o)⋅n=(0,−5,0)⋅(0,1,0)=−5.
Step 2: d⋅n=−1, so t=−5−1=5.
Step 3: Hit point: o+td=(0,5,0)+5(0,−1,0)=(0,0,0).

Conclusion: The ray hits the floor at (0,0,0) after t=5.

Practice: Do boxes A: x∈[0,4], y∈[0,2] and B: x∈[3,6], y∈[3,5] overlap?

No. The x-ranges overlap, but the y-ranges [0,2] and [3,5] do not, so there is no collision.
Practice: Do spheres at (0,0,0), r=1 and (2,2,1), r=1.5 collide?

Squared distance is 4+4+1=9. Squared radius sum is 2.52=6.25. Since 9>6.25, they do not collide.

Key Takeaways

  • Vectors store positions, velocities, and directions; normalize directions before scaling by speed.
  • The dot product measures alignment and angles; the cross product gives perpendicular normals.
  • Trigonometry turns angles into directions; use atan2 to turn directions into angles.
  • Update velocity, then position, each frame using Δt.
  • Rotation matrices rotate points; quaternions avoid gimbal lock in 3D.
  • Collision tests compare squared distances or ranges; reflection uses the surface normal.