An immutable-by-convention 3D vector with full operator overloading.

var a = new Vector3(1, 2, 3);
var b = a + new Vector3(0, 0, 1);
var away = (b - a).normalized() * 5.0;

Every method is inline, and the underlying type is a bare structure, so none of this survives into the generated Lua as a wrapper object — the arithmetic compiles down to direct field reads on plain tables.

Values coming back from natives can be adopted directly:

var coords:Vector3 = cast Natives.entity.getEntityCoords(ped, true);

and values going into natives are passed component-wise (v.x, v.y, v.z), which every native signature in this library accepts.

Static variables

staticread onlylength:Float

staticread onlylengthSquared:Float

Squared length. Prefer this over length when only comparing distances.

Static methods

@:op(A + B)staticinlineadd(this:Vector3Data, rhs:Vector3):Vector3

staticinlineclampLength(this:Vector3Data, max:Float):Vector3

Clamps the vector's length to at most max, keeping its direction.

staticinlinecopy(this:Vector3Data):Vector3

staticinlinecross(this:Vector3Data, rhs:Vector3):Vector3

staticinlinedistance(this:Vector3Data, rhs:Vector3):Float

staticinlinedistance2d(this:Vector3Data, rhs:Vector3):Float

Distance ignoring height — the right choice for "is the player near this spot" checks.

staticinlinedistanceSquared(this:Vector3Data, rhs:Vector3):Float

@:op(A / B)staticinlinedivide(this:Vector3Data, factor:Float):Vector3

staticinlinedot(this:Vector3Data, rhs:Vector3):Float

@:op(A == B)staticinlineequals(this:Vector3Data, rhs:Vector3):Bool

staticinlineforward():Vector3

staticinlinefromArray(values:Array<Float>):Vector3

Builds a vector from a [x, y, z] array.

staticinlinefromHeading(degrees:Float):Vector3

Builds a heading-aligned unit vector, using the same convention as the game's entity headings: 0° faces north (+Y), increasing clockwise.

staticinlinefromNative(value:Dynamic):Vector3

Adopts a value returned by a native (a Lua vector3, or any table with x/y/z) as a Vector3. Returns null for a null input, which is what natives hand back when the entity or query had no result.

staticinlinelerp(this:Vector3Data, target:Vector3, t:Float):Vector3

@:op(A * B)staticinlinemul(this:Vector3Data, rhs:Vector3):Vector3

Component-wise product (Hadamard), not dot or cross.

@:value({ epsilon : 0.001 })staticinlinenearlyEquals(this:Vector3Data, rhs:Vector3, epsilon:Float = 0.001):Bool

Compares component-wise within epsilon, for tolerating float drift.

@:op(-A)staticinlinenegate(this:Vector3Data):Vector3

staticinlinenormalized(this:Vector3Data):Vector3

Unit-length copy. A zero vector is returned unchanged rather than producing NaNs.

@:op(A != B)staticinlinenotEquals(this:Vector3Data, rhs:Vector3):Bool

staticinlineone():Vector3

staticinlineright():Vector3

@:op(A * B)@:commutativestaticinlinescale(this:Vector3Data, factor:Float):Vector3

@:op(A - B)staticinlinesub(this:Vector3Data, rhs:Vector3):Vector3

staticinlinetoArray(this:Vector3Data):Array<Float>

staticinlinetoHeading(this:Vector3Data):Float

The game heading (degrees, 0 = north, clockwise) this vector points in. The Z component is ignored.

@:has_untypedstaticinlinetoNative(this:Vector3Data):Dynamic

A real Lua vector3, for the rare native or resource export that demands one.

staticinlinetoString(this:Vector3Data):String

staticinlinetoVector2(this:Vector3Data):Vector2

@:value({ w : 0 })staticinlinetoVector4(this:Vector3Data, w:Float = 0):Vector4

staticinlineup():Vector3

staticinlinewithZ(this:Vector3Data, z:Float):Vector3

staticinlinezero():Vector3