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
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 ifStatic Methods
pickInView(
candidates: Array.<BGE.TargetCandidate>,
from: BGE.Math.Vector,
viewHeading: float,
range: float,
halfAngle: float,
options?: BGE.TargetingOptions,
): BGE.TargetCandidate
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 positionviewHeading(float) — radians, e.g. the camera's headingrange(float) — world units; a candidate exactly at range countshalfAngle(float) — radians either side of viewHeading; PI or more is all roundoptions(BGE.TargetingOptions, optional, default: "{}")
Returns
BGE.TargetCandidate— or invalid if none qualifies
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
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 positionside(float) — 1 for right, -1 for leftviewHeading(float) — radiansrange(float) — world unitshalfAngle(float) — radians either side of viewHeadingoptions(BGE.TargetingOptions, optional, default: "{}") — onlyplaneis used
Returns
BGE.TargetCandidate— or invalid if there's none that way
bearing(
from: BGE.Math.Vector,
target: BGE.Math.Vector,
plane?: BGE.SeparationPlane,
): float
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
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