[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] 2D vector class



Ethan Glasser-Camp wrote:
It certainly isn't clear how best to take advantage of this
speedup without going crazy from pos_x and pos_y variables all over
the place.

Doing vector operations one at a time in Python is always going to be either slow or very slow, no matter how you go about it.

IMO, the best way to make this sort of thing fast
is to arrange things so that you can use Numeric to
operate on multiple vectors at once. For instance,
store all your object positions in one array and
all the velocities in another, and then just add
the two arrays together.

You could wrap these arrays up in custom PointArray
and VectorArray classes with suitable operators, and
the overhead of method calls would be far less of a
problem, since you only incur it once for the whole
array rather than once per vector.

I used something like this very effectively in a
recent project where I wanted to find the intersection
of a ray with a terrain mesh. First I worked out how
to use Numeric to test the ray against just one
triangle. Then it was almost (although not quite)
trivial to extend that to handle an array of triangles.

Presto - I could then do a hit test against my whole
terrain of hundreds of triangles almost instantaneously!

--
Greg