Skip to content

tes3vector3⚓︎

A simple trio of floating-point numbers.

Properties⚓︎

b⚓︎

The third value in the vector. An alias for z.

Returns:

  • result (number)

g⚓︎

The second value in the vector. An alias for y.

Returns:

  • result (number)

r⚓︎

The first value in the vector. An alias for x.

Returns:

  • result (number)

x⚓︎

The first value in the vector.

Returns:

  • result (number)

y⚓︎

The second value in the vector.

Returns:

  • result (number)

z⚓︎

The third value in the vector.

Returns:

  • result (number)

Methods⚓︎

__tostring⚓︎

Converts the vector to a string with 2 decimal places.

local result = myObject:__tostring()

Returns:

  • result (string)

angle⚓︎

The returns the angle between the two vectors in radians.

local result = myObject:angle(vec)

Parameters:

Returns:

  • result (number)

copy⚓︎

Creates a copy of the vector.

local result = myObject:copy()

Returns:


cross⚓︎

Calculates the cross product with another vector.

local result = myObject:cross(vec)

Parameters:

Returns:


distance⚓︎

Calculates the distance to another vector.

local result = myObject:distance(vec)

Parameters:

Returns:

  • result (number)

dot⚓︎

Calculates the dot product with another vector.

local result = myObject:dot(vec)

Parameters:

Returns:

  • result (number)
Example: The visualization of vector reflection

outDirection = inDirection - (normal * inDirection:dot(normal) * 2)

---@type niSwitchNode, niSwitchNode, niCamera
local lineIn, lineOut, camera
local verticalOffset = tes3vector3.new(0, 0, -30)

local function onLoaded()
    -- MWSE ships with a mesh which contains a few useful widgets.
    -- These can be used during debugging.
    local mesh = tes3.loadMesh("mwse\\widgets.nif") --[[@as niNode]]
    local widgets = {
        -- 3D coordinate axes
        arrows = mesh:getObjectByName("unitArrows") --[[@as niTriShape]],
        -- A common switch node that has three almost infinite lines
        -- along each coordinate exis
        axes = mesh:getObjectByName("axisLines") --[[@as niSwitchNode]],
        plane = mesh:getObjectByName("unitPlane") --[[@as niTriShape]],
        sphere = mesh:getObjectByName("unitSphere") --[[@as niTriShape]]
    }

    local root = tes3.worldController.vfxManager.worldVFXRoot
    ---@cast root niNode

    lineIn = widgets.axes:clone() --[[@as niSwitchNode]]
    lineOut = lineIn:clone() --[[@as niSwitchNode]]

    root:attachChild(lineIn)
    root:attachChild(lineOut)
    root:update()

    -- switchIndex = 0 - x axis (red)
    -- switchIndex = 1 - y axis (green)
    -- switchIndex = 2 - z axis (blue)
    lineIn.switchIndex = 1
    lineOut.switchIndex = 1
    camera = tes3.worldController.worldCamera.cameraData.camera
end
event.register(tes3.event.loaded, onLoaded)

local function simulateCallback()
    lineIn.translation = tes3.getPlayerEyePosition() + verticalOffset

    local inDirection = camera.worldDirection
    local rotation = lineIn.rotation:copy()
    rotation:lookAt(inDirection, camera.worldUp)

    lineIn.rotation = rotation
    lineIn:update()

    -- Now get the coordinates for the outLine
    --local inDirection = tes3.getPlayerEyeVector()
    local hit = tes3.rayTest({
        position = lineIn.translation,
        direction = inDirection,
        returnNormal = true,
        returnSmoothNormal = true,
        ignore = { tes3.player, tes3.player1stPerson }
    })
    if not hit then return end

    lineOut.translation = hit.intersection

    local normal = hit.normal
    local outDirection = inDirection - (normal * inDirection:dot(normal) * 2)
    outDirection:normalize()
    local axis = outDirection:cross(inDirection)
    local rotation = tes3matrix33.new()
    rotation:lookAt(outDirection, axis:normalized())

    lineOut.rotation = rotation
    lineOut:update()
end
event.register(tes3.event.simulate, simulateCallback)

heightDifference⚓︎

Calculates the vertical distance to another vector.

local result = myObject:heightDifference(vec)

Parameters:

Returns:

  • result (number)

interpolate⚓︎

Calculates the interpolated position against the target vector using the distance parameter.

local result = myObject:interpolate(targetPoint, distance)

Parameters:

Returns:


length⚓︎

Calculates the length of the vector.

local result = myObject:length()

Returns:

  • result (number)

lerp⚓︎

Calculates the interpolated vector between this vector and another, given a transition.

local lerpedVector = myObject:lerp(toVector, transition)

Parameters:

  • toVector (tes3vector3): The vector to interpolate towards.
  • transition (number): The interpolation value. Must be between 0.0 (closer to this vector) and 1.0 (closer to the other vector).

Returns:


negate⚓︎

Negates all values in the vector.

myObject:negate()

normalize⚓︎

Normalize the vector in-place, or set its components to zero if normalization is not possible. Returns true if the vector was successfully normalized.

local result = myObject:normalize()

Returns:

  • result (boolean)

normalized⚓︎

Get a normalized copy of the vector.

local result = myObject:normalized()

Returns:


outerProduct⚓︎

Calculates the outer product with another vector.

local result = myObject:outerProduct(vec)

Parameters:

Returns:


toColor⚓︎

Converts the vector to niColor object.

local result = myObject:toColor()

Returns:


Functions⚓︎

new⚓︎

Creates a new vector. If no parameters are provided, an empty set will be constructed.

local vector = tes3vector3.new(x, y, z)

Parameters:

  • x (number): Default: 0.
  • y (number): Default: 0.
  • z (number): Default: 0.

Returns:


Math Operations⚓︎

Addition (+)⚓︎

Left operand type Right operand type Result type Description
tes3vector3 number tes3vector3 Standard vector addition.
tes3vector3 tes3vector3 tes3vector3 Standard vector addition.

Division (/)⚓︎

Left operand type Right operand type Result type Description
tes3vector3 number tes3vector3 Divides the vector by a scalar.

Length (#)⚓︎

Result type Description
number Evaluates to the vector's length in game units.

Multiplication (*)⚓︎

Left operand type Right operand type Result type Description
tes3vector3 tes3vector3 tes3vector3 The per-element multiplication of two vectors.
tes3vector3 number tes3vector3 Multiplies the vector by a scalar.

Subtraction (-)⚓︎

Left operand type Right operand type Result type Description
tes3vector3 number tes3vector3 Standard vector subtraction.
tes3vector3 tes3vector3 tes3vector3 Standard vector subtraction.

Unary minus (-)⚓︎

Result type Description
tes3vector3 Swaps the sign of the vector's components.