Mathematical Operators
Operators · Physics calculations (gravity, velocity, collision).
Mathematical Operators
What it is
JavaScript’s arithmetic operators are +, -, *, /, % (modulo), and ** (exponentiation). They follow standard precedence rules and operate on numbers (and, for +, strings). The Math object provides supporting functions like Math.round(), Math.floor(), and Math.sqrt().
Why it matters
Physics is arithmetic. Position updates are addition. Gravity is acceleration applied via addition each frame. Collision response often involves subtraction (move out of overlap) and multiplication (scale velocity). Modulo wraps sprite animation indexes.
How GateGame implements it
Inside spline(), multiplication scales relative coordinates to canvas pixels: px * width. Math.round() converts the resulting float to a clean integer for crisp rendering. The engine’s physics adds velocity to position each frame, applies gravity by adding to velocity, and uses subtraction to push entities out of overlapping barriers.
Code Example
// Math operators in spline(): multiply to scale, round to integer
splinePoints: points.map(([px, py]) => ({
x: Math.round(px * width), // multiply, then round
y: Math.round(py * height)
})),
// Physics-style example: gravity + velocity
this.y += this.vy; // position += velocity
this.vy += gravity; // velocity += gravity (each frame)
Key Takeaway
Math operators are the verbs of physics. Position moves by addition, gravity accumulates into velocity, scaling happens by multiplication.