news | about | forums | links | mods | tools | tutorials | contact

Vectors & Rotators - author: [mr.self-destruct]
An understanding of basic mechanics is crucial to understanding how (and why) the Unreal engine handles objects in 3D space the way it does.

#### Vectors

In mechanics there are vector and scalar quantities. A scalar has only magnitude, i.e. a numerical value indicating its size. Example scalars are time and mass.
Vectors possess both magnitude and direction, example vectors include velocity, momentum and any force. Any vector can be represented by an arrow with its length giving its magnitude and its direction given by the direction of the arrow, for example:

A vector can be thought of as having several parts, or components. A vector will have a component for each axis, i.e. a 2D vector has two components; horizontal and vertical. The process of dividing a vector into its components is known as resolving:

We can perform mathmatical operations on vectors, just as we can with scalars. Vector math is more complicated than scalar math, because we must take into account the direction of the vectors:

In the diagram above two forces are acting on an object, because they're both in the same direction they reinforce each other, so the resultant force is 60N to the right.

In this case a force of 30N is acting to oppose the 60N force towards the right, so the resultant force is 30N to the right.
Generally vector math is performed by resolving the vectors and calculating the resultant for each component. The Unreal engine does this all automatically for us though, so the amount of math involved when using vectors in UnrealScript is quite low from the programmer's point of view.
So how does all this apply to the Unreal engine? Well, Unreal has a pre-defined data structure called a vector, which is declared as follows:

// A point or direction vector in 3D space struct Vector { var() config float X, Y, Z; }

So we have a 3D vector, stored in the form of its components. With Unreal Z is the vertical, X depth and Y the horizontal:

Unreal uses the vector structure for two purposes. The first is to specify the location of an object in 3D space, so X, Y, Z become the co'ordinates of an object. The vector structure is also used as a vector, such as in the case of an Actor's Velocity, it represents the direction and speed at which the Actor is travelling. So X, Y, Z are the components of the vector.

#### Rotators

Rotators are used to handle angles in Unreal. Rotators control the direction an Actor is facing (and thus the orientation of its mesh) and rotational movement. The rotator data structure is declared as follows:

// An orthogonal rotation in 3D space struct Rotator { var() config int Pitch, Yaw, Roll; }

Pitch is vertical rotation (the same as tilting a plane up or down), Yaw controls horizontal rotation (using the rudder to turn the plane side to side) and Roll (same movement as doing a barrel roll). Perhaps not the best explanation you'll find, but if you've used a flight sim you'll know what I mean ;)
With rotators a value of 65535 is a full circle. But in mechanics angles are measured in radians, and UnrealScript's trigonometric functions work in radians. So to do any kind of math with rotators we must convert into radians. One radian is defined as the angle subtended at the centre of a circle by an arc whose length is equal to the radius:

To convert between radians and degrees use the following formulae:

degrees = radians x 180 / Π radians = degrees / 180 x Π

There are 2Π radians in a full circle, this means that there are `65535 / 2Π = 10430.2192` Unreal Units to the radian.
With the Unreal engine it is possible to convert between vectors and rotators. So we could create a vector pointing in the same direction a rotator we've created and vice versa. The only problem with this is that rotators have no magnitude as such, so if we convert a rotator into a vector we must give it a magnitude:

vector myVector; rotator myRotator; // Facing 90 degrees up myRotator = rot(16384, 0, 0); // Facing 90 degrees up with a magnitude of 20
myVector = 20 * vector(myRotator);

#### Vector Functions

All the standard scalar math operators apply to vectors, e.g. +, -, *, /, <<, >>.

The function VSize returns the magnitude of a vector:

var vector A; var float B; // A line along the horizontal axis A = vect(0, 20, 0); // Magnitude of vector A
B = VSize(A);

A normalised vector is used only to describe a direction, it has a magnitude of one:

var vector A, B; // A vector A = vect(20, 100, 10); // The normal of vector A B = Normal(A);

The dot product of two normalised vectors is the angle between them. The UnrealScript function dot returns the cosine of the angle between two vectors:

var vector A, B; var float Theta; Theta = Normal(A) dot Normal(B);

suggestions? questions? comments? please send them all to ray@beyondunreal.com

copyright 2001 ray davis. this site and all content belongs to me unless otherwise noted, with the exception of some tutorials. the authors retain the rights to their material, and are hosted here with their express permission. everything else is mine though, so don't steal it. bastards.
privacy