Building a Game Physics Engine: Core Math, Vectors, and Newton’s Laws Explained
This article introduces the fundamentals of a game physics engine, covering the concept of engines, essential mathematics like vectors and calculus, TypeScript implementations for vector operations, and the integration of Newtonian mechanics such as mass, inverse mass, and basic object properties.
Overview
Games have become an unavoidable form of entertainment, ranging from large PC titles to mobile and web H5 games. As a front‑end developer, understanding how to build a game and what technical components are required is essential, especially the physics engine that ensures realistic interactions.
What Is an Engine?
An engine, analogous to a car engine that converts energy into motion, is a core module that quickly integrates functionality into a project. Rendering engines handle visual output, game engines provide basic game development scaffolding, and physics engines simulate real‑world physical behavior.
Fundamentals of a Physics Engine
The foundation of any physics engine lies in physics and mathematics. Each frame must compute object positions so they appear correctly, requiring mathematical operations that obey physical laws.
Mathematics in Code
In both 2D and 3D worlds, object positions are represented by vectors, which involve decomposition, addition, subtraction, dot product, and cross product. Below is a basic TypeScript vector class:
class Vector {
x: number
y: number
z: number
constructor(x: number, y: number, z: number) {
this.x = x
this.y = y
this.z = z
}
setX(x: number) { this.x = x }
setY(y: number) { this.y = y }
setZ(z: number) { this.z = z }
}Vector operations are then extended:
class VectorOperation {
add(vectorA: Vector, vectorB: Vector) { // vector addition
return new Vector(
vectorA.x + vectorB.x,
vectorA.y + vectorB.y,
vectorA.z + vectorB.z
)
}
minus(vectorA: Vector, vectorB: Vector) { // vector subtraction
return new Vector(
vectorA.x - vectorB.x,
vectorA.y - vectorB.y,
vectorA.z - vectorB.z
)
}
multiply(vectorA: Vector, times: number) { // scaling
return new Vector(
vectorA.x * times,
vectorA.y * times,
vectorA.z * times
)
}
dotProduct(vectorA: Vector, vectorB: Vector) { // dot product
return vectorA.x * vectorB.x + vectorA.y * vectorB.y + vectorA.z * vectorB.z
}
vectorProduct(vectorA: Vector, vectorB: Vector) { // cross product
return new Vector(
vectorA.y * vectorB.z - vectorA.z * vectorB.y,
vectorA.z * vectorB.x - vectorA.x * vectorB.z,
vectorA.x * vectorB.y - vectorA.y * vectorB.x,
)
}
}Calculus also plays a role: velocity is the derivative of position, and integration yields displacement. Simple examples of average speed and instantaneous speed illustrate these concepts.
Physical Foundations
To mimic reality, a virtual object must have position, velocity, and acceleration. A basic game object class is defined as:
class GameObj {
pos: Vector
velocity: Vector
acceleration: Vector
constructor(pos?: Vector, velocity?: Vector, acceleration?: Vector) {
this.pos = pos || new Vector(0, 0, 0)
this.velocity = velocity || new Vector(0, 0, 0)
this.acceleration = acceleration || new Vector(0, 0, 0)
}
setPos(pos: Vector) { this.pos = pos }
setVelocity(velocity: Vector) { this.velocity = velocity }
setAcceleration(acceleration: Vector) { this.acceleration = acceleration }
}Newton’s second law links force, mass, and acceleration. Adding a mass property introduces the need to handle zero or infinite mass. The concept of inverse mass (the reciprocal of mass) solves this: setting inverse mass to zero represents an immovable object with infinite mass.
class GameObj {
inverseMess: number // inverse mass, must be >= 0
constructor(inverseMess?: number) {
if (inverseMess >= 0) {
this.inverseMess = inverseMess
}
}
setInverseMess(inverseMess: number) {
if (inverseMess >= 0) {
this.inverseMess = inverseMess
}
}
}Conclusion
We have injected the essential mathematics and physics concepts—vectors, calculus, and Newtonian mechanics—into a rudimentary physics engine. This forms the cornerstone for further features such as gravity, drag, momentum, angular momentum, and collision handling, which will be explored in upcoming articles.
Aotu Lab
Aotu Lab, founded in October 2015, is a front-end engineering team serving multi-platform products. The articles in this public account are intended to share and discuss technology, reflecting only the personal views of Aotu Lab members and not the official stance of JD.com Technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
