Difference between revisions of "Vectors Explained (Scripting)"

From AlliedModders Wiki
Jump to: navigation, search
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:
  
 
<pawn>
 
<pawn>
native TeleportEntity(entity, const Float:origin[3], const Float:angles[3], const Float:velocity[3]);
+
native int TeleportEntity(int entity, const float origin[3], const float angles[3], const float velocity[3]);
 
</pawn>
 
</pawn>
  
 +
=== Positions ===
 
'''Position Vector''': The first Vector in this native, declared as origin[3], is the Position Vector. This simply tells us the entities absolute position in the world. The position vector could look something like {15.0, 30.0, 15.0} and this would mean the entity is 15 units along the X axis, 30 units along the Y axis, and 15 units along the Z axis.
 
'''Position Vector''': The first Vector in this native, declared as origin[3], is the Position Vector. This simply tells us the entities absolute position in the world. The position vector could look something like {15.0, 30.0, 15.0} and this would mean the entity is 15 units along the X axis, 30 units along the Y axis, and 15 units along the Z axis.
  
'''Angles Vector''': The second Vector in this native, declared as angles[3], is the Angle Vector. This specifies the rotation of an object. This vector would follow the format {pitch, yaw, roll} which modifies the angles at which the object is looking. 'Pitch' is the up & down angles, 'Yaw' is the left & right, and 'Roll' is the spin along the up/down/left/right vector.
+
=== Angles ===
 +
'''Angles''' specify the ''angles'' of the rotation, not the actual vector of the rotation! (Essentially, it's an arctangent of each direction component.)
 +
Angles are specified as <code>{pitch/up, yaw/left, roll/spin}}</code>. Angles are specified as a `float[3]` array.  
 +
Angles can be turned into a vector (and back into an angle!) using the angle natives: <code>GetAngleVectors</code> and <code>GetVectorAngles</code>.
  
'''Velocity Vector''': The last Vector in the native is the Velocity Vector, declared as velocity[3] in the example. This vector tells the game where the object will be on the next frame. So on the next game frame, the Position Vector of this object will be the sum of the Position Vector and the Velocity Vector from our current frame.
+
Most angles are sanitized between -180 and 180 within the source engine (for up/down on players this is -89/89!), however some weird physics shenanigans and cheating clients can exceed these values so always ensure your angles are well formed before using them.
 +
 
 +
{{CommonMistake|Make sure to use <code>GetAngleVectors</code> before using the angles value to transform world space!}}
 +
 
 +
=== Velocity ===
 +
'''Velocity''', or a '''Vector''', is a true vector in the sense that it is ''only a direction and magnitude''. The velocity represents motion, usually the amount of motion that will occur in one tick. The position of an object on the next tick will be the sum of the current position and the object's velocity.
  
 
==Velocity Vectors==
 
==Velocity Vectors==
 
<pawn>
 
<pawn>
decl vecOrigin[32], vecVelocity[3], vecResult[3];
+
float vecOrigin[3], vecVelocity[3], vecResult[3];
  
 
AddVectors(vecOrigin, vecVelocity, vecResult);
 
AddVectors(vecOrigin, vecVelocity, vecResult);
 
</pawn>
 
</pawn>

Latest revision as of 14:41, 19 May 2024

Introduction

Vectors in general tend to be rather confusing until they are explained. There are three different types you will see. Position Vectors, Velocity Vectors, and Angle Vectors. While in most basic cases you will probably only use one of these at a time, it doesn't hurt to know what the other two parameters are there for. Here is an example of the native 'TeleportEntity' to show you what it looks like:

native int TeleportEntity(int entity, const float origin[3], const float angles[3], const float velocity[3]);

Positions

Position Vector: The first Vector in this native, declared as origin[3], is the Position Vector. This simply tells us the entities absolute position in the world. The position vector could look something like {15.0, 30.0, 15.0} and this would mean the entity is 15 units along the X axis, 30 units along the Y axis, and 15 units along the Z axis.

Angles

Angles specify the angles of the rotation, not the actual vector of the rotation! (Essentially, it's an arctangent of each direction component.) Angles are specified as {pitch/up, yaw/left, roll/spin}}. Angles are specified as a `float[3]` array. Angles can be turned into a vector (and back into an angle!) using the angle natives: GetAngleVectors and GetVectorAngles.

Most angles are sanitized between -180 and 180 within the source engine (for up/down on players this is -89/89!), however some weird physics shenanigans and cheating clients can exceed these values so always ensure your angles are well formed before using them.

Make sure to use GetAngleVectors before using the angles value to transform world space!

Velocity

Velocity, or a Vector, is a true vector in the sense that it is only a direction and magnitude. The velocity represents motion, usually the amount of motion that will occur in one tick. The position of an object on the next tick will be the sum of the current position and the object's velocity.

Velocity Vectors

float vecOrigin[3], vecVelocity[3], vecResult[3];
 
AddVectors(vecOrigin, vecVelocity, vecResult);