Vectors & Rotations

Vector and rotation operations are provided by the Vector Coprocessor.

Vector instructions use the V.* namespace:

V.ADD
V.DOT
V.NORM

Rotation instructions use the V.ROT.* namespace:

V.ROT.EULER
V.ROT.MUL
V.ROT.APPLY

Memory itself is untyped. Vector and rotation instructions interpret ordinary string-backed values as vectors or rotations and write their results back to ordinary memory.

Vector and rotation literals are described in Syntax.

Memory and stack operations are documented in Memory & Stack:

V.MOVE <dst> <src>
V.SWAP <a> <b>
V.PUSH <value>
V.POP <dst>

V.ROT.MOVE <dst> <src>
V.ROT.SWAP <a> <b>
V.ROT.PUSH <value>
V.ROT.POP <dst>

Vector and rotation comparisons are documented in Branching:

V.BEQ
V.BNEQ

V.ROT.BEQ
V.ROT.BNEQ

Vectors

A vector contains three floating-point components:

<x, y, z>

For example:

<1, 2, 3>
<0.5, -1, 4>
<0, 0, 0>

The components are conventionally named:

Component Description
x X component.
y Y component.
z Z component.

Vector components use the same floating-point representation described in Floating Point.

Floating-point components

Vector components are floating-point values and therefore inherit the precision limitations of floating-point arithmetic.

Vectors produced by calculations may contain small rounding differences even when their mathematically exact results are equal.

See Vector Branching for comparison behavior.

Vector Conversion

V.PACK — Pack Vector

V.PACK <dst> <x> <y> <z>

Constructs a vector from three floating-point components.

Operands

Position Name Type Description
1 dst Location Destination location.
2 x Floating-point value X component.
3 y Floating-point value Y component.
4 z Floating-point value Z component.

Modified locations

Location Description
dst Constructed vector.

Examples

Construct a vector:

V.PACK $000 f1 f2 f3
-- > $000 = <1, 2, 3>

Construct a vector from values in memory:

V.PACK $003 $000 $001 $002

V.UNPACK — Unpack Vector

V.UNPACK <x_dst> <y_dst> <z_dst> <src>

Separates a vector into its three floating-point components.

All destinations precede the source operand.

Operands

Position Name Type Description
1 x_dst Location X component destination.
2 y_dst Location Y component destination.
3 z_dst Location Z component destination.
4 src Vector value Source vector.

Modified locations

Location Description
x_dst X component.
y_dst Y component.
z_dst Z component.

Examples

V.UNPACK $000 $001 $002 <1, 2, 3>
-- > $000 = 1.000000
-- > $001 = 2.000000
-- > $002 = 3.000000

Unpack a vector stored in memory:

V.UNPACK $100 $101 $102 $000

Vector Arithmetic

V.ADD — Addition

V.ADD <dst> <lhs> <rhs>

Adds two vectors component-by-component.

Operands

Position Name Type Description
1 dst Location Destination location.
2 lhs Vector value Left operand.
3 rhs Vector value Right operand.

Modified locations

Location Description
dst Sum.

Examples

V.ADD $000 <1, 2, 3> <4, 5, 6>
-- > $000 = <5, 7, 9>

V.SUB — Subtraction

V.SUB <dst> <lhs> <rhs>

Subtracts one vector from another component-by-component.

Operands

Position Name Type Description
1 dst Location Destination location.
2 lhs Vector value Left operand.
3 rhs Vector value Right operand.

Modified locations

Location Description
dst Difference.

Examples

V.SUB $000 <5, 7, 9> <1, 2, 3>
-- > $000 = <4, 5, 6>

V.MUL — Scalar Multiplication

V.MUL <dst> <vector> <scalar>

Multiplies a vector by a floating-point scalar.

Operands

Position Name Type Description
1 dst Location Destination location.
2 vector Vector value Source vector.
3 scalar Floating-point value Scalar multiplier.

Modified locations

Location Description
dst Scaled vector.

Examples

V.MUL $000 <1, 2, 3> f2
-- > $000 = <2, 4, 6>

V.DIV — Scalar Division

V.DIV <dst> <vector> <scalar>

Divides a vector by a floating-point scalar.

Operands

Position Name Type Description
1 dst Location Destination location.
2 vector Vector value Source vector.
3 scalar Floating-point value Scalar divisor.

Modified locations

Location Description
dst Scaled vector.

Examples

V.DIV $000 <2, 4, 6> f2
-- > $000 = <1, 2, 3>

Vector Products

V.DOT — Dot Product

V.DOT <dst> <lhs> <rhs>

Calculates the dot product of two vectors.

The result is a floating-point value.

Operands

Position Name Type Description
1 dst Location Destination location.
2 lhs Vector value Left operand.
3 rhs Vector value Right operand.

Modified locations

Location Description
dst Dot product.

Examples

V.DOT $000 <1, 0, 0> <0, 1, 0>
-- > $000 = 0.000000
V.DOT $000 <1, 2, 3> <4, 5, 6>
-- > $000 = 32.000000

V.CROSS — Cross Product

V.CROSS <dst> <lhs> <rhs>

Calculates the cross product of two vectors.

Operands

Position Name Type Description
1 dst Location Destination location.
2 lhs Vector value Left operand.
3 rhs Vector value Right operand.

Modified locations

Location Description
dst Cross product.

Examples

V.CROSS $000 <1, 0, 0> <0, 1, 0>
-- > $000 = <0, 0, 1>

The order of the operands affects the direction of the result:

V.CROSS $000 <0, 1, 0> <1, 0, 0>
-- > $000 = <0, 0, -1>

Vector Geometry

V.MAG — Magnitude

V.MAG <dst> <vector>

Returns the magnitude of a vector.

The result is a floating-point value.

Operands

Position Name Type Description
1 dst Location Destination location.
2 vector Vector value Source vector.

Modified locations

Location Description
dst Vector magnitude.

Examples

V.MAG $000 <3, 4, 0>
-- > $000 = 5.000000

V.NORM — Normalize

V.NORM <dst> <vector>

Normalizes a vector to a magnitude of 1.

Operands

Position Name Type Description
1 dst Location Destination location.
2 vector Vector value Source vector.

Modified locations

Location Description
dst Normalized vector.

Examples

V.NORM $000 <3, 4, 0>
-- > $000 = <0.6, 0.8, 0>

V.DIST — Distance

V.DIST <dst> <lhs> <rhs>

Returns the distance between two vectors.

The result is a floating-point value.

Operands

Position Name Type Description
1 dst Location Destination location.
2 lhs Vector value First position.
3 rhs Vector value Second position.

Modified locations

Location Description
dst Distance.

Examples

V.DIST $000 <0, 0, 0> <3, 4, 0>
-- > $000 = 5.000000

Rotations

A rotation is represented by a four-component quaternion:

<x, y, z, s>

For example, the identity rotation is:

<0, 0, 0, 1>

The components are:

Component Description
x Quaternion X component.
y Quaternion Y component.
z Quaternion Z component.
s Quaternion scalar component.

Rotations use quaternions as their machine representation. Euler angles and axis-angle representations may be converted to and from this representation using rotation instructions.

Floating-point components

Rotation components are floating-point values and therefore inherit the precision limitations of floating-point arithmetic.

Rotation equality compares quaternion components directly. Equivalent physical orientations may have different quaternion representations.

See Rotation Branching for comparison behavior.

Rotation Conversion

V.ROT.PACK — Pack Rotation

V.ROT.PACK <dst> <x> <y> <z> <s>

Constructs a rotation from four floating-point quaternion components.

Operands

Position Name Type Description
1 dst Location Destination location.
2 x Floating-point value Quaternion X component.
3 y Floating-point value Quaternion Y component.
4 z Floating-point value Quaternion Z component.
5 s Floating-point value Quaternion scalar component.

Modified locations

Location Description
dst Constructed rotation.

Examples

Construct the identity rotation:

V.ROT.PACK $000 f0 f0 f0 f1
-- > $000 = <0, 0, 0, 1>

V.ROT.UNPACK — Unpack Rotation

V.ROT.UNPACK <x_dst> <y_dst> <z_dst> <s_dst> <src>

Separates a rotation into its four floating-point quaternion components.

All destinations precede the source operand.

Operands

Position Name Type Description
1 x_dst Location X component destination.
2 y_dst Location Y component destination.
3 z_dst Location Z component destination.
4 s_dst Location Scalar component destination.
5 src Rotation value Source rotation.

Modified locations

Location Description
x_dst Quaternion X component.
y_dst Quaternion Y component.
z_dst Quaternion Z component.
s_dst Quaternion scalar component.

Examples

V.ROT.UNPACK $000 $001 $002 $003 <0, 0, 0, 1>
-- > $000 = 0.000000
-- > $001 = 0.000000
-- > $002 = 0.000000
-- > $003 = 1.000000

Euler Angles

V.ROT.EULER — Euler to Rotation

V.ROT.EULER <dst> <euler>

Converts Euler angles to a rotation.

The Euler angles are supplied as a vector of rotations around the X, Y, and Z axes.

Angles are expressed in radians.

Operands

Position Name Type Description
1 dst Location Destination location.
2 euler Vector value Euler angles in radians.

Modified locations

Location Description
dst Resulting rotation.

Examples

Create a rotation from Euler angles:

V.ROT.EULER $000 <0, 0, 1.570796>

Floating-point angle conversion may be used when working in degrees:

F.RAD $000 f90
V.PACK $001 f0 f0 $000
V.ROT.EULER $002 $001

V.ROT.TOEULER — Rotation to Euler

V.ROT.TOEULER <dst> <rotation>

Converts a rotation to Euler angles.

The result is a vector containing rotations around the X, Y, and Z axes in radians.

Operands

Position Name Type Description
1 dst Location Destination location.
2 rotation Rotation value Source rotation.

Modified locations

Location Description
dst Euler-angle vector in radians.

Examples

V.ROT.TOEULER $001 $000

Axis-Angle Conversion

V.ROT.AXIS — Axis-Angle to Rotation

V.ROT.AXIS <dst> <axis> <angle>

Constructs a rotation from an axis and angle.

The axis is supplied as a vector and the angle is expressed in radians.

Operands

Position Name Type Description
1 dst Location Destination location.
2 axis Vector value Axis of rotation.
3 angle Floating-point value Angle in radians.

Modified locations

Location Description
dst Resulting rotation.

Examples

Rotate 90 degrees around the Z axis:

V.ROT.AXIS $000 <0, 0, 1> f1.570796

V.ROT.TOAXIS — Rotation to Axis-Angle

V.ROT.TOAXIS <axis_dst> <angle_dst> <rotation>

Separates a rotation into its axis and angle.

The angle is returned in radians.

All destinations precede the source operand.

Operands

Position Name Type Description
1 axis_dst Location Axis destination.
2 angle_dst Location Angle destination.
3 rotation Rotation value Source rotation.

Modified locations

Location Description
axis_dst Rotation axis as a vector.
angle_dst Rotation angle in radians.

Examples

V.ROT.TOAXIS $001 $002 $000

Rotation Construction & Measurement

V.ROT.BETWEEN — Rotation Between Vectors

V.ROT.BETWEEN <dst> <from> <to>

Constructs a rotation that rotates one vector toward another.

Operands

Position Name Type Description
1 dst Location Destination location.
2 from Vector value Starting direction.
3 to Vector value Target direction.

Modified locations

Location Description
dst Resulting rotation.

Examples

Construct a rotation from the positive X axis to the positive Y axis:

V.ROT.BETWEEN $000 <1, 0, 0> <0, 1, 0>

V.ROT.ANGLE — Angle Between Rotations

V.ROT.ANGLE <dst> <lhs> <rhs>

Returns the angular difference between two rotations.

The result is expressed in radians.

Operands

Position Name Type Description
1 dst Location Destination location.
2 lhs Rotation value First rotation.
3 rhs Rotation value Second rotation.

Modified locations

Location Description
dst Angular difference in radians.

Examples

V.ROT.ANGLE $002 $000 $001

Rotation Composition

V.ROT.MUL — Multiply Rotations

V.ROT.MUL <dst> <lhs> <rhs>

Multiplies two rotations.

Rotation multiplication composes the two rotations into a single rotation.

Operands

Position Name Type Description
1 dst Location Destination location.
2 lhs Rotation value Left rotation.
3 rhs Rotation value Right rotation.

Modified locations

Location Description
dst Composed rotation.

Examples

V.ROT.MUL $002 $000 $001

V.ROT.DIV — Divide Rotations

V.ROT.DIV <dst> <lhs> <rhs>

Divides one rotation by another.

Operands

Position Name Type Description
1 dst Location Destination location.
2 lhs Rotation value Left rotation.
3 rhs Rotation value Right rotation.

Modified locations

Location Description
dst Resulting rotation.

Examples

V.ROT.DIV $002 $000 $001

Vector Rotation

V.ROT.APPLY — Apply Rotation

V.ROT.APPLY <dst> <vector> <rotation>

Applies a rotation to a vector.

Operands

Position Name Type Description
1 dst Location Destination location.
2 vector Vector value Vector to rotate.
3 rotation Rotation value Rotation to apply.

Modified locations

Location Description
dst Rotated vector.

Examples

V.ROT.APPLY $002 $000 $001

Apply a rotation directly to a vector literal:

V.ROT.APPLY $001 <1, 0, 0> $000

V.ROT.UNAPPLY — Unapply Rotation

V.ROT.UNAPPLY <dst> <vector> <rotation>

Applies the inverse of a rotation to a vector.

Operands

Position Name Type Description
1 dst Location Destination location.
2 vector Vector value Vector to rotate.
3 rotation Rotation value Rotation to unapply.

Modified locations

Location Description
dst Rotated vector.

Examples

V.ROT.UNAPPLY $002 $000 $001

V.ROT.UNAPPLY reverses the corresponding application:

V.ROT.APPLY $002 $000 $001
V.ROT.UNAPPLY $003 $002 $001

Subject to floating-point precision, $003 contains the original vector from $000.