Feb 18, 2009

Stupid game programming in Python

So i decided to write a short game like thing in python using pygame (fun library). It became known as pizza-shamrock even though i was initially going for some type of sumo wrestling like game. I guess it all started as me wanting to use pythons native support for complex numbers for awesome. I had just come out of a complex analysis course and it seemed obvious that the position of the sprites in any game should be represented by complex numbers.

I know it sounds insane. Its clear its not a great idea to represent their position with two integers, but you could at least just use a vector right? That would be better right? maybe...

Historically, complex numbers and their 4d friends, quaternions, came about long before vector notation became the standard for describing physics. This isn't a mistake, complex numbers and quaternions form a beautiful algebra, unlike vector math. Complex numbers have multiplication and addition as operations. Vectors have addition and a two different kinds of multiplications and man its not as cool, either trust me or take complex analysis.

In short by using complex numbers im able to do cool tricks that would be much harder to do with vectors. Like, i can transform the game to polar coordinates by complex exponentiation. With a vector id have to use trig. I don't really understand how Python does complex exponentiation natively, esp since you have import a math library to do trig, but whatever.

If you don't believe me that the weirdness of complex numbers is worth it, read the quaternion article. They're being used more and more in computer graphics to succinctly describe transforms that would normally be ugly as hell with vectors.

I guess you can argue that for maintainability reasons (cuz very few people are comfortable with complex numbers) my code should've used vectors. So im not really arguing that complex is the way to go, im just arguing that its awesome.