W O D   -   U S c r i p t

V   e   c   t   o   r   s
by   Windex

I n t r o d u c t i o n

Before you get too far into this, I should let you know that this is not a tutorial for someone just starting out in UnrealScript. If you're new to coding, you should check out the Beginner's Guide to UnrealScript. That ought to get you a good start.

Alright, with that out of the way, let's get down to the good stuff. Vectors are a very important part of UnrealScript, since they are responsible for controlling how objects move in the 3D world. If you fire a grenade from your Eightball gun, its path is determined by a vector. Anything you see moving in some way is most likely controlled by a vector. So what is a vector? Keep reading.

T h e   M y s t i c a l   V e c t o r

Well, not exactly mystical, but pretty damned cool, anyway. If you get right down to it, a vector in UnrealScript is simply a struct with three float components: X, Y, and Z. Sounds simple, eh? Well, those three little numbers can do quite a bit.

There are two different ways in which a vector can be used in UnrealScript. First, they can represent a position in the 3D world. X, Y, and Z: Just a point in 3D space. Second, a vector can be used to represent a direction and speed. A direction and speed, you ask? How in hell could 3 simple numbers tell all that? Well, understanding how they do tell all that is key to being successful when you work with vectors. Think of it like this: Say you have an object at position (1,1,1). You set that object's velocity to a vector of (0,0,1). After one unit of time the object's new position will be (1,1,2). How'd it get to (1,1,2)? Well, in that unit of time, the value of the movement vector was added on to the object's original position, giving a new position of (1,1,2). After another unit of time, the object's position will be (1,1,3), then (1,1,4), and so on. So, which direction is the object moving? Why, up, of course. It's Z value is continually getting bigger, while it's X and Y values stay the same.

So, there's the direction part. But what about speed? Well, what if the object's velocity was set to (0,0,5) instead of (0,0,1). Then, after one unit of time, the object's position would be (1,1,6). After two units of time, it would be (1,1,11). So, it's still moving in the same direction (up), it's just moving much more quickly. There's the speed part.

By supplying different values for these three numbers, you can make an object move in any direction at any speed. Conceptually, it's not too hard to see this. But how in the hell would you do the math to figure out what three numbers to use if you wanted an object to move at some obscure angle at some obscure speed? It's possible, obviously, but there must be an easier way. Fortunately, there is.

T h e   R o l e   o f   R o t a t o r s

Let's move away from the X, Y, Z view of vectors for a while, and take another look at them. When I picture a vector in my mind, I always think of an arrow. Where this arrow points indicates a direction, and the length of the arrow indicates a speed. The longer the arrow, the faster the object will move. I don't worry too much about the actual numbers of the vector, because I don't need to. There's an easier way to get to them.

You may have encountered rotators before. Like vectors, they're also structs with three float components: pitch, yaw, and roll. These three values are angles that can describe any rotational direction in 3D space. Pitch is up and down, yaw is side to side, and roll is... well, roll. Roll is unimportant when you're working with vectors, though, so don't worry about it too much. There's one important thing to remember about UnrealScript rotators, though. They're not measured in degrees, or even radians. For some reason, Epic chose to make up their own little system of angle measure. In this system, 360 degrees is about equal to 65535 Unreal angle units. Quite a big difference there. So don't get the two mixed up, or you'll spend hours trying to figure out why in the hell your setting of 60 degrees still looks like 0 in the game.

Now, say you're in a game, and the player is facing a certain direction. How would you find a vector that points 45 degrees to the player's right? Well, it's simple really.

   Vectors and Rotators

function VectorRotator()
	local rotator VectRot;
	local vector SomeVector;
	VectRot = Player.ViewRotation;
	VectRot.Yaw = VectRot.Yaw + 8192;
	SomeVector = Vector(VectRot);

Note that this is all assuming "Player" is an actor reference variable set equal to some PlayerPawn. Anyway, what's going on here? Well, first, VectRot is set equal to the player's view rotation. Then, 8192 (45 degrees) is added to its yaw, making it point 45 degrees to the player's right. Finally, SomeVector is set equal to "Vector(VectRot)". The Vector() function simply takes a rotator, and returns a vector pointing in the same direction as the rotator, with a length (speed) of one. So, now you have an easy way of getting a vector that points in any direction. But what about its speed? What if you want a vector with a speed of five instead of one? This is easy, too. Simply multiply the one-length vector by five. You'll get a vector pointing in the same direction, but with a length of five instead of one.

O t h e r   V e c t o r   O p e r a t i o n s

There are quite a few little tricks you can use to get the vector you're looking for. For instance, how the hell would you come up with a vector that points from one object to another? It's simple, really. You just have to know the trick. So, for your reading pleasure, here is the amazing and wonderful chart of useful UnrealScript vector operations.

Normal(Direction): Returns a vector pointing in the same direction as the vector you supply, but with a length of one.

VSize(Direction): Returns a floating point value equal to the length of the vector you supply.

Direction + Direction: Returns a direction vector which is equal to the bisector of the parallelogram formed by the original two direction vectors. What the bloody hell does this mean? Just take a look at the diagram.

As you can see, this is on a 2D plane, and we're talking about 3D space. If you think about it, though, two vectors in 3D space, no matter which direction they're pointing, can always be thought of as being on some 2D plane or another.

Location - Location: Gives you a direction vector which points from the second location to the first, and has a length equal to the distance between the two locations. This is not only useful for making one object move towards another, but also for determining the distance between things.

Location + Direction: Gives you a location equal to the new location an object would have after one unit of time with a velocity of the direction vector. If that made no sense whatsoever to you, think of it this way. Take the X component of the location vector, and add to it the X component of the direction vector. Do the same for the Y and Z components, and you'll end up with the location that's returned by this operation.

So, that's that. Pretty much everything you'll need to get a good start working with vectors in UnrealScript. If you've got any further questions, feel free to mail me at windex@planetunreal.com.

Visitors Since December 26, 2000