User Tools

Site Tools


mtr_addon:nte:js:math

Math Utilities

This is not an introduction to the math functions of the standard JavaScript library. Instead, this is a set of useful classes provided by NTE to perform transformations for model rendering and other things.

Vector3f

A three-dimensional vector, that is, the coordinates (x, y, z).

Functions And Objects Description
new Vector3f(x: float, y: float, z: float) Creates an instance of the Vector3f class.
Vector3f.x(): float Returns the X coordinate. Has counterparts for Y and Z.
Vector3f.copy(): Vector3f Returns a copy of the vector that can be independently modified.
Vector3f.normalize(): void Normalizes to a unit vector, that is, it preserves its direction, but its length is 1.
Vector3f.add(x: float, y: float, z: float): void Adds the given vector.
Vector3f.add(other: Vector3f): void Adds the given vector.
Vector3f.sub(other: Vector3f): void Subtracts the given vector.
Vector3f.mul(x: float, y: float, z: float): void Multiplies each coordinate by the specified numbers respectively.
Vector3f.mul(n: float): void Multiplies each coordinate by the specified number.
Vector3f.rot(axis: Vector3f, rad: float): void Rotates this vector in the specified direction relative to the coordinate origin. The angle is specified in radians, positive values are counterclockwise, and axis must be a unit vector.
Vector3f.rotDeg(axis: Vector3f, deg: float): void Does the same thing as Vector3f.rot(axis, rad), only using degrees instead of radians.
Vector3f.rotX(rad: float): void Rotates along the X axis, the angle is specified in radians. Has counterparts for Y and Z.
Vector3f.cross(other: Vector3f): void Performs a vector product with another vector. The result will be perpendicular to both vectors.
Vector3f.distance(other: Vector3f): float Returns the distance to the coordinates represented by the other vector.
Vector3f.distanceSq(other: Vector3f): float Returns the square of the distance to the coordinates represented by the other vector. The calculation is somewhat faster than in the previous case.
Vector3f.toBlockPos(): BlockPos Returns BlockPos from Minecraft with rounded vector values.
static Vector3f.ZERO: Vector3f Zero vector. You should not use functions on it.
static Vector3f.XP: Vector3f (1, 0, 0). Has counterparts for Y and Z.

Matrix4f

3D transformation matrix. Represents a transformation relationship that corresponds one point to another. For example, one can say “I want to rotate this model 90 degrees around the Y-axis and then move it 10 meters along the X-axis…” and such a set of transformation operations can be represented by a matrix.

Note that if you think along the lines of “I want to rotate this model 90 degrees around the Y-axis and then move it 10 meters along the X-axis…”, the transformations that will “happen first” will actually happen later. Transformations are actually functions that are called later. You can think of the function called later as being closer to the original model, and the function called earlier as being closer to the final result, so if you think of it in terms of the original position of the model, it will be transformed earlier. [Translator's note: I did understand the message but I didn't understand what the last part meant]

Scaling transformations are not supported.

Functions Description
new Matrix4f() Returns the Matrix4f. Initially it is a unit matrix that has not undergone any transformations.
Matrix4f.copy(): Matrix4f Returns a copy of the matrix that can be independently modified.
Matrix4f.translate(x: float, y: float, z: float): void Add a translation (x,y,z) transformation.
Matrix4f.rotate(axis: Vector3f, rad: float): void Adds a transformation that rotates around some direction at the origin. Angles are in radians, positive values are counterclockwise, and axis needs to be a unit vector.
Matrix4f.rotateX(rad: float): void Rotates along the X axis, the angle is specified in radians. Has counterparts for Y and Z.
Matrix4f.multiply(other: Vector3f): void Right-multiply another transformation matrix, which means that the transformation of that matrix is picked up after this one. [Translator's note: wth does that mean]
Matrix4f.transform(vec: Vector3f): Vector3f Calculates which coordinate a coordinate will go to after applying this transformation. Returns a new Vector3f without moving the input.
Matrix4f.transform3(vec: Vector3f): Vector3f Same as above, but only counts rotations not translations.
Matrix4f.getTranslationPart(): Vector3f What coordinate will be reached after performing this transformation relative to (0,0,0).
Matrix4f.asMoj(): ? Converts to the matrix type used by the original Minecraft.
static Matrix4f.translation(x: float, y: float, z: float): Matrix4f Returns a translation transformation matrix.

Matrices

When rendering, there will be a need to “restore the transformation state of the previous step”. For example, I will add a move-down transformation to render the bogie, but after rendering I will need to return to the pre-move transformation state to render other parts of the car.

Matrices implements a stack where multiple transformations can be stored. push and pop are performed in pairs.

Functions Description
new Matrices() Returns Matrices. Initially there is only one unit matrix.
Matrices.translate(x: float, y: float, z: float): void Adds a (x,y,z) translation transformation to the current state.
Matrices.rotate(axis: Vector3f, rad: float): void Adds a transformation to the current state that rotates around some direction at the origin. The angle is specified in radians, positive values are counterclockwise, and axis must be a unit vector.
Matrices.rotateX(rad: float): void Rotates along the X axis, the angle is specified in radians. Has counterparts for Y and Z.
Matrices.last(): Matrix4f Returns the current state.
Matrices.pushPose(): void Places a copy of the current state on the stack. That is, a copy of the current state is saved.
Matrices.popPose(): void Deletes the last element of the stack. That is, it resets the current state and then restores the last saved state as the last state.
Matrices.popPushPose(): void It deletes first and then makes a copy of the saved state.
Matrices.clear(): boolean Leaves only one state.
Matrices.setIdentity(): void Resets the current state to a unit matrix.

Source

mtr_addon/nte/js/math.txt · Last modified: 2024/02/06 13:07 by weryskok