Angles, Vectors and Traces

An understanding of how quake deals with angles, traces and vectors is important if you are going to do much, especially if you are going to be coding weapons. So here goes.


vectors, angles and coordinates are all stored in a vec3_t datatype. A vec3_t is simply an array of 3 floats; So if you want to take a look inside it you can just use the sub parts of it. i.e. if you had a vec3_t called origin you could access each of the numbers within it with origin[0] , origin[1] and origin[2]. The fact it is an array is the reason you can't just equate two vec3_t's and have to use VectorCopy(Vec3_t1, vec3_t2); I'll put a list of functions affecting vec3_t's at the bottom of the page, with a quick desciption of each and how to use it.

Now to definitions:

Vector

A vector is a set of 3 lengths on the x,y and z axis which define a line from one point to another. A vector does not define a point in space merely a translation from one point to another.a vector {5,5,5} says go 5 units left, 5 units up and 5 units forward. it is relative to where you are at the time.

Coordinates

3 numbers deinfing a unique point in space. Similar to a vector but it is relative to a fixed point (the origin) not where you are at the moment

Angles

3 numbers defining the angle rotated round each axis.(I don't know what the units are for the angles)


Now what i've told you above is all well and good, but you have to be able to use it. And one of the primary uses is in doing a trace. All a trace does look at everything between 2 sets of coordinates and returns what it hits.The first thing you have to do is understand the call to the trace function. an example is below

        tr = gi.trace (start, bound1, bound2, end, ignore_this,mask_this);

tr is a trace_t structure that holds all the return information. I'll get back to it. start and end are coordinates. start is where the trace starts and end is where it will stop unless it hits something first. bound1 and bound2 define the box around the line allowing a box to be checked instead of just a line. if they are NULL only the line is checked. they are vec3_t 's. ignore_this is an entity that the trace is to ignore. Main place i've seen this used is in the railgun, where it can go through someone. mask_this defines what the trace will stop for. A list of values for this is below.

Value

Effect

MASK_ALL

stop on anything

MASK_SOLID

solid wall

MASK_PLAYERSOLID

solid wall or player or non-solid monster?

MASK_DEADSOLID

solid wall or player

MASK_MONSTERSOLID

solid wall or monster

MASK_WATER

any liquid

MASK_OPAQUE

walls and liquid except water (see thru)

MASK_SHOT

anything hit by weapons

MASK_CURRENT

current contents?

Results from the trace are returned in a trace_t structure. this has the following parts

Section

Value and type

.allsolid

boolean - if true, entire trace was in a wall

.startsolid

boolean - if true trace started in a wall

.fraction

Fraction of trace complete. (1 = trace didn't hit anything)

.endpos

Vec3_t - coords of end point

.plane

surface normal at hit point (type cplane_t)

.surface

surface hit (type csurface_t)

.contents

contents od point hit

.ent

Entitiy hit

The basic trace is the trace to see if there is anything in front of you within a certain range. Its fairly easy to do, the code below does it. You'll need to define the variable under the code at the top of the function you are doing the trace within

AngleVectors(ent->s.angles,forward,NULL,NULL)
VectorMA (ent->s.origin, 8192, forward, end);
tr = gi.trace (ent->s.origin, NULL, NULL, end, ent,MASK_SHOT);

vec3_t forward,end;
trace_t tr;

Well thats the basics of traces, angles and functions. if you want to know more as me on #qdevels or ask a specific question on the qdevels message board


Function definitions

 

anglemod (angle)

returns the equivalent of angle between 0 and 360 degrees

AngleVectors (angles, forward_vector, right_vector, up_vector)

Converts the angles into various vectors :
forward_vector : vector along angles
right_vector : vector 90 degrees to the right of forward_vector
up_vector : vector 90 up from forward_vector

CrossProduct (vector1, vector2, vector_out)

sets vector_out to the cross product of vector1 and vector2

DotProduct (vector1, vector2), _DotProduct (vector1, vector2)

gives the dot product of vector1 and vector2

vectoangles (vector, angles)

Sets the angles array to point in the same direction as the vector

VectorAdd (vector1,vector2,vector_out), _VectorAdd (vector1,vector2,vector_out)

adds vector1 to vector2 and puts the result in vector_out

VectorClear (vector)

sets the vector to <0,0,0>

VectorCompare (vector1, vector2)

returns 1 if the vectors are the same and 0 otherwise

VectorCopy (vector_in,vector_out), _VectorCopy (vector_in,vector_out)

copies vector_in to vector_out

VectorInverse (vector)

sets vector to -1 * vector

VectorLength (vector)

returns the length of the vector

VectorMA (vector1, scale, vector2, vector_out)

sets vector_out to : vector1 + scale * vector2

VectorNegate (vector_in, vector_out)

sets vector_out to -1 * vector_in

VectorNormalize (vector)

Returns a vector in the same direction as the given vector, but only one unit long

VectorNormalize2 (vector_in, vector_out)

sets vector_out to the normalised version of vector_in. returns the length of vector_in

VectorScale (vector_in, scale, vector_out)

sets vector_out to scale * vector_in

VectorSet (vector, x, y, z)

sets the x coordinate of vector to x, the y coordinate to y, etc.

VectorSubtract (vector1,vector2,vector_out), _VectorSubtract (vector1,vector2,vector_out)

subtracts vector2 from vector1 and puts the result in vector_out

vectoyaw (vector)

Returns the yaw component of the angles vector

vtos (vector)

converts a vector into a string
Returns the string


Skunkworks Tutorials