Unit a3dge.Math3D

Uses
Classes, Interfaces, Objects and Records
Variables

Description

Some useful mathematic functions.

This unit is mostly for internal use but it is also useful in some cases; for example if you're building or modifying your own 3D geometry.

To use these structs and routines you should have some knowledge of Euclidean Geometry and Linear algebra, matrices, vectors and such.

Remember: OpenGL uses degrees, not radians.

Overview

Functions and Procedures

function Vector3D (const aX, aY, aZ: GLfloat): TVector3D; inline;
function Vector4D (const aX, aY, aZ, aW: GLfloat): TVector4D; inline;
function AreSameVector3D (const aV1, aV2: TVector3D): Boolean; inline;
function AreDifferentVector3D (const aV1, aV2: TVector3D): Boolean; inline;
FUNCTION GetVectorLength (CONST Vector: TVector3D): SINGLE; INLINE;
FUNCTION GetVectorLength2 (CONST Vector: TVector3D): SINGLE; INLINE;
FUNCTION GetNormalizedVector (CONST Vector: TVector3D): TVector3D;
function GetOppositeVector (const aVector: TVector3D): TVector3D;
FUNCTION AddVectors (CONST V1, V2: TVector3D): TVector3D; INLINE;
FUNCTION SubVectors (CONST V1, V2: TVector3D): TVector3D; INLINE;
function MultiplyVector (const aVector: TVector3D; const aValue: GLfloat) : TVector3D; inline;
function DivideVector (const aVector: TVector3D; const aValue: GLfloat) : TVector3D; inline;
FUNCTION VectorAsString (CONST Vector: TVector3D): STRING;
FUNCTION GetCrossProduct (CONST aV1, aV2: TVector3D): TVector3D;
FUNCTION GetDotProduct (CONST aV1, aV2: TVector3D): SINGLE;
function GetVectorLerp (const V1, V2: TVector3D; const T: Real): TVector3D;
FUNCTION GetPlaneNormal (CONST v1, v2, v3: TVector3D): TVector3D;
FUNCTION GetDistance (CONST P1, P2: TVector3D): SINGLE;
FUNCTION GetDistance2 (CONST P1, P2: TVector3D): SINGLE;
FUNCTION GetDistance2Segment (CONST P, A, B: TVector3D): SINGLE;
FUNCTION GetDistance2Segment2 (CONST P, A, B: TVector3D): SINGLE;
PROCEDURE ScaleVectorList ( var VectorList: TVector3DList; CONST Scale: SINGLE ); OVERLOAD;
PROCEDURE ScaleVectorList ( var VectorList: TVector3DList; CONST Scale: TVector3D ); OVERLOAD;
function IdentityMatrix: TMatrix3D;
function GetTranslationMatrix (const x, y, z: GLfloat): TMatrix3D; overload;
function GetTranslationMatrix (const V: TVector3D): TMatrix3D; overload;
function GetScalingMatrix (const x, y, z: GLfloat): TMatrix3D; overload;
function GetScalingMatrix (const S: TVector3D): TMatrix3D; overload;
function GetXRotateMatrix (const r: GLfloat): TMatrix3D;
function GetYRotateMatrix (const r: GLfloat): TMatrix3D;
function GetZRotateMatrix (const r: GLfloat): TMatrix3D;
function GetRotationMatrix (const x, y, z: GLfloat): TMatrix3D; overload;
function GetRotationMatrix (const R: TVector3D): TMatrix3D; overload;
function GetTransformationMatrix ( const aScale, aXrot, aYrot, aZrot, aXtrans, aYtrans, aZtrans: GLfloat ): TMatrix3D; overload;
function GetTransformationMatrix ( const aScale: GLfloat; aRot, aTrans: TVector3D ): TMatrix3D; overload;
function GetTransformationMatrix ( const aScaleX, aScaleY, aScaleZ, aXrot, aYrot, aZrot, aXtrans, aYtrans, aZtrans: GLfloat ): TMatrix3D; overload;
function GetTransformationMatrix ( const aScale, aRot, aTrans: TVector3D ): TMatrix3D; overload;
function MultiplyMatrix (const A, B: TMatrix3D): TMatrix3D;
function MultiplyVectorMatrix ( const aM: TMatrix3D; const aV: TVector3D ): TVector3D; inline;
function RotateVector (const aM: TMatrix3D; const aV: TVector3D): TVector3D; inline;
procedure SetGLMatrix (const aM: TMatrix3D);
FUNCTION GetRelativeDirection ( CONST Chaser, Target: TVector3D; CONST ChaserAngle: SINGLE ): INTEGER; INLINE;
FUNCTION GetBoundingSphereRadius (CONST VectorList: TVector3DList): SINGLE;

Types

TVector3D = ARRAY [Vx..Vz] OF GLfloat;
TVector3DList = ARRAY OF TVector3D;
TVector4D = ARRAY [Vx..Vw] OF GLfloat;
TMatrix3D = ARRAY [0..15] OF GLfloat;

Constants

Deg2Rad = ALLEGRO_TAU / 360;
Rad2Deg = 360 / ALLEGRO_TAU;
Vx = 0;
Vy = 1;
Vz = 2;
Vw = 3;
Vector0: TVector3D = (0 ,0 ,0);
VectorFront: TVector3D = (0, 0, -1);

Description

Functions and Procedures

function Vector3D (const aX, aY, aZ: GLfloat): TVector3D; inline;

Build a 3D vector.

See also
Vector4D
Build a 4D vector.
GetOppositeVector
Calculate the opposite vector.
function Vector4D (const aX, aY, aZ, aW: GLfloat): TVector4D; inline;

Build a 4D vector.

See also
Vector3D
Build a 3D vector.
function AreSameVector3D (const aV1, aV2: TVector3D): Boolean; inline;

Check if vectors are the same.

See also
AreDifferentVector3D
Check if vectors are different.
function AreDifferentVector3D (const aV1, aV2: TVector3D): Boolean; inline;

Check if vectors are different.

See also
AreSameVector3D
Check if vectors are the same.
FUNCTION GetVectorLength (CONST Vector: TVector3D): SINGLE; INLINE;

Calculate length of the vector.

If you only need to compare lengths of vectors, you can use GetVectorLength2 wich is faster.

Parameters
Vector
A vector.
Returns

Length Vector.

See also
GetVectorLength2
Calculate the squared length of the vector.
GetNormalizedVector
Return the given vector with a magitude (length) of 1.
FUNCTION GetVectorLength2 (CONST Vector: TVector3D): SINGLE; INLINE;

Calculate the squared length of the vector.

To calculate vector length a square root is used, wich is slow. Calculating the squared length is quite faster as it only uses addition and multiplication. Since squares are proportional to the base, compare squared lengths have the same result than compare lengths but faster.

Parameters
Vector
A vector.
Returns

Squared length Vector.

See also
GetVectorLength
Calculate length of the vector.
FUNCTION GetNormalizedVector (CONST Vector: TVector3D): TVector3D;

Return the given vector with a magitude (length) of 1.

If vector is too small then vector will became a zero vector (i.e. a vector with all components equal zero).

Parameters
Vector
A vector.
Returns

A vector in the same direction than Vector but length 1 (one).

See also
GetVectorLength
Calculate length of the vector.
function GetOppositeVector (const aVector: TVector3D): TVector3D;

Calculate the opposite vector.

This returns a vector with same direction and magnitude but opposite direction.

FUNCTION AddVectors (CONST V1, V2: TVector3D): TVector3D; INLINE;

Add vectors.

See also
SubVectors
Substract vectors.
MultiplyVector
Multiply a vector by a scalar.
DivideVector
Divide a vector by a scalar.
FUNCTION SubVectors (CONST V1, V2: TVector3D): TVector3D; INLINE;

Substract vectors.

See also
AddVectors
Add vectors.
MultiplyVector
Multiply a vector by a scalar.
DivideVector
Divide a vector by a scalar.
function MultiplyVector (const aVector: TVector3D; const aValue: GLfloat) : TVector3D; inline;

Multiply a vector by a scalar.

See also
AddVectors
Add vectors.
SubVectors
Substract vectors.
DivideVector
Divide a vector by a scalar.
function DivideVector (const aVector: TVector3D; const aValue: GLfloat) : TVector3D; inline;

Divide a vector by a scalar.

See also
AddVectors
Add vectors.
SubVectors
Substract vectors.
MultiplyVector
Multiply a vector by a scalar.
FUNCTION VectorAsString (CONST Vector: TVector3D): STRING;

Build a string with vector values.

Useful for logging and debugging.

FUNCTION GetCrossProduct (CONST aV1, aV2: TVector3D): TVector3D;

Calculate the cross product between the given vectors.

Parameters
aV1
A vector. No need to be normalized.
aV2
A vector. No need to be normalized.
Returns

A vector that is perpendicular to both aV1 and aV2.

See also
GetDotProduct
Calculates dot product of given vectors.
GetNormalizedVector
Return the given vector with a magitude (length) of 1.
GetPlaneNormal
Get the normal of the plane defined by the given vertices.
FUNCTION GetDotProduct (CONST aV1, aV2: TVector3D): SINGLE;

Calculates dot product of given vectors.

For normalized vectors, the result is proportional to the angle between both vectors, returning 1 if both point in the same direction, 0 if they're perpendicular or -1 if they're in opposite directions.

See also
GetCrossProduct
Calculate the cross product between the given vectors.
GetPlaneNormal
Get the normal of the plane defined by the given vertices.
GetNormalizedVector
Return the given vector with a magitude (length) of 1.
function GetVectorLerp (const V1, V2: TVector3D; const T: Real): TVector3D;

Calculate linear interpolation between given vectors.

This is most commonly used to find a point some fraction of the way along a line between two endpoints (e.g. to move an object gradually between those points).

Parameters
V1
Start value, returned when T is 0.
V2
End value, returned when T is 1.
T
Value used to interpolate between V1 and V2.
Returns

Interpolated value, equals to ((V2 - V1) * T) + V1.

FUNCTION GetPlaneNormal (CONST v1, v2, v3: TVector3D): TVector3D;

Get the normal of the plane defined by the given vertices.

The vectors passed as parameters are points in the plane. Such points should be passed in clockwise order.

Parameters
v1
A vector representing a point in a plane.
v2
A vector representing a point in a plane.
v3
A vector representing a point in a plane.
Returns

A vector that is perpendicular to the plane defined by the parameters.

See also
GetCrossProduct
Calculate the cross product between the given vectors.
FUNCTION GetDistance (CONST P1, P2: TVector3D): SINGLE;

Calculate the distace between two points.

See also
GetDistance2
Calculate the square distance between two points.
GetDistance2Segment
Calculate the distance from point P to line segment A - B.
FUNCTION GetDistance2 (CONST P1, P2: TVector3D): SINGLE;

Calculate the square distance between two points.

This function is faster than GetDistance, so compare square of distance is faster than compare distance.

This function can be used to check sphere collisions:

type
  TSphere = record
    Center: TVector3D;
    Radius: Single
  end;

function CheckSphereCollision (aSphere1, aSphere2: TSphere): Boolean;
begin
  Result := GetDistance2 (aSphere1.Center, aSphere2.Center) <= Sqr (aSphere1.Radius + aSphere2.Radius)
end;

See also
GetDistance
Calculate the distace between two points.
FUNCTION GetDistance2Segment (CONST P, A, B: TVector3D): SINGLE;

Calculate the distance from point P to line segment A - B.

See also
GetDistance2Segment2
Calculate the square of the distance from point P to line segment A - B.
GetDistance
Calculate the distace between two points.
FUNCTION GetDistance2Segment2 (CONST P, A, B: TVector3D): SINGLE;

Calculate the square of the distance from point P to line segment A - B.

This function is faster than GetDistance2Segment, so compare square of distance is faster than compare distance.

It can be used to check for collisions but it is slower than sphere or bounding-box collision algorithms.

See also
GetDistance2Segment
Calculate the distance from point P to line segment A - B.
GetDistance2
Calculate the square distance between two points.
PROCEDURE ScaleVectorList ( var VectorList: TVector3DList; CONST Scale: SINGLE ); OVERLOAD;

Scale all vertex of the list.

PROCEDURE ScaleVectorList ( var VectorList: TVector3DList; CONST Scale: TVector3D ); OVERLOAD;

Scale all vertex of the list.

function IdentityMatrix: TMatrix3D;

Build the identity matrix.

See also
GetTransformationMatrix
Construct a full 3D transformation matrix.
function GetTranslationMatrix (const x, y, z: GLfloat): TMatrix3D; overload;

Construct a 3D translation matrix.

function GetTranslationMatrix (const V: TVector3D): TMatrix3D; overload;

Construct a 3D translation matrix.

See also
GetScalingMatrix
Construct a 3D scaling matrix.
GetRotationMatrix
Construct a 3D translation matrix that rotate points around all three axis by the specified amount (given in degrees).
GetTransformationMatrix
Construct a full 3D transformation matrix.
MultiplyVectorMatrix
Multiply the given vector with the given matrix.
function GetScalingMatrix (const x, y, z: GLfloat): TMatrix3D; overload;

Construct a 3D scaling matrix.

function GetScalingMatrix (const S: TVector3D): TMatrix3D; overload;

Construct a 3D scaling matrix.

See also
GetTranslationMatrix
Construct a 3D translation matrix.
GetRotationMatrix
Construct a 3D translation matrix that rotate points around all three axis by the specified amount (given in degrees).
GetTransformationMatrix
Construct a full 3D transformation matrix.
MultiplyVectorMatrix
Multiply the given vector with the given matrix.
function GetXRotateMatrix (const r: GLfloat): TMatrix3D;

Construct a 3D tranformation matrix which will rotate points around the x axis by the specified amount (given in degrees).

See also
GetYRotateMatrix
Construct a 3D tranformation matrix which will rotate points around the t axis by the specified amount (given in degrees).
GetZRotateMatrix
Construct a 3D tranformation matrix which will rotate points around the z axis by the specified amount (given in degrees).
GetRotationMatrix
Construct a 3D translation matrix that rotate points around all three axis by the specified amount (given in degrees).
GetTransformationMatrix
Construct a full 3D transformation matrix.
MultiplyVectorMatrix
Multiply the given vector with the given matrix.
function GetYRotateMatrix (const r: GLfloat): TMatrix3D;

Construct a 3D tranformation matrix which will rotate points around the t axis by the specified amount (given in degrees).

See also
GetXRotateMatrix
Construct a 3D tranformation matrix which will rotate points around the x axis by the specified amount (given in degrees).
GetZRotateMatrix
Construct a 3D tranformation matrix which will rotate points around the z axis by the specified amount (given in degrees).
GetRotationMatrix
Construct a 3D translation matrix that rotate points around all three axis by the specified amount (given in degrees).
GetTransformationMatrix
Construct a full 3D transformation matrix.
MultiplyVectorMatrix
Multiply the given vector with the given matrix.
function GetZRotateMatrix (const r: GLfloat): TMatrix3D;

Construct a 3D tranformation matrix which will rotate points around the z axis by the specified amount (given in degrees).

See also
GetXRotateMatrix
Construct a 3D tranformation matrix which will rotate points around the x axis by the specified amount (given in degrees).
GetYRotateMatrix
Construct a 3D tranformation matrix which will rotate points around the t axis by the specified amount (given in degrees).
GetRotationMatrix
Construct a 3D translation matrix that rotate points around all three axis by the specified amount (given in degrees).
GetTransformationMatrix
Construct a full 3D transformation matrix.
MultiplyVectorMatrix
Multiply the given vector with the given matrix.
function GetRotationMatrix (const x, y, z: GLfloat): TMatrix3D; overload;

Construct a 3D translation matrix that rotate points around all three axis by the specified amount (given in degrees).

function GetRotationMatrix (const R: TVector3D): TMatrix3D; overload;

Construct a 3D translation matrix that rotate points around all three axis by the specified amount (given in degrees).

See also
GetXRotateMatrix
Construct a 3D tranformation matrix which will rotate points around the x axis by the specified amount (given in degrees).
GetYRotateMatrix
Construct a 3D tranformation matrix which will rotate points around the t axis by the specified amount (given in degrees).
GetZRotateMatrix
Construct a 3D tranformation matrix which will rotate points around the z axis by the specified amount (given in degrees).
GetScalingMatrix
Construct a 3D scaling matrix.
GetTranslationMatrix
Construct a 3D translation matrix.
GetTransformationMatrix
Construct a full 3D transformation matrix.
RotateVector
The same than MultiplyVectorMatrix but assumes that matrix is rotation only.
MultiplyVectorMatrix
Multiply the given vector with the given matrix.
function GetTransformationMatrix ( const aScale, aXrot, aYrot, aZrot, aXtrans, aYtrans, aZtrans: GLfloat ): TMatrix3D; overload;

Construct a full 3D transformation matrix.

function GetTransformationMatrix ( const aScale: GLfloat; aRot, aTrans: TVector3D ): TMatrix3D; overload;

Construct a full 3D transformation matrix.

function GetTransformationMatrix ( const aScaleX, aScaleY, aScaleZ, aXrot, aYrot, aZrot, aXtrans, aYtrans, aZtrans: GLfloat ): TMatrix3D; overload;

Construct a full 3D transformation matrix.

function GetTransformationMatrix ( const aScale, aRot, aTrans: TVector3D ): TMatrix3D; overload;

Construct a full 3D transformation matrix.

See also
IdentityMatrix
Build the identity matrix.
GetTranslationMatrix
Construct a 3D translation matrix.
GetScalingMatrix
Construct a 3D scaling matrix.
GetRotationMatrix
Construct a 3D translation matrix that rotate points around all three axis by the specified amount (given in degrees).
MultiplyVectorMatrix
Multiply the given vector with the given matrix.
function MultiplyMatrix (const A, B: TMatrix3D): TMatrix3D;

Multiply two matrices.

See also
TMatrix3D
A matrix in array form.
function MultiplyVectorMatrix ( const aM: TMatrix3D; const aV: TVector3D ): TVector3D; inline;

Multiply the given vector with the given matrix.

Parameters
aM
Matrix. Assumes row 4 of [0 0 0 1].
aV
Vector.
Returns

The transformed vector.

See also
SetGLMatrix
Replace current OpenGL matrix with the given one.
GetTransformationMatrix
Construct a full 3D transformation matrix.
RotateVector
The same than MultiplyVectorMatrix but assumes that matrix is rotation only.
function RotateVector (const aM: TMatrix3D; const aV: TVector3D): TVector3D; inline;

The same than MultiplyVectorMatrix but assumes that matrix is rotation only.

procedure SetGLMatrix (const aM: TMatrix3D);

Replace current OpenGL matrix with the given one.

Note this will replace the current OpenGL matrix stack.

FUNCTION GetRelativeDirection ( CONST Chaser, Target: TVector3D; CONST ChaserAngle: SINGLE ): INTEGER; INLINE;

Returns the direction where the Target relative to the Chaser.

Note this function works on the x/z plane only.

Parameters
Chaser
Position of the chaser object.
Target
Position of the target object.
Direction
The x/z angle the Chaser is facing.
Returns

A value in the interval 0..7 where 0 is in front of, 1 is in the front/right, 2 is on the right and so one.

FUNCTION GetBoundingSphereRadius (CONST VectorList: TVector3DList): SINGLE;

Calculate the radius of the sphere that encloses all points in the list.

Types

TVector3D = ARRAY [Vx..Vz] OF GLfloat;

A 3D vector.

A TVector3D can be used to represent a point, a direction or an axis.

See also
Vector3D
Build a 3D vector.
GetOppositeVector
Calculate the opposite vector.
TVector3DList
List of vectors.
TMatrix3D
A matrix in array form.
GetVectorLength
Calculate length of the vector.
GetNormalizedVector
Return the given vector with a magitude (length) of 1.
AddVectors
Add vectors.
TVector3DList = ARRAY OF TVector3D;

List of vectors.

TVector4D = ARRAY [Vx..Vw] OF GLfloat;

A 4D vector.

A TVector4D can be used to represent a plane, where components Vx, Vy and Vz is the plane normal and Vw is the distance to the origin.

See also
Vector4D
Build a 4D vector.
TVector3D
A 3D vector.
TMatrix3D = ARRAY [0..15] OF GLfloat;

A matrix in array form.

You can multiply two matrices together to produce a third matrix, and this will have the same effect on points as applying the original two matrices one after the other. For example, if you have one matrix that rotates a point and another that shifts it sideways, you can combine them to produce a matrix that will do the rotation and the shift in a single step. You can build up extremely complex transformations in this way, while only ever having to multiply each point by a single matrix.

This array is zero based and the order it is stored is the one OpenGL expects:

|  0  4  8 12 |
|  1  5  9 13 |
|  2  6 10 14 |
|  3  7 11 15 | <- Note A3DGE doesn't use this row!
            ˆ----- Translation column.

So to acces to element M(m,n) use M[((n - 1) * 4) + (m - 1)].

A3DGE assumes row 4 values are { 0 0 0 1 } in any case so you can ignore such row.

See also
IdentityMatrix
Build the identity matrix.
GetTransformationMatrix
Construct a full 3D transformation matrix.
TVector3D
A 3D vector.
MultiplyVectorMatrix
Multiply the given vector with the given matrix.

Constants

Deg2Rad = ALLEGRO_TAU / 360;

Value to convert degrees to radians.

Usage

Radians := Degrees * Deg2Rad;

See also
Rad2Deg
Value to convert radians to degrees.
Rad2Deg = 360 / ALLEGRO_TAU;

Value to convert radians to degrees.

Usage

Degrees := Radians * Deg2Rad;

See also
Deg2Rad
Value to convert degrees to radians.
Vx = 0;

Identifies the x component of a vector.

Vy = 1;

Identifies the y component of a vector.

Vz = 2;

Identifies the z component of a vector.

Vw = 3;

Identifies the w component of a vector.

Vector0: TVector3D = (0 ,0 ,0);

Vector zero. For example, the center of an space.

See also
VectorFront
Vector that points to the "front" from a center.
VectorFront: TVector3D = (0, 0, -1);

Vector that points to the "front" from a center.

See also
Vector0
Vector zero.

Generated by PasDoc 0.15.0. Generated on 2025-07-31 11:41:01.