Game Math Documentation
A handy collection of mathematical functions and constants for game development.
Math Methods
b8.Math.dist2D(x0, y0, x1, y1)
Calculates a 2D distance between points (x0, y0) and (x1, y1).
Parameters
- x0 (number): The x-coordinate of the first point.
- y0 (number): The y-coordinate of the first point.
- x1 (number): The x-coordinate of the second point.
- y1 (number): The y-coordinate of the second point.
Returns
- (number): The distance between the two points.
b8.Math.distManhattan(a, b)
Calculate the Manhattan distance between two locations.
The Manhattan distance is the sum of the absolute differences of their Cartesian coordinates. It represents the distance traveled along axes at right angles (like navigating a grid). It’s not a straight-line distance, but rather the total number of steps required to move from one point to another when only moving horizontally and vertically.
Parameters
- a (Object): The first location with col and row properties.
- b (Object): The second location with col and row properties.
Returns
- (number): The Manhattan distance between the two locations.
b8.Math.fade(t)
A smoothing function for interpolation.
This is Perlin’s classic fade function 6t^5 - 15t^4 + 10t^3. It eases coordinate values so that they will ease towards integral values. This ends up smoothing the final output.
Parameters
- t (number): The interpolation factor (0.0 to 1.0).
Returns
- (number): The smoothed interpolation factor.
b8.Math.lerp(a, b, t)
Linearly interpolates between two values.
Parameters
- a (number): The start value.
- b (number): The end value.
- t (number): The interpolation factor (0.0 to 1.0).
Returns
- (number): The interpolated value.
b8.Math.smoothstep(t)
A simpler smoothing function for interpolation.
This is the smoothstep function 3t^2 - 2t^3. It eases coordinate values so that they will ease towards integral values. This ends up smoothing the final output.
Parameters
- t (number): The interpolation factor (0.0 to 1.0).
Returns
- (number): The smoothed interpolation factor.