Vecmath

From EventScripts Community Encyclopedia


Module: vecmath

Module Overview

EventScripts version: 2.0 or greater

Table of Contents

Contents


Description

Vecmath provides a powerful way to handle and manipulate coordinates and vectors, being compatible with how the Source engine handles them.

Quick Example

Show kill beam:

import es
import vecmath
import effectlib
 
def player_death(event_var):
    # save the userids to local variables:
    victim = int(event_var['userid'])
    attacker = int(event_var['attacker'])
    
    if attacker:
        
        # get the player locations as vecmath.vectors for better manipulation
        victim_location = vecmath.vector(es.getplayerlocation(victim))
        attacker_location = vecmath.vector(es.getplayerlocation(attacker))
        
        # check attacker crouching state and adjust z coordinate accordingly
        attacker_flags = int(es.getplayerprop(attacker, 'CBasePlayer.m_fFlags'))
        if attacker_flags & 2:
            #attacker is crouching
            attacker_location['z'] += 44
        else:
            #attacker is standing
            attacker_location['z'] += 64
        
        # draw a beam using effectlib
        # note that you can do simple vector math inline, like adding values:
        effectlib.drawLine(attacker_location, victim_location + (0,0,96/2),
            seconds=5)

Module Content

Classes

  • vecmath.vector -- 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

Methods

angle(vector1, vector2)
Returns the angle between two vectors in radians
Example:
>>> print vecmath.angle((10,20,30), (-20, 50, 30))
0.742111027349
>>> print vecmath.angle((1,0,0), (0,1,0))/math.pi*180
90.0
angles(vector1, vector2)
Returns a list of angles for when the vectors are projected to planes perpendicular to the Cartesian coordinate axles (x, y and z).
The first item (index 0) is the angle when projected onto y-z plane, the second item (index 1) is the angle when projected onto x-z plane and the third item (index 2) is the angle when projected onto x-y plane.
Example:
>>> print vecmath.angles((1,0,0), (0,1,0))
[None, None, 1.5707963267948966]
cp(vector1, vector2)
Returns the vector that is the result of cross product between the two vectors given as parameters.
Example:
>>> print vecmath.cp((1,0,0), (0,1,0))
0.0,0.0,1.0
ip(vector1, vector2)
Returns the dot product of the two vectors.
Example:
>>> print vecmath.ip((5,3,2), (0,5,-5))
5
>>> print vecmath.ip((1,1,0), (-1,1,0))
0
length(vector)
Returns the length of the vector.
Example:
>>> print vecmath.length((3,0,4))
5.0
normalize(vector)
Returns a vector that points to the same direction as the vector parameter but has the length of 1.
Example:
>>> print vecmath.normalize(vecmath.vector("10,10,10"))
0.57735026919,0.57735026919,0.57735026919
setlength(vector, length)
Returns a vector that points to the same direction as the vector parameter but has the length specified.
Example:
>>> print vecmath.setlength(vecmath.vector([3,4,0]), 10)
6.0,8.0,0.0

Helper methods

These methods are not directly vector manipulation, but rather to help some common tasks related to coordinates and vectors.

distance(coord1, coord2)
Returns the distance between the two coordinates
Example:
def player_death(event_var):
    victim = int(event_var['userid'])
    attacker = int(event_var['attacker'])
    if attacker:
        victim_location = vecmath.vector(es.getplayerlocation(victim))
        attacker_location = vecmath.vector(es.getplayerlocation(attacker))
        distance = vecmath.distance(victim_location, attacker_location)
        es.tell(victim,'You were killed from %f m'%(distance*0.0254))
viewangles(coord1, coord2, roll=0.0)
Counts the pitch, yaw and roll values (roll copied from optional parameter) based on the two coordinates (entity at coord1, looking at coord2)
viewvector(viewangles)
Creates a normalized vector pointing to the direction the parameter viewangles points to, viewangles must be of form [pitch, yaw, roll]
isbetweenRect(what, coord1, coord2)
Checks if the specified coordinate what is between a 3D rectangle defined by two corner coordinates.
Returns True or False depending on the case.
Example:
>>> vecmath.isbetweenRect((10,10,10), (0,0,0), (20,20,20))
True
>>> vecmath.isbetweenRect((10,30,10), (20,0,20), (0,20,0))
False
isbetweenVect(what, coord1, coord2)
Checks if the specified coordinate what is on the line drawn from coord1 to coord2.
Returns True or False depending on the case.

See Also

blog comments powered by Disqus