BGE/Targeting

Picks targets for lock-on, auto-aim or homing: which candidate is roughly where the player looks, and which is next round to the left or right. Pure functions over positions; filter the candidates (alive, visible, hostile) before passing them in.

Headings follow BGE.Math's convention (heading 0 faces -z, clockwise from above), so a Camera3d or OrbitCamera heading can be passed straight in.

Alias: BGE.Targeting

Example

CODE
living = []
for each foe in m.enemies
if foe.isAlive()
living.push(foe)
end if
end for
target = BGE.Targeting.pickInView(living, m.position, m.cameraRig.heading, 480, BGE.Math.PI * 0.4)
if invalid <> target
m.cameraRig.lockOn(target)
end if

Static Methods

pickInView( candidates: Array.<BGE.TargetCandidate>, from: BGE.Math.Vector, viewHeading: float, range: float, halfAngle: float, options?: BGE.TargetingOptions, ): BGE.TargetCandidate

static

The candidate within range of from and halfAngle radians of viewHeading that's nearest, counting one straight ahead as closer (see BGE.TargetingOptions.angleWeight).

Parameters

  • candidates (Array.<BGE.TargetCandidate>)
  • from (BGE.Math.Vector) — e.g. the player's position
  • viewHeading (float) — radians, e.g. the camera's heading
  • range (float) — world units; a candidate exactly at range counts
  • halfAngle (float) — radians either side of viewHeading; PI or more is all round
  • options (BGE.TargetingOptions, optional, default: "{}")

Returns

nextByBearing( candidates: Array.<BGE.TargetCandidate>, from: BGE.Math.Vector, current: BGE.Math.Vector, side: float, viewHeading: float, range: float, halfAngle: float, options?: BGE.TargetingOptions, ): BGE.TargetCandidate

static

The candidate whose bearing from from is next round from current's, to the right (side 1, clockwise) or left (-1): for switching lock-on targets with a stick flick. Only candidates within range and halfAngle of viewHeading count. current itself, and any candidate at exactly its bearing, is skipped.

Parameters

  • candidates (Array.<BGE.TargetCandidate>)
  • from (BGE.Math.Vector)
  • current (BGE.Math.Vector) — the current target's position
  • side (float) — 1 for right, -1 for left
  • viewHeading (float) — radians
  • range (float) — world units
  • halfAngle (float) — radians either side of viewHeading
  • options (BGE.TargetingOptions, optional, default: "{}") — only plane is used

Returns

bearing( from: BGE.Math.Vector, target: BGE.Math.Vector, plane?: BGE.SeparationPlane, ): float

static

The heading from from to target on plane. 0 when they're (almost) on top of each other.

Parameters

  • from (BGE.Math.Vector)
  • target (BGE.Math.Vector)
  • plane (BGE.SeparationPlane, optional, default: "BGE.SeparationPlane.xz")

Returns

  • float — radians, -PI..PI

distance( from: BGE.Math.Vector, target: BGE.Math.Vector, plane?: BGE.SeparationPlane, ): float

static

Distance from from to target on plane, ignoring the third axis.

Parameters

  • from (BGE.Math.Vector)
  • target (BGE.Math.Vector)
  • plane (BGE.SeparationPlane, optional, default: "BGE.SeparationPlane.xz")

Returns

  • float