

(15 intermediate revisions by 2 users not shown) 
Line 1: 
Line 1: 
−  {{tocright}}
 +  #redirect [[Vector]] 
−   
−  '''Vector''' is a C++ class that represents a line with direction and length, starting at the current [[origin]].
 
−   
−  Each vector contains three <code>[[vec_t]]</code> 'ordinates'. <code>(0,20,5)</code> is a Vector that ends 20 [[unit]]s in front and 5 units above the current origin.
 
−   
−  {{noteSource's vector class is ''very'' different to [[Wikipedia:Vector (C++)the Standard Template Library's]], which is a type of [[Wikipedia:Arrayarray]]. The STLstyle vector has been renamed <code>[[CUtlVector]]</code> in Source.}}
 
−   
−  == Declaration ==
 
−   
−  Vector vecMyVector = Vector(0,20,5);
 
−   
−  You could also assign to the X, Y and Z member variables separately.
 
−   
−  == Uses ==
 
−   
−  === Positioning ===
 
−   
−  Every entity's position is stored as a vector relative to its [[parent]]. You are likely to be familiar with this idea already as [[coordinatesgrid coordinates]].
 
−   
−  The parentrelative vector stored by the entity is 'local'; calculating an 'absolute' vector relative to the [[world]] requires extra work. The two Abs functions below do that work for you, but are more [[expensive]].
 
−   
−  * <code>[[GetAbsOrigin()]]</code>, <code>[[SetAbsOrigin()]]</code>
 
−  * <code>[[GetLocalOrigin()]]</code>, <code>[[SetLocalOrigin()]]</code>
 
−   
−  === Movement ===
 
−   
−  An entity attempts to move the length of its velocity vector once per second. The code looks something like this:
 
−   
−  Vector velocity = Vector(0,5,0); ''// 5 units/second forward''
 
− 
 
−  Vector vecNewOrigin = GetAbsOrigin() + velocity * [[gpGlobals]]>frametime; ''// frametime is in seconds, e.g. 0.033''
 
− 
 
−  SetAbsOrigin(vecNewOrigin);
 
−   
−  Notice how <code>frametime</code> is used to regulate the entity's speed regardless of how long each frame takes to calculate. See [[#Scalarscalar multiplication]] for more detail on the operation.
 
−   
−  As with the origin, velocity is stored relative to the entity. {{todoWhat does AbsVelocity mean?}}
 
−   
−  * <code>[[GetAbsVelocity()]]</code>, <code>[[SetAbsVelocity()]]</code>
 
−  * <code>[[GetLocalVelocity()]]</code>, <code>[[SetLocalVelocity()]]</code>
 
−   
−  === Tracelines ===
 
−   
−  ''Main article: [[TraceLines]]''
 
−   
−  Tracing is the process of going from a point A towards another point B, and finding out the first thing that we "hit" on our way from A to B ('''TraceLine''').
 
−   
−  == Operations ==
 
−   
−  All vectors in an operation must have the same [[origin]] for the result to make sense. Whether a local or absolute origin is used depends on what you're trying to achieve.
 
−   
−  === Addition ===
 
−   
−  Adding two (or more) vectors '''combines''' them. You have already experienced vector addition if you've ever pushed an object with two hands!
 
−   
−  [[Image:Vector_add.pngcenterVector addition: (4,1) + (3,1) = (1,2)]]
 
−   
−  === Subtraction ===
 
−   
−  Subtracting one vector from another produces the '''difference''' between the two  in other words, how to get to the first location from the second. The result is local to the ''second'' vector.
 
−   
−  [[Image:Vector_subtraction.pngcenterVector subtraction: (2,3)  (2,1) = (4,2)]]
 
−   
−  {{tipThe order in which you subtract defines the direction of the vector.}}
 
−   
−  === Multiplication ===
 
−   
−  ==== Scalar ====
 
−   
−  Multiplying or dividing a vector by a [[Wikipedia:Scalarscalar]] (i.e. an [[int]] or [[float]]) will change its '''length''' (sometimes called "magnitude") without affecting its direction.
 
−   
−  [[Image:Vectorscalar_multiply.pngcenterVectorscalar multiplication: (1,2) x 2 = (2,4)]]
 
−   
−  {{tipDividing a vector by its length [[normal]]ises it. Use <code>VectorNormalize()</code> to do this quickly.}}
 
−   
−  ==== Dot product ====
 
−   
−  Multiplying two vectors then adding the result's ordinates produces a '''dot product''', which when both vectors have been normalised (see above) is equal to the '''cosine''' of the angle between the two vectors. One use of a dot product is to tell how closely the direction of two vectors match. +1 means a match, 0 means they are perpendicular to each other, and 1 means they are opposed.
 
−   
−  {{note''True dot products are only produced when the length of both vectors is 1. '' The [[normal]]isation step has been skipped in the following demonstration to make its equations simpler (but the positive/zero/negative rule still applies).}}
 
−   
−  [[Image:Vector_dotproduct.pngcenterVector dot products: (2,2) x (2,0) = (4,0) = 4; (2,2) x (2,2) = (4,4) = 0; (2,2) x (2,2) = (4,4) = 8]]
 
−   
−  This code calculates a dot product with the aid of Source's various helper functions:
 
−   
−  Vector vecTarget = GetAbsOrigin()  pTarget>GetAbsOrigin(); ''// Get local vector to target''
 
−  '''VectorNormalize'''(vecTarget); ''// Normalisation needs to be done beforehand''
 
− 
 
−  Vector vecFacing;
 
−  '''[[AngleVectors()AngleVectors]]'''(GetLocalAngles(),&vecFacing); ''// Convert facing angle to equivalent vector (arrives normalised)''
 
− 
 
−  float result = '''DotProduct'''(vecTarget,vecFacing); ''// Get the dot product.''
 
− 
 
−  if (result > 0)
 
−  Msg("pTarget is in front of me!\n");
 
−   
−  {{tipThere is no need to normalise if you only care about whether one location is in front of another.}}
 
−   
−  ==== Cross product ====
 
−   
−  A '''cross product''' is a vector perpendicular to two input vectors. The cross product of a vector pointing down the Xaxis and a vector pointing down the Yaxis is a vector pointing down the Zaxis. It's used to extrapolate a third dimension from just two.
 
−   
−  The equation is fiddly and doesn't have to be learnt; just use <code>CrossProduct()</code>. There generally isn't any need to normalise the input vectors.
 
−   
−  == Member functions ==
 
−   
−  ; <code>[[float]]/[[vec_t]] Length()</code>
 
−  ; <code>vec_t VectorLength(const Vector&)</code>
 
−  : Returns the vector's length, in units.
 
−  ; <code>vec_t LengthSqr()</code>
 
−  : Returns length squared.
 
−  ; <code>vec_t Length2D()</code>
 
−  : Returns the length, disregarding the upelement.
 
−  ; <code>vec_t Length2DSqr()</code>
 
−  : Returns the length squared, disregarding the upelement.
 
−  ; <code>void Negate()</code>
 
−  : Reverses the vector's direction without affecting its length.
 
−  ; <code>vec_t DotProduct(const Vector& a, const Vector& b)</code>
 
−  : {{todoSometing to do with determining how closely two vectors match.}}
 
−  ; <code>void CrossProduct(const Vector& a, const Vector& b, Vector& result )</code>
 
−  : {{todoSomething to do with creating a surface normal.}}
 
−  ; <code>bool WithinAABox( Vector const &boxmin, Vector const &boxmax)</code>
 
−  : Does the Vector intersect this axisaligned box? {{todoAre argument vectors local or abs?}}
 
−   
−  == Helper functions ==
 
−   
−  * <code>[[UTIL_VecToYaw()]]</code>, <code>[[UTIL_VecToPitch()]]</code>
 
−  * <code>[[AngleVectors()]]</code>
 
−  * <code>[[VectorAngles()]]</code>
 
−  * <code>[[VectorNormalize()]]</code>
 
−   
−  == See also ==
 
−   
−  * [[Wikipedia:Euclidean vector]]
 
−  * <code>[[QAngle]]</code>
 
−  * <code>[[vec_t]]</code>
 
−  * <code>[[VectorVectors]]</code>
 
−  * <code>[[CUtlVector]]</code>
 
−   
−  [[Category:Glossary]]
 
−  [[Category:Variables]]
 
−  [[Category:Helpers]]
 