Standard Library#
A standard library of mathematics and printing for server side functions, and of the colors other services use.
Note
Vectors are position and direction tuples, and rotations are unit quaternion tuples in the order x, y, z, w the SpaceCenter service uses. Angles are in radians unless stated otherwise.
-
StdLib.pi:
number# The constant pi.
- Attribute:
Read-only, cannot be set
-
StdLib.e:
number# The constant e, the base of the natural logarithm.
- Attribute:
Read-only, cannot be set
-
staticmethod StdLib.abs(x:
number):number# The absolute value of a number.
- Parameters:
x – The number.
-
staticmethod StdLib.sign(x:
number):number# The sign of a number: -1 if it is negative, 1 if it is positive, and 0 if it is zero or not a number.
- Parameters:
x – The number.
-
staticmethod StdLib.copy_sign(x:
number, y:number):number# The magnitude of the first number with the sign of the second.
- Parameters:
x – The number giving the magnitude.
y – The number giving the sign.
-
staticmethod StdLib.is_nan(x:
number):boolean# Whether a number is not a number.
- Parameters:
x – The number.
-
staticmethod StdLib.is_finite(x:
number):boolean# Whether a number is neither infinite nor not a number.
- Parameters:
x – The number.
-
staticmethod StdLib.floor(x:
number):number# The largest whole number less than or equal to the given number.
- Parameters:
x – The number.
-
staticmethod StdLib.ceiling(x:
number):number# The smallest whole number greater than or equal to the given number.
- Parameters:
x – The number.
-
staticmethod StdLib.round(x:
number,digits?:number):number# The given number rounded to the given number of decimal places, with a halfway value rounded to the nearer even number.
- Parameters:
x – The number.
digits – The number of decimal places to round to. Negative to round to tens, hundreds and beyond. Defaults to
0.
Note
Throws an exception if the rounded value is too large for a double.
-
staticmethod StdLib.round_half_away_from_zero(x:
number,digits?:number):number# The given number rounded to the given number of decimal places, with a halfway value rounded away from zero.
- Parameters:
x – The number.
digits – The number of decimal places to round to. Negative to round to tens, hundreds and beyond. Defaults to
0.
Note
Throws an exception if the rounded value is too large for a double.
-
staticmethod StdLib.round_half_up(x:
number,digits?:number):number# The given number rounded to the given number of decimal places, with a halfway value rounded toward positive infinity.
- Parameters:
x – The number.
digits – The number of decimal places to round to. Negative to round to tens, hundreds and beyond. Defaults to
0.
Note
Throws an exception if the rounded value is too large for a double.
-
staticmethod StdLib.sqrt(x:
number):number# The square root of a number.
- Parameters:
x – The number.
-
staticmethod StdLib.hypot(x:
number, y:number):number# The length of the hypotenuse of a right triangle with the given sides.
- Parameters:
x – The first side.
y – The second side.
-
staticmethod StdLib.sin(x:
number):number# The sine of an angle, in radians.
- Parameters:
x – The angle.
-
staticmethod StdLib.cos(x:
number):number# The cosine of an angle, in radians.
- Parameters:
x – The angle.
-
staticmethod StdLib.tan(x:
number):number# The tangent of an angle, in radians.
- Parameters:
x – The angle.
-
staticmethod StdLib.asin(x:
number):number# The angle, in radians, whose sine is the given number.
- Parameters:
x – The number.
-
staticmethod StdLib.acos(x:
number):number# The angle, in radians, whose cosine is the given number.
- Parameters:
x – The number.
-
staticmethod StdLib.atan(x:
number):number# The angle, in radians, whose tangent is the given number.
- Parameters:
x – The number.
-
staticmethod StdLib.atan2(y:
number, x:number):number# The angle, in radians, between the positive x axis and the point
(x, y).- Parameters:
y – The y coordinate.
x – The x coordinate.
-
staticmethod StdLib.log(x:
number,new_base?:number):number# The logarithm of a number, natural unless another base is given.
- Parameters:
x – The number.
new_base – The base of the logarithm. Defaults to
2.718281828459045.
-
staticmethod StdLib.log10(x:
number):number# The base 10 logarithm of a number.
- Parameters:
x – The number.
-
staticmethod StdLib.exp(x:
number):number# The number e raised to the given power.
- Parameters:
x – The power.
-
staticmethod StdLib.min(x:
number, y:number):number# The smaller of two numbers.
- Parameters:
x – The first number.
y – The second number.
-
staticmethod StdLib.max(x:
number, y:number):number# The larger of two numbers.
- Parameters:
x – The first number.
y – The second number.
-
staticmethod StdLib.clamp(x:
number, min:number, max:number):number# The given number limited to the given range.
- Parameters:
x – The number.
min – The minimum of the range.
max – The maximum of the range.
Note
Throws an exception if the minimum is greater than the maximum.
-
staticmethod StdLib.lerp(a:
number, b:number, t:number):number# Linear interpolation between two numbers.
- Parameters:
a – The number when t is 0.
b – The number when t is 1.
t – The interpolation parameter.
Note
The parameter is not limited to the range 0 to 1, and a value outside it extrapolates.
-
staticmethod StdLib.inverse_lerp(a:
number, b:number, x:number):number# The interpolation parameter of a number between two others, limited to the range 0 to 1.
- Parameters:
a – The number at 0.
b – The number at 1.
x – The number.
Note
Returns 0 if the two numbers are equal.
-
staticmethod StdLib.degrees_to_radians(x:
number):number# The angle in radians of an angle in degrees.
- Parameters:
x – The angle in degrees.
-
staticmethod StdLib.radians_to_degrees(x:
number):number# The angle in degrees of an angle in radians.
- Parameters:
x – The angle in radians.
-
staticmethod StdLib.vector_add(u:
Tuple, v:Tuple):Tuple# The sum of two vectors.
- Parameters:
u – The first vector.
v – The second vector.
-
staticmethod StdLib.vector_subtract(u:
Tuple, v:Tuple):Tuple# The difference of two vectors.
- Parameters:
u – The first vector.
v – The second vector.
-
staticmethod StdLib.vector_scale(v:
Tuple, scale:number):Tuple# A vector scaled by a number.
- Parameters:
v – The vector.
scale – The scaling factor.
-
staticmethod StdLib.vector_dot(u:
Tuple, v:Tuple):number# The dot product of two vectors.
- Parameters:
u – The first vector.
v – The second vector.
-
staticmethod StdLib.vector_cross(u:
Tuple, v:Tuple):Tuple# The cross product of two vectors.
- Parameters:
u – The first vector.
v – The second vector.
-
staticmethod StdLib.vector_magnitude(v:
Tuple):number# The magnitude (length) of a vector.
- Parameters:
v – The vector.
-
staticmethod StdLib.vector_normalize(v:
Tuple):Tuple# A vector scaled to a magnitude of 1.
- Parameters:
v – The vector.
Note
Throws an exception if the vector is zero.
-
staticmethod StdLib.vector_distance(u:
Tuple, v:Tuple):number# The distance between two positions.
- Parameters:
u – The first position.
v – The second position.
-
staticmethod StdLib.vector_angle(u:
Tuple, v:Tuple):number# The angle between two vectors, in radians, between 0 and \(\pi\).
- Parameters:
u – The first vector.
v – The second vector.
Note
Throws an exception if either vector is zero.
-
staticmethod StdLib.vector_lerp(u:
Tuple, v:Tuple, t:number):Tuple# Linear interpolation between two vectors.
- Parameters:
u – The vector when t is 0.
v – The vector when t is 1.
t – The interpolation parameter.
Note
The parameter is not limited to the range 0 to 1, and a value outside it extrapolates.
-
staticmethod StdLib.quaternion_multiply(q:
Tuple, r:Tuple):Tuple# The composition of two rotations: the rotation r followed by the rotation q.
- Parameters:
q – The second rotation.
r – The first rotation.
Note
Throws an exception if either quaternion is zero.
-
staticmethod StdLib.quaternion_inverse(q:
Tuple):Tuple# The inverse of a rotation.
- Parameters:
q – The rotation.
Note
Throws an exception if the quaternion is zero.
-
staticmethod StdLib.quaternion_rotate_vector(q:
Tuple, v:Tuple):Tuple# A vector rotated by a rotation.
- Parameters:
q – The rotation.
v – The vector.
Note
Throws an exception if the quaternion is zero.
-
staticmethod StdLib.quaternion_from_axis_angle(axis:
Tuple, angle:number):Tuple# The rotation about an axis by an angle.
- Parameters:
axis – The axis of rotation.
angle – The angle of rotation, in radians.
Note
Throws an exception if the axis is zero.
-
staticmethod StdLib.quaternion_to_axis_angle(q:
Tuple):StdLib.AxisAngle# The axis and angle of a rotation.
- Parameters:
q – The rotation.
Note
Throws an exception if the quaternion is zero.
-
staticmethod StdLib.quaternion_from_to_rotation(u:
Tuple, v:Tuple):Tuple# The shortest rotation that takes one direction onto another.
- Parameters:
u – The direction to rotate from.
v – The direction to rotate to.
Note
Opposite directions give a half turn about an arbitrary perpendicular axis. Throws an exception if either vector is zero.
-
staticmethod StdLib.quaternion_look_rotation(forward:
Tuple, up:Tuple):Tuple# The rotation that takes the z axis onto a forward direction, and the y axis toward an up direction.
- Parameters:
forward – The direction of the z axis.
up – The direction the y axis points toward.
Note
The up direction need not be perpendicular to the forward direction. Throws an exception if either vector is zero, or if they are parallel.
-
staticmethod StdLib.quaternion_angle(q:
Tuple, r:Tuple):number# The angle of the rotation that takes one rotation to another, in radians, between 0 and \(\pi\).
- Parameters:
q – The first rotation.
r – The second rotation.
Note
Throws an exception if either quaternion is zero.
-
staticmethod StdLib.quaternion_slerp(q:
Tuple, r:Tuple, t:number):Tuple# Spherical linear interpolation between two rotations.
- Parameters:
q – The rotation when t is 0.
r – The rotation when t is 1.
t – The interpolation parameter.
Note
The parameter is not limited to the range 0 to 1, and a value outside it extrapolates. Throws an exception if either quaternion is zero.
-
staticmethod StdLib.print(message:
string)# Writes a message to the game’s log, for debugging.
- Parameters:
message – The message.
Note
A function evaluated by a stream or an event prints on every update. The message is written at the Info level, so a server log level of Warning or Error hides it.
- class StdLib.AxisAngle#
A rotation as an axis and an angle.
Note
-
axis:
Tuple# The axis of the rotation, as a unit vector, or zero for no rotation.
-
angle:
number# The angle of the rotation, in radians between 0 and \(\pi\).
-
axis:
- class StdLib.ColorRGB#
A color, as red, green and blue components between 0 and 1.
-
r:
number# The red component.
-
g:
number# The green component.
-
b:
number# The blue component.
-
r: