xpenguins cont'd - repainting a window after penguins walk on it, and when penguins overlap.

Corbin Simpson mostawesomedude at gmail.com
Mon Jun 6 10:47:57 PDT 2011


On Mon, Jun 6, 2011 at 2:10 AM, Alan Cox <alan at lxorguk.ukuu.org.uk> wrote:
>> However I'm not a computer scientist and the only way that makes sense for
>> me to implement this is lots of inefficient loops to check every penguin
>> against every other penguin.
>
> That's a beginners game writing question not surprisingly. Games
> often solve this in simple form by keeping all their sprites (penguins in
> your case) in a sorted list (eg top to bottom, left to right). You still
> have to do a bit of scanning as obviously the top of one penguin can land
> on the feet of another, but you can do a lot less scanning that way.

Indeed, this is a classic question in games: How does one efficiently
and quickly search for spatial collisions?

Alan's answer is great for your case. If you're feeling adventurous,
you could create a spatial dictionary -- an associative array which
stores all of the penguins by coordinates, using buckets which
correspond to spatial areas. This massively reduces collision checking
time for one-against-many, especially when you have lots of sprites to
check. It might be overkill here, unless you're planning on making
thousands of penguins. :3

~ C.

-- 
When the facts change, I change my mind. What do you do, sir? ~ Keynes

Corbin Simpson
<MostAwesomeDude at gmail.com>



More information about the xorg mailing list