Thursday, 3 May 2018

Delphi 10.2 TQuaternion3D Example

Delphi’s System.Math.Vectors unit contains the TQuaternion3D class.

There is no information about the class in the Help so I post some code here so I remember how to use the class.

Following are some code snippets showing how to use quaternions to transform the mesh vertices in a model according to a skeletal hierarchy.

The procedures are used to build the models from old classic Tomb Raider games and export them to a 3D modelling program.

In Tomb Raider the animation data is stored as an (x,y,z) offset for the root bone and YXZ Euler angles for the rotations of all the bones.

In Tomb Raider a right handed coordinate system is used but the –Y axis is up so usually a further transformation is needed to get the models the right way up in other programs.

Here I do a 180 degree rotation about the X axis.

It is important to do this transformation last since then you don’t have to worry about adjusting the angles or offsets.

The code snippets also use System.Generics.Collections TStack and TList.

I also include a small example for the System.Math.Vectors TMatrix3D class which similarly has no Help topic.