Skip to content

Math

Classes

aabb

An aabb is an axis aligned bounding box, a box whose sides are straight along the x, y and z axes, defined by the two opposite corners with the smallest and largest coordinates.

  1. aabb();

  2. aabb(const vector&in min, const vector&in max);

  3. aabb(const vector&in center, float radius);

  4. aabb(const aabb&in other);

Arguments (2):

Arguments (3):

Arguments (4):

Remarks:

A default constructed aabb is empty and invalid, which is useful as the starting point for a box that grows to contain a set of points with encapsulate(). Boxes are used to describe regions of the world, such as the bounds of a body, and to check cheaply whether things could be touching.

Example:

void main() {
	aabb box1(vector(1, 1, 1), vector(5, 5, 5));
	aabb box2(vector(2, 2, 2), vector(4, 4, 4));
	if (box1.contains(box2)) alert("Boxed in a box!", "Box1 contains box2.");
}

Methods

contains

Check whether a point or another box is inside this box.

  1. bool aabb::contains(const vector&in point) const;

  2. bool aabb::contains(const aabb&in box) const;

Arguments:
Returns:

bool: true if it is inside.

Example:
void main() {
	aabb room(vector(0, 0, 0), vector(10, 10, 3));
	vector player(5, 5, 1);
	alert("Inside", room.contains(player) ? "The player is in the room." : "The player is outside.");
}
encapsulate

Grow the box so that it contains a point or another box.

  1. void aabb::encapsulate(const vector&in point);

  2. void aabb::encapsulate(const aabb&in box);

Arguments:
Remarks:

inflate_with_point() does the same as the first form.

Example:
void main() {
	aabb bounds;
	bounds.encapsulate(vector(1, 5, 0));
	bounds.encapsulate(vector(-3, 2, 4));
	alert("Bounds", "The box is " + bounds.size.x + " by " + bounds.size.y + " by " + bounds.size.z);
}
inflate

Make the box bigger on every side.

void aabb::inflate(float x, float y, float z);

Arguments:
Remarks:

The box grows by twice each amount along that axis. Negative values shrink it.

Example:
void main() {
	aabb box(vector(0, 0, 0), vector(1, 1, 1));
	box.inflate(1, 1, 1);
	alert("Size", "The box is now " + box.size.x + " wide."); // 3
}
inflate_with_point

Grow the box so that it contains a point.

void aabb::inflate_with_point(const vector&in point);

Arguments:
Remarks:

This is the same as the first form of encapsulate().

Example:
void main() {
	aabb box(vector(0, 0, 0), vector(1, 1, 1));
	box.inflate_with_point(vector(5, 0, 0));
	alert("Max", "" + box.max); // 5, 1, 1
}
intersect

Get the box where this box and another overlap.

aabb@ aabb::intersect(const aabb&in other) const;

Arguments:
Returns:

aabb@: the shared part of the boxes, which is not valid if they don't overlap.

Example:
void main() {
	aabb a(vector(0, 0, 0), vector(2, 2, 2));
	aabb b(vector(1, 1, 1), vector(3, 3, 3));
	aabb shared = a.intersect(b);
	alert("Shared", "The overlap is " + shared.size.x + " wide.");
}
overlaps

Check whether another box overlaps this one.

bool aabb::overlaps(const aabb&in other) const;

Arguments:
Returns:

bool: true if any part of the boxes overlap.

Remarks:

test_collision() does the same.

test_collision

Check whether another box overlaps this one.

bool aabb::test_collision(const aabb&in other) const;

Arguments:
Returns:

bool: true if any part of the boxes overlap.

Remarks:

This is the same as overlaps().

Example:
void main() {
	aabb a(vector(0, 0, 0), vector(2, 2, 2));
	aabb b(vector(1, 1, 1), vector(3, 3, 3));
	alert("Collision", a.test_collision(b) ? "The boxes overlap." : "The boxes are apart.");
}

Operators

opAssign

Overloads the = operator, copying another box.

aabb& aabb::opAssign(const aabb&in other);

Arguments:
opEquals

Overloads the == operator, comparing two boxes.

bool aabb::opEquals(const aabb&in other) const;

Arguments:
Returns:

bool: true if both corners are equal.

Properties

center

The point in the middle of the box.

const vector aabb::center;

Example:
void main() {
	aabb box(vector(0, 0, 0), vector(10, 4, 2));
	alert("Center", "" + box.center); // 5, 2, 1
}
extent

Half of the size of the box along each axis, the distance from the center to the faces.

const vector aabb::extent;

Example:
void main() {
	aabb box(vector(0, 0, 0), vector(10, 4, 2));
	alert("Extent", "" + box.extent); // 5, 2, 1
}
is_valid

Whether the box is valid, meaning that its minimum is not past its maximum on any axis.

const bool aabb::is_valid;

Remarks:

A default constructed box is not valid, until it has been given a point with encapsulate().

max

The corner of the box with the largest coordinates.

vector aabb::max;

Example:
void main() {
	aabb box(vector(1, 2, 3), vector(4, 5, 6));
	alert("Max", "" + box.max);
}
min

The corner of the box with the smallest coordinates.

vector aabb::min;

Example:
void main() {
	aabb box(vector(1, 2, 3), vector(4, 5, 6));
	alert("Min", "" + box.min);
}
size

The size of the box along each axis, max - min.

const vector aabb::size;

volume

The volume of the box.

const float aabb::volume;

Example:
void main() {
	aabb box(vector(0, 0, 0), vector(2, 3, 4));
	alert("Volume", "" + box.volume); // 24
}

complex

A class representing a complex number, composed of a real part r and an imaginary part i.

  1. complex();

  2. complex(const complex&in other);

  3. complex(float r, float i = 0.0);

Arguments (3):

Remarks:

The complex class supports basic arithmetic operations (add, subtract, multiply, divide) and can be compared for equality. It also provides access to magnitude (abs()), and utilities for working with real/imaginary order via ri and ir properties.

Example:

void main() {
	complex a(2, 3); // 2 + 3i
	complex b(1, -1); // 1 - i
	complex c = a + b; // (2+1, 3-1) = (3, 2)
	alert("result", "real: " + c.r + ", imag: " + c.i);
}

Functions

abs

Returns the absolute value (magnitude) of the complex number.

float complex::abs() const;

Example:
void main() {
	complex c(3, 4);
	alert("Magnitude =", c.abs());
}

Operators

opAdd

Adds two complex numbers and returns the result.

complex complex::opAdd(const complex&in other) const;

Arguments:
Returns:

complex: a new complex object containing the value of the addition.

Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
	return "(" + c.r + ", " + c.i + ")";
}
void main() {
	complex a(1, 2);
	complex b(3, 4);
	complex c = a + b;
	alert("a + b =", to_string(c));
}
opAddAssign

Adds another complex number to this one.

complex& complex::opAddAssign(const complex&in other);

Arguments:
Returns:

complex&: a reference to the complex number being operated on.

Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
	return "(" + c.r + ", " + c.i + ")";
}
void main() {
	complex a(1, 2);
	complex b(3, 4);
	a += b;
	alert("a + b =", to_string(a));
}
opDiv

Divides this complex number by another and returns the result.

complex complex::opDiv(const complex&in other) const;

Arguments:
Returns:

complex: a new complex object containing the value of the division.

Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
	return "(" + c.r + ", " + c.i + ")";
}
void main() {
	complex a(1, 2);
	complex b(3, 4);
	complex c = a / b;
	alert("a / b =", to_string(c));
}
opDivAssign

Divides this complex number by another.

complex& complex::opDivAssign(const complex&in other);

Arguments:
Returns:

complex&: a reference to the complex number being operated on.

Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
	return "(" + c.r + ", " + c.i + ")";
}
void main() {
	complex a(1, 2);
	complex b(3, 4);
	a /= b;
	alert("a / b =", to_string(a));
}
opEquals

Checks if two complex numbers are equal.

bool complex::opEquals(const complex&in other) const;

Arguments:
Returns:

bool: true if the two complex numbers are equal, false otherwise.

Example:
void main() {
	complex a(1, 2);
	complex b(1, 2);
	alert("a equals b?", a == b);
}
opMul

Multiplies two complex numbers and returns the result.

complex complex::opMul(const complex&in other) const;

Arguments:
Returns:

complex: a new complex object containing the value of the multiplication.

Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
	return "(" + c.r + ", " + c.i + ")";
}
void main() {
	complex a(1, 2);
	complex b(3, 4);
	complex c = a * b;
	alert("a * b =", to_string(c));
}
opMulAssign

Multiplies this complex number by another.

complex& complex::opMulAssign(const complex&in other);

Arguments:
Returns:

complex&: a reference to the complex number being operated on.

Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
	return "(" + c.r + ", " + c.i + ")";
}
void main() {
	complex a(1, 2);
	complex b(3, 4);
	a *= b;
	alert("a * b =", to_string(a));
}
opSub

Subtracts a complex number from this one and returns the result.

complex complex::opSub(const complex&in other) const;

Arguments:
Returns:

complex: a new complex object containing the value of the subtraction.

Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
	return "(" + c.r + ", " + c.i + ")";
}
void main() {
	complex a(5, 6);
	complex b(2, 3);
	complex c = a - b;
	alert("a - b =", to_string(c));
}
opSubAssign

Subtracts another complex number from this one.

complex& complex::opSubAssign(const complex&in other);

Arguments:
Returns:

complex&: a reference to the complex number being operated on.

Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
	return "(" + c.r + ", " + c.i + ")";
}
void main() {
	complex a(5, 6);
	complex b(2, 3);
	a -= b;
	alert("a - b =", to_string(a));
}

Properties

i

Represents the imaginary component of the complex number.

float complex::i;

Example:
void main() {
	complex c(0, 1);
	alert("The imaginary part is", c.i);
}
ir

Accesses the complex number in imaginary-real order.

complex complex::ir [property];

Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
	return "(" + c.r + ", " + c.i + ")";
}
void main() {
	complex c(1, 2);
	alert("Imaginary-Real =", to_string(c.ir));
}
ri

Accesses the complex number in real-imaginary order.

complex complex::ri;

Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
	return "(" + c.r + ", " + c.i + ")";
}
void main() {
	complex c(1, 2);
	alert("Real-Imaginary =", to_string(c.ri));
}

plane

A plane is a flat surface that goes on forever in every direction, described by the direction it faces, its normal, and how far it is from the origin, its constant.

  1. plane();

  2. plane(const vector&in normal, float constant);

  3. plane(const vector&in point, const vector&in normal);

  4. plane(const vector&in point1, const vector&in point2, const vector&in point3);

  5. plane(const plane&in other);

Arguments (2):

Arguments (3):

Arguments (4):

Arguments (5):

Remarks:

A default constructed plane is the ground of NVGT's coordinate system, facing up along the z axis through the origin.

The points on a plane are those for which point.dot(normal) + constant is 0, which is what signed_distance() calculates. The side the normal points to is in front of the plane, and the other side is behind it. A plane given to physics_plane_shape_create() is solid behind it, which makes planes a simple way to make the floor of a map.

The normal is always kept at a length of 1. A longer or shorter normal given to the second constructor is shortened or lengthened along with the constant, so that the plane is still in the same place, plane(vector(0, 0, 2), -4) being the same as plane(vector(0, 0, 1), -2). A normal of zero, or three points that are in a line, make a plane whose normal is zero, which is not a valid plane.

Example:

void main() {
	plane floor(vector(0, 0, 0), vector(0, 0, 1)); // A point on the plane, then the direction it faces.
	vector player(3, 4, 1.5);
	alert("Height", "The player is " + floor.signed_distance(player) + " meters above the floor.");
}

Methods

offset

Get a copy of the plane moved along its normal.

plane@ plane::offset(float distance) const;

Arguments:
Returns:

plane@: the moved plane. The plane this is called on is not changed.

Example:
void main() {
	plane ground_floor;
	plane second_floor = ground_floor.offset(3);
	alert("Second floor", "The second floor is " + second_floor); // plane(0, 0, 1, -3)
}
project_point

Get the point on the plane that is closest to another point.

vector@ plane::project_point(const vector&in point) const;

Arguments:
Returns:

vector@: the point on the plane directly in front of or behind the given point.

Example:
void main() {
	plane slope(vector(0, 0, 0), vector(0, -1, 1)); // A ramp rising as y increases.
	vector bird(0, 10, 20);
	alert("Below", "The point on the slope below the bird is " + slope.project_point(bird));
}
scaled

Get a copy of the plane as it would be if all of space were stretched by different amounts along each axis.

plane@ plane::scaled(const vector&in scale) const;

Arguments:
Returns:

plane@: the scaled plane. The plane this is called on is not changed.

Remarks:

Points that were on the plane are on the scaled plane once each of their coordinates is multiplied by the scale, so a plane at a height of 1 scaled by vector(1, 1, 2) is at a height of 2. No part of the scale may be 0.

signed_distance

Get the distance from the plane to a point.

float plane::signed_distance(const vector&in point) const;

Arguments:
Returns:

float: the distance, which is positive when the point is in front of the plane, the side its normal points to, negative when it is behind the plane and 0 when it is on it.

Example:
void main() {
	plane water_surface(vector(0, 0, 0), vector(0, 0, 1));
	vector diver(10, 5, -3);
	if (water_surface.signed_distance(diver) < 0) alert("Underwater", "The diver is " + -water_surface.signed_distance(diver) + " meters deep.");
}
transformed

Get a copy of the plane turned and then moved.

plane@ plane::transformed(const vector&in position, const quaternion&in rotation) const;

Arguments:
Returns:

plane@: the transformed plane. The plane this is called on is not changed.

Remarks:

This is useful for describing a plane relative to something, such as the floor of a room, and then placing it in the world at that thing's position and rotation.

Example:
void main() {
	plane floor;
	plane wall = floor.transformed(vector(5, 0, 0), quaternion_from_axis_angle(vector(0, 1, 0), 1.5707964)); // Tipped over to face along x, then moved 5 meters along x.
	alert("Wall", "The wall is " + wall + ", and the point (5, 3, 2) is " + wall.signed_distance(vector(5, 3, 2)) + " meters from it.");
}

Operators

opAssign

Overloads the = operator, copying another plane.

plane& plane::opAssign(const plane&in other);

Arguments:
opEquals

Overloads the == operator, comparing two planes.

bool plane::opEquals(const plane&in other) const;

Arguments:
Returns:

bool: true if the normals and constants are equal.

Remarks:

Two planes in the same place but facing opposite ways are not equal.

opImplConv

Converts the plane to a string.

string plane::opImplConv() const;

Returns:

string: the plane in the form plane(normal x, normal y, normal z, constant).

Properties

constant

The negative of the plane's distance from the origin along its normal.

float plane::constant;

Remarks:

A plane facing up with a constant of -2 is at a height of 2, and one with a constant of 2 is at a height of -2. The plane is the set of points for which point.dot(normal) + constant is 0.

normal

The direction the plane faces, which is always of length 1.

vector plane::normal;

Remarks:

A normal that is set is made length 1 without changing the constant, so the plane stays the same distance from the origin but turns to face the new direction.

quaternion

A quaternion holds a rotation in 3D space, which turns something about an axis by an angle.

  1. quaternion();

  2. quaternion(float x, float y, float z, float w);

  3. quaternion(const quaternion&in other);

Arguments (2):

Arguments (3):

Remarks:

The four numbers of a quaternion are not angles, and are not meant to be set by hand. A default constructed quaternion is the identity, meaning no rotation, which is also available as IDENTITY_QUATERNION. Make other rotations with quaternion_from_axis_angle() or quaternion_from_euler_angles(). Rotations are measured in radians.

Quaternions are used to rotate bodies in a physics_world. Multiplying two quaternions with * combines their rotations, the one on the right being applied first, and multiplying a quaternion by a vector rotates that vector. Unlike angles, quaternions don't suffer from awkward cases such as the order of rotations mattering or rotations that line up and lose a direction, and they interpolate smoothly with slerp().

Example:

void main() {
	// A quarter turn counter clockwise as seen from above, around the z axis (up).
	quaternion turn = quaternion_from_axis_angle(vector(0, 0, 1), 3.14159265 / 2);
	vector forward(0, 1, 0);
	vector rotated = turn * forward;
	alert("Rotated", "Facing forward and turning a quarter now points to " + rotated.x + ", " + rotated.y + ", " + rotated.z); // Approximately -1, 0, 0.
}

Methods

conjugated

Get the conjugate of the quaternion.

quaternion@ quaternion::conjugated() const;

Returns:

quaternion@: the conjugate, which for a rotation of length 1 is the opposite rotation.

dot

Calculate the dot product of this quaternion and another.

float quaternion::dot(const quaternion&in other) const;

Arguments:
Returns:

float: the dot product, which is 1 for equal rotations and closer to 0 the more different they are.

get_rotation_angle

Get the angle that the quaternion rotates by around an axis.

float quaternion::get_rotation_angle(const vector&in axis) const;

Arguments:
Returns:

float: the angle in radians.

Remarks:

This is meant for rotations that are around the given axis, such as turning around the z axis, to find out how far that was.

Example:
void main() {
	quaternion q = quaternion_from_axis_angle(vector(0, 0, 1), 0.5);
	alert("Angle", "The rotation around the z axis is " + q.get_rotation_angle(vector(0, 0, 1)) + " radians.");
}
inversed

Get the inverse of the quaternion.

quaternion@ quaternion::inversed() const;

Returns:

quaternion@: the quaternion that undoes this rotation.

Remarks:

Multiplying a quaternion by its inverse gives no rotation.

Example:
void main() {
	quaternion q = quaternion_from_axis_angle(vector(0, 0, 1), 1.0);
	quaternion undone = q.inversed() * q;
	alert("Undone", "Rotating and then undoing it leaves w at " + undone.w); // 1
}
is_normalized

Check whether the quaternion has a length of 1.

bool quaternion::is_normalized(float tolerance = 1.0e-5f) const;

Arguments:
Returns:

bool: true if the quaternion is normalized to within the tolerance.

length

Calculate the length of the quaternion.

float quaternion::length() const;

Returns:

float: the length, which is 1 for a valid rotation.

lerp

Interpolate between this rotation and another in a straight line.

quaternion@ quaternion::lerp(const quaternion&in dest, float t) const;

Arguments:
Returns:

quaternion@: the rotation in between.

Remarks:

This is faster than slerp() and fine for small differences, but the speed of the rotation is not constant.

normalized

Get a copy of the quaternion with a length of 1.

quaternion@ quaternion::normalized() const;

Returns:

quaternion@: the normalized quaternion.

Remarks:

Repeatedly combining rotations can make a quaternion drift away from a length of 1, which makes it a distorted rotation. Normalizing it brings it back.

rotate_axis_x

Find where the x axis ends up when rotated by the quaternion.

vector@ quaternion::rotate_axis_x() const;

Returns:

vector@: the rotated x axis, which for a body is the direction to its right.

rotate_axis_y

Find where the y axis ends up when rotated by the quaternion.

vector@ quaternion::rotate_axis_y() const;

Returns:

vector@: the rotated y axis, which for a body is the direction it is facing.

rotate_axis_z

Find where the z axis ends up when rotated by the quaternion.

vector@ quaternion::rotate_axis_z() const;

Returns:

vector@: the rotated z axis, which for a body is the direction above it.

slerp

Interpolate between this rotation and another at a constant speed.

quaternion@ quaternion::slerp(const quaternion&in dest, float t) const;

Arguments:
Returns:

quaternion@: the rotation in between.

Remarks:

The rotation takes the shortest path and the speed is constant, which is what you want for smoothly turning something. lerp() is cheaper but the speed varies.

Example:
void main() {
	quaternion start; // No rotation.
	quaternion end = quaternion_from_axis_angle(vector(0, 0, 1), 1.0);
	quaternion half = start.slerp(end, 0.5);
	alert("Halfway", "The angle halfway is " + half.get_rotation_angle(vector(0, 0, 1))); // 0.5
}

Operators

opAssign

Overloads the = operator, copying another quaternion.

quaternion& quaternion::opAssign(const quaternion&in other);

Arguments:
opEquals

Overloads the == operator, comparing two quaternions.

bool quaternion::opEquals(const quaternion&in other) const;

Arguments:
Returns:

bool: true if all four components are equal.

opImplConv

Converts the quaternion to a string.

string quaternion::opImplConv() const;

Returns:

string: the quaternion in the form quaternion(x, y, z, w).

opMul

Overloads the * operator, combining two rotations or rotating a vector.

  1. quaternion@ quaternion::opMul(const quaternion&in other) const;
  2. vector@ quaternion::opMul(const vector&in v) const;
Arguments:
Returns:

quaternion@ (1): the combined rotation.

vector@ (2): the rotated vector.

Remarks:

The result of combining rotations depends on their order. In a * b, b is applied first and then a.

Properties

w

The w component of the quaternion.

float quaternion::w;

x

The x component of the quaternion.

float quaternion::x;

Remarks:

The components of a quaternion don't correspond to angles. To work out an angle use get_rotation_angle().

y

The y component of the quaternion.

float quaternion::y;

z

The z component of the quaternion.

float quaternion::z;

vector

A vector holds three numbers, x, y and z, that usually represent a point or a direction in 3D space.

  1. vector();

  2. vector(float x, float y, float z = 0.0);

  3. vector(const vector&in other);

Arguments (2):

Arguments (3):

Remarks:

A default constructed vector is all zeros. Vectors are used throughout NVGT's 3D features, such as the positions of sounds and the physics_world. Positions and directions use the same type: a position is where something is, and a direction is where to move or how far from the origin something is, and can be multiplied by a distance to move that far.

Vectors support the usual operators, adding, subtracting and comparing them, and multiplying and dividing by a number. They are copied when assigned, so changing a copy does not change the original.

Example:

void main() {
	vector v1(5, 5, 5);
	vector v2(10, 10, 0);
	vector v3 = v1 + v2;
	alert("example", "the new vector is " + v3.x + ", " + v3.y + ", " + v3.z); // will show that the vector is 15, 15, 5.
}

Methods

abs

Get a copy of the vector with the sign of each coordinate removed.

vector@ vector::abs() const;

Returns:

vector@: the vector with each coordinate made positive.

cross

Calculate the cross product of this vector and another.

vector@ vector::cross(const vector&in other) const;

Arguments:
Returns:

vector@: a vector at right angles to both of the originals.

Remarks:

The cross product follows the right hand rule, so with x to the right and y forward the cross product of x with y points up the z axis. Its length is the product of the lengths of the vectors times the sine of the angle between them.

Example:
void main() {
	vector right(1, 0, 0), forward(0, 1, 0);
	vector up = right.cross(forward);
	alert("Cross", "Up is " + up.x + ", " + up.y + ", " + up.z);
}
dot

Calculate the dot product of this vector and another.

float vector::dot(const vector&in other) const;

Arguments:
Returns:

float: the dot product.

Remarks:

For two vectors of length 1 the dot product is 1 when they point the same way, 0 when they are at right angles and -1 when they point opposite ways, which makes it useful for finding out whether something is in front of or behind something else.

Example:
void main() {
	vector facing(0, 1, 0);
	vector to_enemy(0, 5, 0);
	alert("Dot", "The dot product is " + facing.dot(to_enemy)); // Positive, so the enemy is in front.
}
is_near_zero

Check whether the vector is so short that it is practically zero.

bool vector::is_near_zero(float max_dist_sq = 1.0e-12f) const;

Arguments:
Returns:

bool: true if the vector's squared length is no more than max_dist_sq.

is_normalized

Check whether the vector has a length of 1.

bool vector::is_normalized(float tolerance = 1.0e-6f) const;

Arguments:
Returns:

bool: true if the vector is normalized to within the tolerance.

length

Calculate the length of the vector.

float vector::length() const;

Returns:

float: the distance of the vector's point from the origin.

Remarks:

To compare distances it is quicker to use length_square() and compare squares.

Example:
void main() {
	vector v(3, 4, 0);
	alert("Length", "The length is " + v.length()); // 5
}
length_square

Calculate the square of the vector's length.

  1. float vector::length_square() const;

  2. float vector::length_sq() const;

Returns:

float: the squared length.

Remarks:

This avoids the square root that length() has to calculate, so is better for comparing distances: instead of v.length() < 5 write v.length_square() < 25. length_sq() is a shorter name for the same method.

Example:
void main() {
	vector a(1, 2, 3), b(4, 6, 3);
	vector difference = b - a;
	alert("Distance", "The squared distance between the points is " + difference.length_square());
}
normalize

Change the vector's length to 1 while keeping the direction it points in.

void vector::normalize();

Remarks:

Unlike normalized(), this changes the vector itself rather than returning a copy. A vector of length 0 has no direction, so it is left unchanged instead of being filled with invalid numbers.

Example:
void main() {
	vector v(3, 4, 0);
	v.normalize();
	alert("Vector", "(" + v.x + ", " + v.y + ", " + v.z + ")");
}
normalized

Get a copy of the vector with a length of 1 that points in the same direction.

vector@ vector::normalized() const;

Returns:

vector@: the normalized vector.

Remarks:

Normalizing is useful when you need the direction something is pointing rather than how far it goes, such as when you want to move at a particular speed in a direction: position += direction.normalized() * speed. The original vector is not changed.

Normalizing a vector of length 0 gives a vector of invalid numbers, so check is_near_zero() first if that is possible.

Example:
void main() {
	vector v(3, 4, 0);
	vector n = v.normalized();
	alert("Vector", "(" + n.x + ", " + n.y + ", " + n.z + ")");
}
set

Set all three coordinates at once.

void vector::set(float x, float y, float z);

Arguments:
Example:
void main() {
	vector v;
	v.set(1, 2, 3);
	alert("Vector", v.x + ", " + v.y + ", " + v.z);
}
set_to_zero

Set all three coordinates of the vector to 0.

  1. void vector::set_to_zero();

  2. void vector::setToZero();

Remarks:

This is the same as v = VEC3_ZERO;. setToZero() is an older name for the same method.

Example:
void main() {
	vector v(1, 2, 3);
	v.set_to_zero();
	alert("Vector", "(" + v.x + ", " + v.y + ", " + v.z + ")");
}

Operators

opAdd

Overloads the + operator, adding two vectors together.

vector@ vector::opAdd(const vector&in other) const;

Arguments:
Returns:

vector@: the sum.

Remarks:

Each coordinate is added to the same coordinate of the other vector.

Example:
void main() {
	vector v1(1, 2, 3), v2(10, 20, 30);
	vector v3 = v1 + v2;
	alert("Sum", v3.x + ", " + v3.y + ", " + v3.z);
}
opAddAssign

Overloads the += operator, adding a vector to this one.

vector& vector::opAddAssign(const vector&in other);

Arguments:
Example:
void main() {
	vector position(0, 0, 0);
	position += vector(1, 2, 3);
	alert("Position", position.x + ", " + position.y + ", " + position.z);
}
opAssign

Overloads the = operator, copying another vector's coordinates into this one.

vector& vector::opAssign(const vector&in other);

Arguments:
Example:
void main() {
	vector a(1, 2, 3), b;
	b = a;
	a.x = 100; // b is unaffected.
	alert("b", b.x + ", " + b.y + ", " + b.z);
}
opDiv

Overloads the / operator, dividing a vector by a number or by another vector.

  1. vector@ vector::opDiv(const vector&in other) const;

  2. vector@ vector::opDiv(float scalar) const;

Arguments:
Returns:

vector@: the quotient.

Remarks:

Dividing two vectors divides each coordinate by the same coordinate of the other.

Example:
void main() {
	vector v1(10, 20, 30);
	vector v2 = v1 / 10;
	alert("Vector", v2.x + ", " + v2.y + ", " + v2.z);
}
opDivAssign

Overloads the /= operator, dividing this vector by a number.

vector& vector::opDivAssign(float scalar);

Arguments:
Example:
void main() {
	vector v(2, 4, 6);
	v /= 2;
	alert("Vector", v.x + ", " + v.y + ", " + v.z);
}
opEquals

Overloads the == operator, comparing two vectors.

bool vector::opEquals(const vector&in other) const;

Arguments:
Returns:

bool: true if all three coordinates are equal.

Remarks:

Because coordinates are floating point numbers, vectors that were calculated by different means may differ by a tiny amount even when they ought to be the same.

Example:
void main() {
	vector a(1, 2, 3), b(1, 2, 3);
	alert("Equal", a == b ? "yes" : "no");
}
opImplConv

Converts the vector to a string.

string vector::opImplConv() const;

Returns:

string: the vector in the form vector(x, y, z).

Remarks:

This conversion happens automatically when a vector is added to a string, so "position: " + v works.

Example:
void main() {
	vector v(1, 2, 3);
	alert("Vector", "The vector is " + v);
}
opIndex

Overloads the [] operator, accessing a coordinate by number.

  1. float vector::opIndex(uint index) const;

  2. void vector::opIndex(uint index, float value);

Arguments:
Example:
void main() {
	vector v(1, 2, 3);
	v[2] = 30;
	alert("Vector", "z is " + v[2]);
}
opMul

Overloads the * operator, multiplying a vector by a number or by another vector.

  1. vector@ vector::opMul(const vector&in other) const;

  2. vector@ vector::opMul(float scalar) const;

Arguments:
Returns:

vector@: the product.

Remarks:

Multiplying by a number scales the vector, so multiplying a direction by a distance gives the offset. Multiplying two vectors multiplies each coordinate by the same coordinate of the other, which is not the dot or cross product.

Example:
void main() {
	vector v1(1, 4, 8);
	vector v2 = v1 * 3;
	alert("The second vector =", "(" + v2.x + ", " + v2.y + ", " + v2.z + ")");
}
opMulAssign

Overloads the *= operator, multiplying this vector by a number.

vector& vector::opMulAssign(float scalar);

Arguments:
Example:
void main() {
	vector v(1, 2, 3);
	v *= 2;
	alert("Vector", v.x + ", " + v.y + ", " + v.z);
}
opNeg

Overloads the unary - operator, giving the vector pointing the opposite way.

vector@ vector::opNeg() const;

Returns:

vector@: the vector with every coordinate negated.

opSub

Overloads the - operator, subtracting a vector from another.

vector@ vector::opSub(const vector&in other) const;

Arguments:
Returns:

vector@: the difference.

Remarks:

Subtracting one position from another gives the vector from the second to the first, whose length is the distance between them.

Example:
void main() {
	vector player(0, 0, 0), enemy(3, 4, 0);
	vector between = enemy - player;
	alert("Distance", "The enemy is " + between.length() + " meters away.");
}
opSubAssign

Overloads the -= operator, subtracting a vector from this one.

vector& vector::opSubAssign(const vector&in other);

Arguments:
Example:
void main() {
	vector position(10, 10, 10);
	position -= vector(1, 2, 3);
	alert("Position", position.x + ", " + position.y + ", " + position.z);
}

Properties

absolute

A copy of the vector with the sign of each coordinate removed, the same as abs().

const vector@ vector::absolute;

is_finite

Whether all three coordinates of the vector are real numbers, meaning none of them are infinite or NaN.

const bool vector::is_finite;

Remarks:

Dividing by zero or normalizing a zero vector with normalized() are common ways to end up with a vector that is not finite.

Example:
void main() {
	vector v(1, 2, 3);
	alert("Is finite", "" + v.is_finite); // true
	v /= 0;
	alert("Is finite", "" + v.is_finite); // false
}
is_unit

Whether the vector's length is 1, allowing for a tiny rounding error.

const bool vector::is_unit;

Remarks:

Use is_normalized() if you need to choose how much error is allowed.

Example:
void main() {
	vector v(3, 4, 0);
	alert("Is unit", "" + v.is_unit); // false
	v.normalize();
	alert("Is unit", "" + v.is_unit); // true
}
is_zero

Whether the vector's length is 0, allowing for a tiny rounding error.

const bool vector::is_zero;

Remarks:

Use is_near_zero() if you need to choose how much error is allowed.

Example:
void main() {
	vector v(1, 2, 3);
	alert("Is zero", "" + v.is_zero); // false
	v.set_to_zero();
	alert("Is zero", "" + v.is_zero); // true
}
max_axis

The index of the vector's largest coordinate, 0 for x, 1 for y or 2 for z.

const int vector::max_axis;

Example:
void main() {
	vector v(4, -2, 9);
	alert("Largest axis", "" + v.max_axis); // 2
}
max_value

The largest of the vector's three coordinates.

const float vector::max_value;

Example:
void main() {
	vector v(4, -2, 9);
	alert("Largest", "" + v.max_value); // 9
}
min_axis

The index of the vector's smallest coordinate, 0 for x, 1 for y or 2 for z.

const int vector::min_axis;

Example:
void main() {
	vector v(4, -2, 9);
	alert("Smallest axis", "" + v.min_axis); // 1
}
min_value

The smallest of the vector's three coordinates.

const float vector::min_value;

Example:
void main() {
	vector v(4, -2, 9);
	alert("Smallest", "" + v.min_value); // -2
}
x

The x coordinate of the vector.

float vector::x;

Remarks:

Each of the three can also be read and written by index, v[0], v[1] and v[2].

Example:
void main() {
	vector v(1, 2, 3);
	v.x = 10;
	alert("Vector", "The x coordinate is now " + v.x);
}
y

The y coordinate of the vector.

float vector::y;

Example:
void main() {
	vector v(1, 2, 3);
	v.y = 10;
	alert("Vector", "The y coordinate is now " + v.y);
}
z

The z coordinate of the vector.

float vector::z;

Example:
void main() {
	vector v(1, 2, 3);
	v.z = 10;
	alert("Vector", "The z coordinate is now " + v.z);
}

Constants

Floating Point Limits

These constants describe the characteristics of the float and double types on the machine running your script. NVGT measures them when the engine starts, so they always reflect the real hardware. On every platform NVGT currently supports, both types follow the IEEE 754 standard, so the values given below are the ones you will see in practice.

float constants

double constants

Other constants

Rounding mode codes

The FLOAT_ROUNDING_MODE and DOUBLE_ROUNDING_MODE constants hold one of the following values:

A value of 3 or more means that underflow is gradual: values smaller than the minimum normalized value become denormals rather than flushing straight to 0.

Remarks

The epsilon constants are useful for comparing floating point values, which rarely compare exactly equal after arithmetic. Keep in mind that they describe the precision of values near 1, so for comparing large values you should scale your tolerance accordingly.

FLOAT_MAX also has a special meaning in some audio functions. The sound_play() function and audio_engine::play() method default their position argument to vector(FLOAT_MAX, FLOAT_MAX, FLOAT_MAX), which means that the sound should not be positioned in 3D space at all.

Example

void main() {
	double sum = 0;
	for (int i = 0; i < 10; i++) sum += 0.1;
	alert("Ten tenths exactly equal 1", sum == 1.0 ? "yes" : "no");
	alert("Ten tenths equal 1 within a small tolerance", abs(sum - 1.0) <= 10 * DOUBLE_EPSILON ? "yes" : "no");
	alert("Largest float", FLOAT_MAX);
}

Functions

abs

Returns the absolute value of a value.

double abs(double x);

Arguments:

Returns:

double: the absolute value of the given value.

Example:

void main() {
	alert("Example", "The absolute value of -5 is " + abs(-5));
}

acos

Returns the arc cosine of a value in radians.

double acos(double x);

Arguments:

Returns:

double: the arc cosine of the given value.

Example:

void main() {
    alert("Example", "The arc cosine of 0.5 is " + acos(0.5) + " radians");
}

asin

Returns the arc sine of a value in radians.

double asin(double x);

Arguments:

Returns:

double: the arc sine of the given value.

Example:

void main() {
    alert("Example", "The arc sine of 0.5 is " + asin(0.5) + " radians");
}

atan

Returns the arc tangent of a value in radians.

double atan(double x);

Arguments:

Returns:

double: the arc tangent of the given value.

Example:

void main() {
    alert("Example", "The arc tangent of 1 is " + atan(1) + " radians");
}

atan2

Returns the arc tangent of y/x, where y and x are the coordinates of a point.

double atan2(double y, double x);

Arguments:

Returns:

double: the arc tangent of y/x.

Example:

void main() {
    alert("Example", "The arc tangent of (1, 2) is " + atan2(1, 2) + " radians");
}

ceil

Returns the smallest integer greater than or equal to a value.

double ceil(double x);

Arguments:

Returns:

double: the smallest integer greater than or equal to x.

Example:

void main() {
	alert("Example", "The ceiling of 3.14 is " + ceil(3.14));
}

clamp

Restrict a number to a range.

double clamp(double value, double min, double max);

Arguments:

Returns:

double: value if it is within the range, otherwise the nearest end of it.

Remarks:

Integers and floats can be mixed freely in the arguments, so clamp(volume, 0, 1) keeps a fractional volume intact. The result can be assigned straight to an integer variable or used as an array index, though the compiler warns about the implicit conversion when warnings are enabled.

Example:

void main() {
	alert("Volume", "" + clamp(150, 0, 100)); // 100
	alert("Fraction", "" + clamp(0.5, 0, 1)); // 0.5
}

cos

Returns the cosine of an angle given in radians.

double cos(double x);

Arguments:

Returns:

double: the cosine of the angle.

Example:

void main() {
	alert("Example", "The cosine of 45 is " + cos(45 * 3.14159 / 180) + " radians");
}

cosh

Returns the hyperbolic cosine of a value.

double cosh(double x);

Arguments:

Returns:

double: the hyperbolic cosine of the given value.

Example:

void main() {
    alert("Example", "The hyperbolic cosine of 2 is " + cosh(2));
}

floor

Returns the largest integer less than or equal to a value.

double floor(double x);

Arguments:

Returns:

double: the largest integer less than or equal to x.

Example:

void main() {
	alert("Example", "The floor of 3.14 is " + floor(3.14));
}

log

Returns the natural logarithm (base e) of a value.

double log(double x);

Arguments:

Returns:

double: the natural logarithm of the given value.

Example:

void main() {
    alert("Example", "The natural logarithm of 10 is " + log(10));
}

log10

Returns the base 10 logarithm of a value.

double log10(double x);

Arguments:

Returns:

double: the base 10 logarithm of the given value.

Example:

void main() {
    alert("Example", "The base 10 logarithm of 100 is " + log10(100));
}

modf

Splits a value into its whole number and fractional parts.

  1. double modf(double num, double&out iptr);

  2. float modf(float num, float&out iptr);

Arguments:

Returns:

double/float: the fractional part of the value.

Remarks:

Both parts keep the sign of the original value, so splitting -3.75 gives a fractional part of -0.75 and a whole number part of -3.

Example:

void main() {
	double whole;
	double fraction = modf(3.75, whole);
	alert("Example", "3.75 has a whole number part of " + whole + " and a fractional part of " + fraction);
}

pow

Returns x raised to the power of y.

double pow(double x, double y);

Arguments:

Returns:

double: x raised to the power of y.

Example:

void main() {
    alert("Example", "2 raised to the power of 3 is " + pow(2, 3));
}

quaternion_from_axis_angle

Create a rotation around an axis.

quaternion@ quaternion_from_axis_angle(const vector&in axis, float angle);

Arguments:

Returns:

quaternion@: the rotation.

Remarks:

Angles are in radians. To convert degrees to radians multiply by 3.14159265 / 180.

Example:

void main() {
	quaternion turn = quaternion_from_axis_angle(vector(0, 0, 1), 90 * (3.14159265 / 180)); // 90 degrees around the up axis.
	alert("Rotation", "" + turn);
}

quaternion_from_euler_angles

Create a rotation from angles around each axis.

quaternion@ quaternion_from_euler_angles(const vector&in angles);

Arguments:

Returns:

quaternion@: the combined rotation.

Remarks:

When more than one angle is given the order the rotations are combined in matters. If you only need to turn around one axis, quaternion_from_axis_angle() is simpler.

Example:

void main() {
	quaternion tilt = quaternion_from_euler_angles(vector(0.5, 0, 0)); // About 29 degrees around the x axis.
	alert("Rotation", "" + tilt);
}

round

Returns the value of a number rounded to the nearest integer.

double round(double n, int p);

Arguments:

Returns:

double: The value of the number rounded to the nearest integer.

Example:

void main() {
	alert("Example", "Rounding 3.14159 to 2 decimal places gives " + round(3.14159, 2));
}

sin

Returns the sine of an angle given in radians.

double sin(double x);

Arguments:

Returns:

double: the sine of the angle.

Example:

void main() {
	alert("Example", "The sine of 45 is " + sin(45 * 3.14159 / 180) + " radians");
}

sinh

Returns the hyperbolic sine of a value.

double sinh(double x);

Arguments:

Returns:

double: the hyperbolic sine of the given value.

Example:

void main() {
    alert("Example", "The hyperbolic sine of 2 is " + sinh(2));
}

sqrt

Returns the square root of a value.

double sqrt(double x);

Arguments:

Returns:

double: the square root of the given value.

Example:

void main() {
	alert("Example", "The square root of 16 is " + sqrt(16));
}

tan

Returns the tangent of an angle given in radians.

double tan(double x);

Arguments:

Returns:

double: the tangent of the angle.

Example:

void main() {
	alert("Example", "The tangent of 45 is " + tan(45 * 3.14159 / 180) + " radians");
}

tanh

Returns the hyperbolic tangent of a value.

double tanh(double x);

Arguments:

Returns:

double: the hyperbolic tangent of the given value.

Example:

void main() {
    alert("Example", "The hyperbolic tangent of 2 is " + tanh(2));
}

tinyexpr

Evaluate a mathematical expression using the tinyexpr library.

double tinyexpr(const string&in expression);

Arguments:

Returns:

double: the result of the expression.

Example:

void main() {
	string expression = input_box("Expression", "Enter expression to evaluate");
	if (expression.is_empty()) exit();
	alert("Result", expression + "= " + tinyexpr(expression));
}

Global Properties

IDENTITY_QUATERNION

The identity quaternion, which represents no rotation at all.

const quaternion IDENTITY_QUATERNION;

Remarks:

The identity quaternion has x, y and z components of 0 and a w component of 1. Combining it with any other rotation leaves that rotation unchanged, so it is the usual starting orientation for objects.

A new quaternion with the same value is returned every time this property is read, so modifying the result does not affect the property.

Example:

void main() {
	quaternion q = IDENTITY_QUATERNION;
	alert("Identity quaternion", "x " + q.x + ", y " + q.y + ", z " + q.z + ", w " + q.w);
}

IDENTITY_TRANSFORM

The identity transform, which has no translation and no rotation.

const physics_transform IDENTITY_TRANSFORM;

Remarks:

The identity transform has a position of (0, 0, 0) and an orientation equal to IDENTITY_QUATERNION. It is commonly used as the starting transform for physics bodies and colliders.

A new transform with the same value is returned every time this property is read, so modifying the result does not affect the property.

Example:

void main() {
	physics_transform t = IDENTITY_TRANSFORM;
	vector p = t.position;
	alert("Identity transform position", p.x + ", " + p.y + ", " + p.z);
	quaternion r = t.rotation;
	alert("Identity transform rotation", "" + r);
}

VEC3_AXIS_X

A vector of length 1 that points along the x axis (right), vector(1, 0, 0).

const vector VEC3_AXIS_X;

Remarks:

A new vector is returned every time this property is read, so modifying the result does not change the property.

Example:

void main() {
	vector v = VEC3_AXIS_X;
	alert("X axis", "" + v.x);
}

VEC3_AXIS_Y

A vector of length 1 that points along the y axis (forward), vector(0, 1, 0).

const vector VEC3_AXIS_Y;

Remarks:

A new vector is returned every time this property is read, so modifying the result does not change the property.

Example:

void main() {
	vector v = VEC3_AXIS_Y;
	alert("Y axis", "" + v.y);
}

VEC3_AXIS_Z

A vector of length 1 that points along the z axis (up), vector(0, 0, 1).

const vector VEC3_AXIS_Z;

Remarks:

A new vector is returned every time this property is read, so modifying the result does not change the property.

Example:

void main() {
	vector v = VEC3_AXIS_Z;
	alert("Z axis", "" + v.z);
}

VEC3_ONE

A vector of all ones, vector(1, 1, 1).

const vector VEC3_ONE;

Remarks:

A new vector is returned every time this property is read, so modifying the result does not change the property.

Example:

void main() {
	vector v = VEC3_ONE;
	alert("One", "" + v.length());
}

VEC3_ZERO

A vector of all zeros, vector(0, 0, 0).

const vector VEC3_ZERO;

Remarks:

A new vector is returned every time this property is read, so modifying the result does not change the property.

Example:

void main() {
	vector v = VEC3_ZERO;
	alert("Zero", "" + v.length());
}