Vecmath.vector

From EventScripts Community Encyclopedia



Contents

Class: vecmath.vector

Note: Requires EventScripts 2.0
Module: vecmath
EventScripts version: 2.0

Overview

Description

A class representing a vector object that has methods for doing simple and complex vector mathematical operations as well as importing and exporting the data in multiple formations such as vectorstrings or lists.

Quick Example

An example script showing "attract" effect.

import es
import vecmath
 
push_modifier = es.ServerVar('push_modifier', 0.5)
 
def load():
    es.doblock('corelib/noisy_on')
 
def unload():
    es.doblock('corelib/noisy_off')
 
def bullet_impact(event_var):
    userid = int(event_var['userid'])
    
    # vectors can be constructed from tuples and lists:
    bullet_location = vecmath.vector((
        event_var['x'],
        event_var['y'],
        event_var['z']
        ))
    # vectors can also be constructed from vectorstrings
    player_location = vecmath.vector(
        es.getplayerlocation(userid)
        )
    
    # you can do simple calculations with vectors directly:
    push_vector = (bullet_location - player_location) * float(push_modifier)
    
    # vectors can be quickly returned as vectorstrings also:
    es.setplayerprop(userid, 'CBasePlayer.localdata.m_vecBaseVelocity',
        str(push_vector)
        )

Methods

__init__ (vl)
Class instance creation method. Takes any subscriptable object (list, tuple, vector) or vectorstring (string with three float values separated with commas)
Example:
myvector = vecmath.vector( [-2, 0, 1.5] )
__list__ ()
The same as calling list(vector), returns a list type representing the vector.
Example:
mylist = list(myvector)
getdict ()
Creates a dict object representing the vector with keys 'x', 'y' and 'z'.
Example:
mydict = myvector.getdict()
__str__ ()
The same as calling str(vector), returns a vectorstring of the vector.
Example:
es.setplayerprop( userid, 'CBaseEntity.m_vecOrigin', str(myvector) )
getstr (separator=',')
Returns a vectorstring separated with specified character.
Example:
es.server.queuecmd( 'es_xsetpos %d %s'%(userid, myvector.getstr(' ')) )
__len__ ()
The same as calling len(vector), returns the number of components; should be equal to 3 always.
Example:
es.dbgmsg(0, '%d vector components'% len(myvector))
__add__ (vector2)
Sums two vectors.
Example:
sumvector = vector1 + vector2
__sub__ (vector2)
Subtracts two vectors.
Example:
diffvector = vector1 - vector2
__mul__ (scalar)
Multiplies vector length.
Example:
doublevector = myvector * 2
__div__ (scalar)
Divides vector length.
Example:
halfvector = myvector / 2
__getitem__ (index)
The same as vector[index]. The index can be integer 0, 1, 2 or string 'x', 'y' or 'z'.
Example:
xvalue = myvector['x']
yvalue = myvector[1]
__setitem__ (index, value)
The same as vector[index] = value. The index can be integer 0, 1, 2 or string 'x', 'y' or 'z'. The value must be a float.
Example:
myvector[0] = 0
myvector[1] = 0
myvector['z'] += 64
__neg__ ()
The same as -vector, negates all components.
Example:
oppositevector = -myvector
copy ()
Creates a new, identical vector
Example:
backupvector = myvector.copy()
# Note, the above is NOT the same as
backupvector = myvector
ip (vector2)
Inner (dot) product.
Example:
# These two are exactly the same functionality:
product = myvector.ip(vector2)
product = vecmath.ip(myvector, vector2)
cp (vector2)
Cross product.
Example:
# These two are exactly the same functionality:
pervector = myvector.cp(vector2)
pervector = vecmath.cp(myvector, vector2)
angle (vector2)
The angle between the vectors.
Example:
# These two are exactly the same functionality:
angle = myvector.angle(vector2)
angle = vecmath.angle(myvector, vector2)
angles (vector2)
The angles between the vectors (see vecmath for more info)
Example:
# These two are exactly the same functionality:
angles = myvector.angles(vector2)
angles = vecmath.angles(myvector, vector2)
length ()
Returns the length of the vector.
Example:
distance = (vector2 - vector1).length()
setlength (newlength)
Creates a new vector with same direction but set length.
Example:
shortvector = myvector.setlength(2)
normalize ()
Creates a new vector with same direction but length 1.
Example:
# These two are the same:
direction = myvector.setlength(1)
direction = myvector.normalize()

Attributes

None are directly usable. Get and set component values with vector[index] notation.

Notes

(None)

See Also

blog comments powered by Disqus