BGE/Math
Alias: BGE.Math
Static Methods
createCornerPoints(
topLeft: Vector,
topRight: Vector,
bottomLeft: Vector,
bottomRight: Vector,
): CornerPoints
Parameters
Returns
isFloatArray44(x: dynamic): boolean
Parameters
x(dynamic)
Returns
boolean
getFloat44Identity(): Array.<Array.<float>>
Returns
Array.<Array.<float>>
fillFloat44(
dest: Array.<Array.<float>>,
src: Array.<Array.<float>>,
): void
Parameters
dest(Array.<Array.<float>>)src(Array.<Array.<float>>)
Returns
void
lookAt(
from: Vector,
lookTo: Vector,
up?: Vector,
): Array.<Array.<float>>
Builds a camera-to-world basis matrix looking from from toward lookTo, using up to resolve the remaining roll ambiguity around the view axis - defaults to world-up, matching every call site that doesn't care about roll (Camera2d, and Camera3d before roll support existed).
Parameters
Returns
Array.<Array.<float>>
orthographicMatrix(
top: float,
bottom: float,
left: float,
right: float,
far: float,
near: float,
): Array.<Array.<float>>
Parameters
top(float)bottom(float)left(float)right(float)far(float)near(float)
Returns
Array.<Array.<float>>
getScaleMatrix(scale: Vector): Array.<Array.<float>>
Parameters
scale(Vector)
Returns
Array.<Array.<float>>
getRotationMatrix(rotation: Vector): Array.<Array.<float>>
Parameters
rotation(Vector)
Returns
Array.<Array.<float>>
getTranslationMatrix(position: Vector): Array.<Array.<float>>
Parameters
position(Vector)
Returns
Array.<Array.<float>>
getTransformationMatrix(
position: Vector,
rotation: Vector,
scale: Vector,
): Array.<Array.<float>>
Parameters
Returns
Array.<Array.<float>>
createTransform(): Transform
Returns
boundingSphereOfPoints(
points: Array.<BGE.Math.Vector>,
): BGE.Math.BoundingSphere
A sphere holding every point: centred on their average, out to the farthest one. Not the smallest such sphere, but quick. The radius is padded a little, so a test against it errs towards "maybe" rather than flipping on rounding.
Parameters
points(Array.<BGE.Math.Vector>) — at least one
Returns
BGE.Math.BoundingSphere
headingForward(heading: float): BGE.Math.Vector
Unit vector on the ground that heading faces.
Parameters
heading(float) — radians
Returns
BGE.Math.Vector
rotationForward(rotation: float): BGE.Math.Vector
Unit vector on the ground that a drawable or model built facing +z (a wall image, a model's front) faces once turned by y rotation rotation: (-sin(rotation), 0, cos(rotation)). Not the heading convention: a rotation of a quarter turn faces -x, where that heading faces +x.
Parameters
rotation(float) — its rotation.y, radians
Returns
BGE.Math.Vector
rotationFacing(direction: BGE.Math.Vector): float
The y rotation that turns a drawable or model built facing +z to face direction on the ground (see rotationForward()); its y is ignored.
Parameters
direction(BGE.Math.Vector)
Returns
float— radians, -PI..PI
directionFromYawPitch(yaw: float, pitch: float): BGE.Math.Vector
Unit vector that faces yaw (a heading) tilted up by pitch - e.g. where a sun or moon sits in the sky. Negate it for the way its light travels.
Parameters
yaw(float) — heading, radianspitch(float) — radians above the ground (negative looks down)
Returns
BGE.Math.Vector
headingRight(heading: float): BGE.Math.Vector
Unit vector on the ground to the right of heading (forward turned a quarter clockwise).
Parameters
heading(float) — radians
Returns
BGE.Math.Vector
headingOf(direction: BGE.Math.Vector): float
The heading a direction faces on the ground; its y is ignored.
Parameters
direction(BGE.Math.Vector)
Returns
float— radians, -PI..PI
wrapAngle(angle: float): float
Wraps an angle into -PI..PI.
Parameters
angle(float) — radians
Returns
float
turnToward(current: float, target: float, maxStep: float): float
Turns current toward target by at most maxStep radians, the short way round.
Parameters
current(float) — radianstarget(float) — radiansmaxStep(float) — radians, e.g. turnSpeed * dt
Returns
float— the new heading (lands on target's direction when within maxStep)
flatDistance(a: BGE.Math.Vector, b: BGE.Math.Vector): float
Distance between two points on the ground, ignoring height.
Parameters
a(BGE.Math.Vector)b(BGE.Math.Vector)
Returns
float
flatDirection(
a: BGE.Math.Vector,
b: BGE.Math.Vector,
): BGE.Math.Vector
Unit vector on the ground from a toward b, ignoring height. Gives heading 0's forward when the points are (almost) on top of each other.
Parameters
a(BGE.Math.Vector)b(BGE.Math.Vector)
Returns
BGE.Math.Vector
isWithinArc(
origin: BGE.Math.Vector,
heading: float,
target: BGE.Math.Vector,
reach: float,
halfAngle: float,
targetRadius?: float,
): boolean
True when target is inside the ground-plane wedge reaching reach from origin, up to halfAngle either side of heading: the shape Renderer.drawWorldArc(origin, reach, heading - halfAngle, heading + halfAngle, rgba, true) draws. Heights are ignored. E.g. a melee swing's hit test.
Parameters
origin(BGE.Math.Vector)heading(float) — radians, where the wedge pointstarget(BGE.Math.Vector)reach(float) — world units; a target exactly at reach is insidehalfAngle(float) — radians either side of heading; PI or more is a full circletargetRadius(float, optional, default: 0) — the target's body radius, added to reach so a body counts as soon as its edge is in reach (the angle is still measured to its centre)
Returns
boolean— true if inside; a target on top of the origin always is
cameraRelativeMove(
stick: BGE.Math.Vector,
cameraHeading: float,
): BGE.Math.Vector
Turns a move stick into a direction on the ground relative to a camera: stick up (+y) is the way the camera looks, stick right (+x) is the camera's right. Its length follows the stick's, clamped to 1, so a partly pushed stick still gives a slower move.
Parameters
stick(BGE.Math.Vector) — x right, y up, each -1..1cameraHeading(float) — radians, e.g. BGE.OrbitCamera.heading
Returns
BGE.Math.Vector— y is 0, length 0..1
Min(a: float, b: float): float
Parameters
a(float)b(float)
Returns
float
Max(a: float, b: float): float
Parameters
a(float)b(float)
Returns
float
Clamp(number: float, minVal: float, maxVal: float): float
Parameters
number(float)minVal(float)maxVal(float)
Returns
float
wrapValue(value: float, range: float): float
Wraps value into the half-open range [0, range) - correct for a negative value too, unlike a raw MOD, which can return a negative result in BrightScript for a negative left-hand operand. Used to map an unbounded world-space anchor point back into a single repeating tile's own pixel bounds (see SceneObjectPlane's tiledImage fill mode).
Parameters
value(float)range(float)
Returns
float
Atan2(y: float, x: float): float
Parameters
y(float)x(float)
Returns
float
IsIntegerEven(number: integer): boolean
Parameters
number(integer)
Returns
boolean
IsIntegerOdd(number: integer): boolean
Parameters
number(integer)
Returns
boolean
Power(number: float, pow: integer): float
Parameters
number(float)pow(integer)
Returns
float
Round(number: float, decimals?: integer): float
Parameters
number(float)decimals(integer, optional, default: 0)
Returns
float
DegreesToRadians(degrees: float): float
Parameters
degrees(float)
Returns
float
RadiansToDegrees(radians: float): float
Parameters
radians(float)
Returns
float
RandomInRange(lowestInt: integer, highestInt: integer): integer
Parameters
lowestInt(integer)highestInt(integer)
Returns
integer
randomFloat(min: float, max: float): float
A random float from min up to (not including) max.
Parameters
min(float)max(float)
Returns
float
randomSign(): integer
1 or -1, at random - e.g. m.velocity.y = 300 * BGE.Math.randomSign().
Returns
integer
hash2(x: float, y: float, salt?: float): float
A repeatable random float in [0, 1) for a grid cell or point: the same x, y and salt always give the same value, so a level can scatter things the same way every time without storing them. Use a different salt for each thing you pick per cell.
Parameters
x(float)y(float)salt(float, optional, default: "0.0")
Returns
float
Example
jitterX = (BGE.Math.hash2(col, row, 1) - 0.5) * cellSize
treeIndex = Int(BGE.Math.hash2(col, row, 2) * trees.Count())constrainAngle(angleRad: float): float
Parameters
angleRad(float)
Returns
float
RotateVectorAroundPoint2d(
vector1: Vector,
point: Vector,
radians: float,
): Vector
Parameters
Returns
RotateVectorAroundPoint3d(
vector1: Vector,
point: Vector,
axis: Vector,
radians: float,
): Vector
Rotates vector1 around point in 3D by radians about the provided axis. axis does not need to be normalized.
Parameters
Returns
TotalDistance(vector1: Vector, vector2: Vector): float
Parameters
Returns
float
GetAngle(point1: Vector, point2: Vector): float
Parameters
Returns
float
GetAngleBetweenVectors(
center: vector,
point1: Vector,
point2: Vector,
): float
Parameters
Returns
float
GetAngleAtPoint3d(
center: Vector,
point1: Vector,
point2: Vector,
): float
Returns the angle at center between legs center->point1 and center->point2 in radians. Uses atan2(|u x v|, u · v) for stable 3D angle calculation in [0, PI].
Parameters
Returns
float
GetSliceAnglesAtPoint3d(
centerPoint: Vector,
lineStart: Vector,
lineEnd: Vector,
sliceLength: float,
includeRemainder?: boolean,
): Array.<float>
Returns one theta (radians) per slice along the segment from lineStart to lineEnd, where each theta is measured at centerPoint between consecutive slice endpoints.
Parameters
centerPoint(Vector)lineStart(Vector)lineEnd(Vector)sliceLength(float)includeRemainder(boolean, optional, default: false)
Returns
Array.<float>
average(num1: float, num2: float): float
Parameters
num1(float)num2(float)
Returns
float
arraySum(nums: Array.<float>): float
Parameters
nums(Array.<float>)
Returns
float
arrayAverage(nums: Array.<float>): float
Parameters
nums(Array.<float>)
Returns
float
arrayMin(nums: Array.<float>): float
Parameters
nums(Array.<float>)
Returns
float
arrayMax(nums: Array.<float>): float
Parameters
nums(Array.<float>)
Returns
float
lerp(a: float, b: float, t: float): float
Linearly interpolates between two scalars.
Parameters
a(float) — start value (returned at t=0)b(float) — end value (returned at t=1)t(float) — interpolation factor, typically 0-1 (not clamped - values outside 0-1 extrapolate past a/b)
Returns
float— the interpolated value
intersectRayWithPlane(plane: Plane, ray: Ray): BGE.Math.Vector
Parameters
Returns
BGE.Math.Vector
signedDistanceFromPointToPlane(
plane: Plane,
targetPoint: BGE.Math.Vector,
): float
Returns signed distance from a point to a plane. Positive/negative sign indicates which side of the plane normal the point is on.
Parameters
plane(Plane)targetPoint(BGE.Math.Vector)
Returns
float
distanceFromPointToPlane(
plane: Plane,
targetPoint: BGE.Math.Vector,
): float
Returns the absolute shortest distance from a point to a plane.
Parameters
plane(Plane)targetPoint(BGE.Math.Vector)
Returns
float
angleToPlaneSurfaceDeg(
plane: BGE.Math.Plane,
v: BGE.Math.Vector,
): float
0..90 deg 0 = parallel to plane, 90 = perpendicular to plane
Parameters
plane(BGE.Math.Plane)v(BGE.Math.Vector)
Returns
float
angleToPlaneNormalDeg(
plane: BGE.Math.Plane,
v: BGE.Math.Vector,
): float
0..180 deg 0 = +normal, 180 = -normal, 90 = parallel to plane
Parameters
plane(BGE.Math.Plane)v(BGE.Math.Vector)
Returns
float
closestPointOnPlane(
plane: BGE.Math.Plane,
point: BGE.Math.Vector,
): BGE.Math.Vector
Parameters
plane(BGE.Math.Plane)point(BGE.Math.Vector)
Returns
BGE.Math.Vector
projectRayOntoPlane(
plane: BGE.Math.Plane,
ray: BGE.Math.Ray,
): BGE.Math.Ray
Parameters
plane(BGE.Math.Plane)ray(BGE.Math.Ray)
Returns
BGE.Math.Ray
worldPointToTexturePixel(
worldPoint: BGE.Math.Vector,
textureCenterWorldPoint: BGE.Math.Vector,
textureWidth: float,
textureHeight: float,
worldUnitsPerPixel?: float,
): BGE.Math.Vector
Maps a world-space point on a plane to pixel coordinates within a textureWidth x textureHeight texture, anchored so that textureCenterWorldPoint corresponds to the texture's own center pixel (rather than world (0,0) mapping to the texture's (0,0) pixel, which would pin the texture to a fixed absolute-world-space footprint regardless of where the plane/camera actually are).
Parameters
worldPoint(BGE.Math.Vector) — a point on the plane, in world spacetextureCenterWorldPoint(BGE.Math.Vector) — the world-space point that should map to the texture's centertextureWidth(float)textureHeight(float)worldUnitsPerPixel(float, optional, default: "1.0") — how many world units each texture pixel covers
Returns
BGE.Math.Vector— pixel coordinates within the texture (z unused)
bounceInRect(
position: BGE.Math.Vector,
velocity: BGE.Math.Vector,
rect: BGE.Math.Rectangle,
halfWidth?: float,
halfHeight?: float,
openEdges?: Array.<BGE.Math.RectEdge>,
): BGE.Math.RectEdge
Bounces a body off the edges of rect: a body past an edge is moved back onto it and, if it was heading out, its velocity on that axis flips. Updates position and velocity in place.
Parameters
position(BGE.Math.Vector) — the body's centrevelocity(BGE.Math.Vector)rect(BGE.Math.Rectangle)halfWidth(float, optional, default: "0.0") — half the body's width (its radius, for a ball)halfHeight(float, optional, default: "0.0") — half the body's heightopenEdges(Array.<BGE.Math.RectEdge>, optional, default: "[]") — edges the body passes through, e.g. the goal ends in pong
Returns
BGE.Math.RectEdge— the edge it bounced off (the vertical one, if it hit a corner), or none
Example
' In onUpdate: a ball that can fall out of the bottom
edge = BGE.Math.bounceInRect(m.position, m.velocity, m.playArea, radius, radius, [BGE.Math.RectEdge.bottom])
if edge <> BGE.Math.RectEdge.none
m.game.playSound("bounce")
end ifhasRectEdge(
edges: Array.<BGE.Math.RectEdge>,
edge: BGE.Math.RectEdge,
): boolean
Whether edges includes edge.
Parameters
edges(Array.<BGE.Math.RectEdge>)edge(BGE.Math.RectEdge)
Returns
boolean
clampToRect(
position: BGE.Math.Vector,
rect: BGE.Math.Rectangle,
halfWidth?: float,
halfHeight?: float,
): boolean
Moves a body that pokes out of rect back inside it. Updates position in place.
Parameters
position(BGE.Math.Vector) — the body's centrerect(BGE.Math.Rectangle)halfWidth(float, optional, default: "0.0")halfHeight(float, optional, default: "0.0")
Returns
boolean— true if it had to be moved
Example
' In onUpdate: keep the player on screen
BGE.Math.clampToRect(m.position, m.screenRect, radius, radius)wrapInRect(
position: BGE.Math.Vector,
rect: BGE.Math.Rectangle,
): boolean
Wraps a point that left rect around to the opposite edge, like the ships and rocks in Asteroids. Updates position in place.
Parameters
position(BGE.Math.Vector)rect(BGE.Math.Rectangle)
Returns
boolean— true if it wrapped
Example
if BGE.Math.wrapInRect(m.position, m.screenRect) and m.dieOffScreen
m.invalidate()
end ifVectorZero(): BGE.Math.Vector
Returns
BGE.Math.Vector
VectorFromFloatArray(array: Array.<float>): BGE.Math.Vector
Parameters
array(Array.<float>)
Returns
BGE.Math.Vector
createScaleVector(
scale_x: float,
scale_y?: dynamic,
scale_z?: dynamic,
): BGE.Math.Vector
Parameters
scale_x(float) — horizontal scalescale_y(dynamic, optional, default: "invalid") — vertical scale, or if invalid, use the horizontal scale as vertical scalescale_z(dynamic, optional, default: "invalid")
Returns
BGE.Math.Vector
distanceFromPlane(
planePoint: BGE.Math.Vector,
planeNormal: BGE.Math.Vector,
targetPoint: BGE.Math.Vector,
): float
Parameters
planePoint(BGE.Math.Vector)planeNormal(BGE.Math.Vector)targetPoint(BGE.Math.Vector)
Returns
float
midPointBetweenPoints(
a: BGE.Math.Vector,
b: BGE.Math.Vector,
): BGE.Math.Vector
Parameters
a(BGE.Math.Vector)b(BGE.Math.Vector)
Returns
BGE.Math.Vector
averagePoint(points: Array.<BGE.Math.Vector>): BGE.Math.Vector
The centroid of a set of points - their component-wise average. Note this is not the same as the center of their bounding box (see getBounds).
Parameters
points(Array.<BGE.Math.Vector>)
Returns
BGE.Math.Vector— the average of every point, or the origin if given no points
getBounds(
points: Array.<BGE.Math.Vector>,
): Array.<BGE.Math.Vector>
Parameters
points(Array.<BGE.Math.Vector>)
Returns
Array.<BGE.Math.Vector>
boundsOverlapRect(
points: Array.<BGE.Math.Vector>,
width: float,
height: float,
): boolean
Checks if the (x, y) bounding box of points overlaps the axis-aligned rectangle [0, width] x [0, height] at all.
Parameters
points(Array.<BGE.Math.Vector>)width(float)height(float)
Returns
boolean
getBoundingCubePoints(
points: Array.<BGE.Math.Vector>,
): Array.<BGE.Math.Vector>
Parameters
points(Array.<BGE.Math.Vector>)
Returns
Array.<BGE.Math.Vector>
vectorArraysEqual(
a?: Array.<BGE.Math.Vector>,
b?: Array.<BGE.Math.Vector>,
): boolean
Parameters
a(Array.<BGE.Math.Vector>, optional, default: "[]")b(Array.<BGE.Math.Vector>, optional, default: "[]")
Returns
boolean
vectorArrayCopy(
a?: Array.<BGE.Math.Vector>,
): Array.<BGE.Math.Vector>
Parameters
a(Array.<BGE.Math.Vector>, optional, default: "[]")
Returns
Array.<BGE.Math.Vector>
intersectPointPoint(
p1: Vector,
p2: Vector,
p3: Vector,
p4: Vector,
mustBeInSegment?: boolean,
): BGE.Math.Vector
From https://paulbourke.net/geometry/pointlineplane/javascript.txt
Parameters
Returns
BGE.Math.Vector
intersectPointAngle(
p1: BGE.Math.Vector,
angle1FromXAxis: float,
p2: BGE.Math.Vector,
angle2FromXAxis: float,
): BGE.Math.Vector
Parameters
p1(BGE.Math.Vector)angle1FromXAxis(float)p2(BGE.Math.Vector)angle2FromXAxis(float)
Returns
BGE.Math.Vector
Enums
BGE.Math.WindingDirection
Properties
clockwise(default: 0)counterclockwise(default: 1)
BGE.Math.RectEdge
An edge of a rectangle - see bounceInRect().
Properties
none(default: "none")left(default: "left")right(default: "right")bottom(default: "bottom")top(default: "top")
Other
CornerPoints
Properties
topLeft(dynamic)topRight(dynamic)bottomLeft(dynamic)bottomRight(dynamic)
Transform
Properties
position(BGE.Math.Vector)rotation(BGE.Math.vector)scale(BGE.Math.Vector)
Triangle
Properties
lengths(Array.<float>)angles(Array.<float>)anglesToNext(Array.<float>)anglesToPrevious(Array.<float>)points(Array.<BGE.Math.Vector>)longestIndex(integer)height(float)winding(WindingDirection)angleRotatedFromOriginal(float)
BoundingSphere
Extends: roAssociativeArray
A sphere in world space: centre (x, y, z) and radius.
Properties
x(float)y(float)z(float)radius(float)
Circle
Parameters
x(float, optional, default: 0)y(float, optional, default: 0)radius(float, optional, default: 0)
Properties
x(float)y(float)radius(float)
Returns
BGE.Math.Circle
PI
Default: 3.14159265358
HALF_PI
Default: 1.57079632679
Plane
Properties
Ray
Properties
Rectangle
Parameters
x(float, optional, default: 0)y(float, optional, default: 0)width(float, optional, default: 0)height(float, optional, default: 0)
Properties
x(float)y(float)width(float)height(float)
Returns
BGE.Math.Rectangle
Vector
Properties
x(float)y(float)z(float)