UserFunction; Vec3 - HWRM/KarosGraveyard GitHub Wiki

Vec3(<initial_value>)

By Novaras aka Fear.


📦 Get the Vec3 script from here. Paste it /copy it somewhere into your mod.

🧪 An accompanying script to test all the operations of Vec3 is here.

Requires

None.

Description

Utils & syntax sugar for using 3-d vector tables.

A 'Vec3' is just a Lua table with only three values on the keys [1], [2], and [3]. Tag methods are set to cause access to the keys 'x', 'y', and 'z' to alias the real number keys.

The Vec3 table is the class definition for the Vec3 type. Its type is not Vec3, but Vec3Static. This table has a 'create' function which is used to produce Vec3 variables.

Although a Vec3:create method is available, you can call the table directly as though it was a function:

local v1 = Vec3:create({ 0, 10, -3 });
local v2 = Vec3({ -1, 1, 12 });

Tag methods are also set to allow you to work with Vec3s with math operators like +, -, *, /, and some utils are provided on the static Vec3 table such as 'magnitude', 'cross' (product), 'dot' (product).

Finally, a tag method is set to allow Vec3 to be castable to string, so you can concat your vectors for printing etc.

Example

dofilepath("data:path/to/Vec3.lua"); -- import it from wherever you put it

-- now the Vec3 (static) table is available, we can create some Vec3s:

local v1 = Vec3:create({ 0, 10, -3 });
local v2 = Vec3({ -1, 1, 12 }); -- can call Vec3 as a constructor directly

print("v1 is " .. v1);
print("add our vecs: " .. v1 + v2);
print("scalar * vector: " .. 10 * v1);
print("cross product: " .. Vec3:cross(v1, v2));

print("v1[2]: " .. v1[2]); -- 10
print("v1.y: " .. v1.y); -- 10

-- use with sobgroup functions:
local pos_a = Vec3(SobGroup_GetPosition("group-a"));
local pos_b = Vec3(SobGroup_GetPosition("group-b"));
print("dot product of group a and b position vectors: " .. Vec3:dot(pos_a, pos_b));

Arguments

The Vec3 table is callable as a function, although it is actually a table.. The callable syntax is equivalent to calling the more standard Vec3::create.

Param Type Description
initial_value Vec3Like (table) The initial values for the vector to hold. A Vec3Like is anything which may be constructed into a Vec3, including other Vec3s and plain tables. A table's other values aside from the 1, 2, and 3 keys will be discarded.

Returns

Type Description
Vec3 (table) A new 3-d vector table, with many tag methods set for convenience.