[racket] Doing collision detection in universe.ss

From: John Clements (clements at brinckerhoff.org)
Date: Wed Nov 28 02:04:20 EST 2012

On Nov 27, 2012, at 5:56 PM, Yaron Minsky wrote:

> I've been weaning my son off of Scratch in favor of Racket, and trying
> to get him to write interactive games using universe.ss and image.ss.
> I'm wondering if anyone has suggestions for how to do things like
> collision detection.  image.ss has these nice first-class images, but
> I don't see a good way of querying two images to see if they overlap.
> Has anyone else had luck in doing this?  universe has a nice
> programming model, but I've found it challenging to find simple ways
> of doing the kinds of things that Scratch makes easy.

Sounds like you want a raster model for images.  2htdp/image *almost kinda sorta* provides it, in that it exposes a "freeze" primitive that renders an image as a bitmap.  For images that are frozen, it should be possible to determine collision in a straightforward way. In fact, I suppose you could just use memoization to keep around a frozen version of any image.

It seems to me like the simplest interface would be one that accepted the two images and their relative positions, and returned #t if they overlap.

I think that the hardest part will be writing the collision detector, and I think even that part won't be too horrible; first do a fast check to see if their bounding boxes overlap. If they do, then blit the alpha channels of the overlapping region into a small fresh buffer, and ... um... yeah, that part gets a bit interesting. It's probably faster to pack the bitmask into 64-bit ints, and then just check the matching ints for non-zero-ness. Lots of fiddly details.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4370 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20121127/3a9b29e7/attachment.p7s>

Posted on the users mailing list.