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

[pygame] Collision Resolver module



Hi all.

Here's a "collision resolver" module I've been working on.

A common problem in 2D games is trying to move a sprite in a direction
(deltaX, deltaY) and then discovering that there is a solid object in the
new position it wants to occupy, so you have to move it back to the edge of
that solid object.

Further complicating matters, just checking the end position is sometimes
not enough.  When a rectangle moves it sweeps through a hexagonal shape, and
there might be solid objects that collide with this "sweep hexagon" that
don't collide with the end position.  This issue is more pronounced the
bigger (deltaX, deltaY) gets.

An even further complication: if you displace the rect due to a collision
inside the sweep hexagon, it may then collide with a new solid object that
lies outside the original sweep hexagon.

This module provides a function that you can call, resolve_collisions() that
will sort all this mess out for you.

I think this is a general enough problem in 2D games that the resolver
function be considered for inclusion in pygame itself.

I have included a program that demonstrates what it does.  Just run:

python collision_resolver_example.py

options are printed to the terminal.

-sjbrown

Attachment: collision_resolver.tar.gz
Description: GNU Zip compressed data