Vecmath

From EventScripts Community Encyclopedia

Contents

Overview

Syntax: vecmath <result> <vector1> <operator> <vector2>

Note: Command parameters are described inside the < and > characters. Optional parameters are contained within [ and ] characters.

Description

Performs mathematical operations with vectors.


Note: It doesn't expand server variables. Prefix the command with es to expand variables (es vecmath).

Parameters

  • result - variable(s) to which the resulting vector will be saved
  • vector1 - the vector to be operated
  • operator - the mathematical operation to be done
  • vector2 - the second argument to operator (not applicable to all ops)


Operators

Operations for two vectors

These operations require the full syntax with no parameters omitted.

+
Sums up the two vectors (x1+x2, y1+y2, z1+z2)
-
Subtracts vector2 from vector1 (x1-x2, y1-y2, z1-z2)
To negate a vector, supply vector1 as "0,0,0" and vector2 as the vector you want to negate
.
Calculate the inner product of the vectors (x1*x2 + y1*y2 + z1*z2)
The result is a scalar value instead of a vector!
X
Calculate the cross product of the vectors (y1*z2-z1*y2, z1*x2-x1*z2, x1*y2-y1*x2)
angle
Calcultaes the angle in radians between the vectors
The result is a scalar value instead of a vector!
angles
Calculates the angles in radians of the vectors when projected onto the axis planes
The result is a vector whose components represent the angle between the vectors when projected onto the plane which is perpendicular to the axis named as the replaced component. In other words, the first resulting component is the angle on yz-plane, which is perpendicular to the x-axis (x is the first component)

Operations for one vector

These operations only need one vector. Some of the operations also need a scalar value to be used with them. The scalar can be given in place of vector2 (the x component of vector2, if using separate-form syntax)

length
Calculates the length of the vector (the result is a scalar)
setlength
Calculates a new vector that is paraller to the vector1 but has the length specified as parameter vector2 (the result is a vector)
normalize
Calculates a new vector that is paraller to the vector1 but has length of 1 (the result is a vector)
*
Multiplies the vector with a scalar (the result is a vector)
/
Divides the vector with a scalar (the result is a vector)

Vector notation

This command supports three types of vector notations. The preferred way is to use vector strings as the game engine uses them. A vector string looks like "23.672363,-4.000000,0.125342"; the components x, y and z are stored as floats and delimited using commas. To create a vector string from separate values, see es_createvectorstring.

When using the vector string notation with vecmath, the syntax is as above and the resulting vector is stored in one variable in vector string format (with exception of operations that return a scalar, see Operators)

Sometimes you might have the vector components stored in separate variables. You can use es_createvectorstring to combine them into a vector string, but vecmath also supports directly passing the components as separate parameters. The syntax then is:

Syntax:vecmath <res x> <res y> <res z> <x1> <y1> <z1> <operator> <x2> <y2> <z2>

So when you pass the vector components separately, you will also get the result in three different variables. Some operations only return a scalar value, they will save the result in <res x> variable and do not use the two other result parameters at all. You must still include them to comform to the syntax.

Take a note that if the operation does not need all of the second vector parameters, they can be omitted, but you can never omit the parameters that are before operator.

This component syntax also supports plane vectors (2D). The syntax is similar to above case:

Syntax:vecmath <res x> <res y> <x1> <y1> <operator> <x2> <y2>

The same rules apply to this syntax as above.

Examples

// Count the distance between two players
// variables userid1 and userid2 contain valid, different userid
es_xset location1 0
es_xset location2 0
es_xset vectorbw 0
es_xset distance 0
 
es_getplayerprop location1 server_var(userid1) "CBaseEntity.m_vecOrigin"
es_getplayerprop location2 server_var(userid2) "CBaseEntity.m_vecOrigin"
 
// Create a vector between the player locations
es vecmath vectorbw server_var(location1) - server_var(location2)
// Count the length of the resulting vector
es vecmath distance server_var(vectorbw) length
 
es_msg The distance between the two players is server_var(distance) game units.

Notes

  • Included in EventScripts 1.5

See also

blog comments powered by Disqus