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

[pygame] Strategies for pixel-based collision



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I've just thrown together a very simple start to a game that involves a car 
driving around a map. It's fun :)

The car is a single image which I rotate using transform.rotate, and the map 
is a tiled area based on the code I posted to my weblog a while back (I have 
smooth scrolling working now):

http://www.mechanicalcat.net/richard/log/Python/PyGame_sample__drawing_a_map__and_moving_around_it

Anyway, I'd like to crash into stuff. Simple sprite collision stuff won't 
work, as the rotation of the car means that the sprite rect can be quite 
unrepresentative of the actual sprite.

Performing a pixel-by-pixel check would be expensive (though I've not tested 
it) -- eventually I want many cars on the map.

What approach would people take to detecting collision in this sort of setup?

I've thought about defining the bounding box of the car as four vertexes, 
transforming them when I rotate the car and then manually drawing a line 
between them (using Bresenham - has anyone written that in Python? :) and 
checking only those pixels.

Just wondering whether there's any other ideas out there?


    Richard
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBYHI2rGisBEHG6TARApA7AJ4necIdPwpZKGqjKfTGSuTOdA2bGQCfaMNc
OQuQU0TnXDKJiIwjsXk2u2w=
=uB1S
-----END PGP SIGNATURE-----