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. VectorsIn 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.
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. RotatorsRotators 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:
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:
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 FunctionsAll the standard scalar math operators apply to vectors, e.g. +, -, *, /, <<, >>.The function VSize returns the magnitude of a vector:
A normalised vector is used only to describe a direction, it has a magnitude of one:
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:
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 |