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

Re: [pygame] 2D vector class



On Fri, Aug 11, 2006 at 01:38:06PM -0500, Nelson, Scott wrote:
> I've got 2 simple games roughed out (about 600 lines each) with about
> 2000 lines worth of shared modules I've written.  I have a somewhat full
> featured 2D vector class (supports adding, subtracting, scaling,
> rotation, normalizing, angle between, etc. that I use in both heavily to
> represent points, velocities, etc.  After using it for awhile, I've
> wondered how others have approached creating a 2D vector class.  Here's
> my "philosophical" questions, as the class I have works well, but I want
> to know how to make it better.  My main goals are to keep it simple, let
> it interface cleanly with Pygame's use of tuples of 2 ints for 2D
> points, not be specifically tied to Pygame, and have it not be a
> performance hog.
> 
> So, here's the issues that I'd love to hear feedback on:
> 
> #1 - Do you store the vector components internally as self.x and self.y
> or as a tuple?  Or subclass the vector class from a tuple?  If you use
> self.x and self.y, what is an efficient way to pass this as a tuple to
> pygame functions?

I chose to subclass tuple in pyspacewar, but I forgot why.

> #2 - Do you store the values as ints (making it easier to pass to
> Pygame) or floats (more accurate vector math, but need to cast to int
> before passing to pygame functions that require a tuple of ints?).  Or
> do you not even care and just let duck-typing do it stuff?

I needed floats, because I wanted the world to be scaled arbitrarily for
display, and besides that I needed the accuracy for gravity
calculations.

You can find my Vector class here:
http://mg.pov.lt/pyspacewar/trac/browser/trunk/src/pyspacewar/world.py

It has unit tests, and is somewhat optimized (only those bits that
showed up when profiling).

Marius Gedminas
-- 
C is for Cookies.  Perl is even better for Cookies.

Attachment: signature.asc
Description: Digital signature