Cross

vector:Cross(vector)

Returns the cross product between two vectors.

For each coordinate slot (XYZ) of this vector (eg. X), the function multiplies the value in the next slot of this vector (eg. Y) by the value in the slot after next (eg. Z) of another vector. Then does the opposite, the value in the slot after next (eg. Z) of this vector multiplied by the other vector's next slot (eg. Y). Then subtracts the former multiplication's result from the latter, to return a third vector.

The cross product is the vector perpendicular to the two initial vectors according to the right hand rule.

Parameters:

Name
Type
Description

vector

The numerical value to be multiplied by.

Returns:

Type
Description

A cross product vector perpendicular to both initial vectors. Eg. The vector which called the function (a) calculating the cross product with the vector (b):

a.Y*b.Z - a.Z*b.Y a.Z*b.X - a.X*b.Z a.X*b.Y - a.Y*b.X

Example:

Show that the cross product of the X axis vector and the Y axis vector is the Z axis vector.

-- entity.lua
local function init(self)
    self.Physics = true

    local box_shape = api.physics.NewBoxShape(1, 1, 1) 
    local box = api.physics.NewBody(box_shape, 1)   
    self.Body = box
end

local function update(self, dt)	
    local vector_x = api.physics.NewVector(1,0,0)
    local vector_y = api.physics.NewVector(0,1,0)
 
    local vector_z = vector_x:Cross(vector_y)
    print("The Z axis", vector_z, "is perpendicular to the X and Y axes.")
end

-- Turn on physics calculations for this entity.
-- Create a box shape and a body. Assign it to the entity.
-- Create a vector for the X and Y axes directions.
-- Generate the direction of the Z axis using the cross product of the X and Y axes.

-- Prints:
-- The Z axis &{0 0 30} is perpendicular to the X and Y axes.
-- ...

Last updated