BGE/Math

Alias: BGE.Math


Static Methods

createCornerPoints( topLeft: Vector, topRight: Vector, bottomLeft: Vector, bottomRight: Vector, ): CornerPoints

static

Parameters

Returns

isFloatArray44(x: dynamic): boolean

static

Parameters

  • x (dynamic)

Returns

  • boolean

getFloat44Identity(): Array.<Array.<float>>

static

Returns

  • Array.<Array.<float>>

fillFloat44( dest: Array.<Array.<float>>, src: Array.<Array.<float>>, ): void

static

Parameters

  • dest (Array.<Array.<float>>)
  • src (Array.<Array.<float>>)

Returns

  • void

lookAt( from: Vector, lookTo: Vector, up?: Vector, ): Array.<Array.<float>>

static

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>>

static

Parameters

  • top (float)
  • bottom (float)
  • left (float)
  • right (float)
  • far (float)
  • near (float)

Returns

  • Array.<Array.<float>>

getScaleMatrix(scale: Vector): Array.<Array.<float>>

static

Parameters

Returns

  • Array.<Array.<float>>

getRotationMatrix(rotation: Vector): Array.<Array.<float>>

static

Parameters

Returns

  • Array.<Array.<float>>

getTranslationMatrix(position: Vector): Array.<Array.<float>>

static

Parameters

Returns

  • Array.<Array.<float>>

getTransformationMatrix( position: Vector, rotation: Vector, scale: Vector, ): Array.<Array.<float>>

static

Parameters

Returns

  • Array.<Array.<float>>

createTransform(): Transform

static

Returns

boundingSphereOfPoints( points: Array.<BGE.Math.Vector>, ): BGE.Math.BoundingSphere

static

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

static

Unit vector on the ground that heading faces.

Parameters

  • heading (float) — radians

Returns

  • BGE.Math.Vector

rotationForward(rotation: float): BGE.Math.Vector

static

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

static

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

static

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, radians
  • pitch (float) — radians above the ground (negative looks down)

Returns

  • BGE.Math.Vector

headingRight(heading: float): BGE.Math.Vector

static

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

static

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

static

Wraps an angle into -PI..PI.

Parameters

  • angle (float) — radians

Returns

  • float

turnToward(current: float, target: float, maxStep: float): float

static

Turns current toward target by at most maxStep radians, the short way round.

Parameters

  • current (float) — radians
  • target (float) — radians
  • maxStep (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

static

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

static

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

static

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 points
  • target (BGE.Math.Vector)
  • reach (float) — world units; a target exactly at reach is inside
  • halfAngle (float) — radians either side of heading; PI or more is a full circle
  • targetRadius (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

static

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..1
  • cameraHeading (float) — radians, e.g. BGE.OrbitCamera.heading

Returns

  • BGE.Math.Vector — y is 0, length 0..1

Min(a: float, b: float): float

static

Parameters

  • a (float)
  • b (float)

Returns

  • float

Max(a: float, b: float): float

static

Parameters

  • a (float)
  • b (float)

Returns

  • float

Clamp(number: float, minVal: float, maxVal: float): float

static

Parameters

  • number (float)
  • minVal (float)
  • maxVal (float)

Returns

  • float

wrapValue(value: float, range: float): float

static

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

static

Parameters

  • y (float)
  • x (float)

Returns

  • float

IsIntegerEven(number: integer): boolean

static

Parameters

  • number (integer)

Returns

  • boolean

IsIntegerOdd(number: integer): boolean

static

Parameters

  • number (integer)

Returns

  • boolean

Power(number: float, pow: integer): float

static

Parameters

  • number (float)
  • pow (integer)

Returns

  • float

Round(number: float, decimals?: integer): float

static

Parameters

  • number (float)
  • decimals (integer, optional, default: 0)

Returns

  • float

DegreesToRadians(degrees: float): float

static

Parameters

  • degrees (float)

Returns

  • float

RadiansToDegrees(radians: float): float

static

Parameters

  • radians (float)

Returns

  • float

RandomInRange(lowestInt: integer, highestInt: integer): integer

static

Parameters

  • lowestInt (integer)
  • highestInt (integer)

Returns

  • integer

randomFloat(min: float, max: float): float

static

A random float from min up to (not including) max.

Parameters

  • min (float)
  • max (float)

Returns

  • float

randomSign(): integer

static

1 or -1, at random - e.g. m.velocity.y = 300 * BGE.Math.randomSign().

Returns

  • integer

hash2(x: float, y: float, salt?: float): float

static

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

CODE
jitterX = (BGE.Math.hash2(col, row, 1) - 0.5) * cellSize
treeIndex = Int(BGE.Math.hash2(col, row, 2) * trees.Count())

constrainAngle(angleRad: float): float

static

Parameters

  • angleRad (float)

Returns

  • float

RotateVectorAroundPoint2d( vector1: Vector, point: Vector, radians: float, ): Vector

static

Parameters

Returns

RotateVectorAroundPoint3d( vector1: Vector, point: Vector, axis: Vector, radians: float, ): Vector

static

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

static

Parameters

Returns

  • float

GetAngle(point1: Vector, point2: Vector): float

static

Parameters

Returns

  • float

GetAngleBetweenVectors( center: vector, point1: Vector, point2: Vector, ): float

static

Parameters

Returns

  • float

GetAngleAtPoint3d( center: Vector, point1: Vector, point2: Vector, ): float

static

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>

static

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

static

Parameters

  • num1 (float)
  • num2 (float)

Returns

  • float

arraySum(nums: Array.<float>): float

static

Parameters

  • nums (Array.<float>)

Returns

  • float

arrayAverage(nums: Array.<float>): float

static

Parameters

  • nums (Array.<float>)

Returns

  • float

arrayMin(nums: Array.<float>): float

static

Parameters

  • nums (Array.<float>)

Returns

  • float

arrayMax(nums: Array.<float>): float

static

Parameters

  • nums (Array.<float>)

Returns

  • float

lerp(a: float, b: float, t: float): float

static

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

static

Parameters

Returns

  • BGE.Math.Vector

signedDistanceFromPointToPlane( plane: Plane, targetPoint: BGE.Math.Vector, ): float

static

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

static

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

static

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

static

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

static

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

static

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

static

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 space
  • textureCenterWorldPoint (BGE.Math.Vector) — the world-space point that should map to the texture's center
  • textureWidth (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

static

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 centre
  • velocity (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 height
  • openEdges (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

CODE
' 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 if

hasRectEdge( edges: Array.<BGE.Math.RectEdge>, edge: BGE.Math.RectEdge, ): boolean

static

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

static

Moves a body that pokes out of rect back inside it. Updates position in place.

Parameters

  • position (BGE.Math.Vector) — the body's centre
  • rect (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

CODE
' 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

static

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

CODE
if BGE.Math.wrapInRect(m.position, m.screenRect) and m.dieOffScreen
m.invalidate()
end if

VectorZero(): BGE.Math.Vector

static

Returns

  • BGE.Math.Vector

VectorFromFloatArray(array: Array.<float>): BGE.Math.Vector

static

Parameters

  • array (Array.<float>)

Returns

  • BGE.Math.Vector

createScaleVector( scale_x: float, scale_y?: dynamic, scale_z?: dynamic, ): BGE.Math.Vector

static

Parameters

  • scale_x (float) — horizontal scale
  • scale_y (dynamic, optional, default: "invalid") — vertical scale, or if invalid, use the horizontal scale as vertical scale
  • scale_z (dynamic, optional, default: "invalid")

Returns

  • BGE.Math.Vector

distanceFromPlane( planePoint: BGE.Math.Vector, planeNormal: BGE.Math.Vector, targetPoint: BGE.Math.Vector, ): float

static

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

static

Parameters

  • a (BGE.Math.Vector)
  • b (BGE.Math.Vector)

Returns

  • BGE.Math.Vector

averagePoint(points: Array.<BGE.Math.Vector>): BGE.Math.Vector

static

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>

static

Parameters

  • points (Array.<BGE.Math.Vector>)

Returns

  • Array.<BGE.Math.Vector>

boundsOverlapRect( points: Array.<BGE.Math.Vector>, width: float, height: float, ): boolean

static

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>

static

Parameters

  • points (Array.<BGE.Math.Vector>)

Returns

  • Array.<BGE.Math.Vector>

vectorArraysEqual( a?: Array.<BGE.Math.Vector>, b?: Array.<BGE.Math.Vector>, ): boolean

static

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>

static

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

static

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

static

Parameters

  • p1 (BGE.Math.Vector)
  • angle1FromXAxis (float)
  • p2 (BGE.Math.Vector)
  • angle2FromXAxis (float)

Returns

  • BGE.Math.Vector

Enums

BGE.Math.WindingDirection

enumstaticreadonly

Properties

  • clockwise (default: 0)
  • counterclockwise (default: 1)

BGE.Math.RectEdge

enumstaticreadonly

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

static

Properties

  • topLeft (dynamic)
  • topRight (dynamic)
  • bottomLeft (dynamic)
  • bottomRight (dynamic)

Transform

static

Properties

  • position (BGE.Math.Vector)
  • rotation (BGE.Math.vector)
  • scale (BGE.Math.Vector)

Triangle

static

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

static

Extends: roAssociativeArray

A sphere in world space: centre (x, y, z) and radius.

Properties

  • x (float)
  • y (float)
  • z (float)
  • radius (float)

Circle

static

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

staticreadonly

Default: 3.14159265358

HALF_PI

staticreadonly

Default: 1.57079632679

Plane

static

Properties

Ray

static

Properties

Rectangle

static

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

static

Properties

  • x (float)
  • y (float)
  • z (float)