[racket] Doing collision detection in universe.ss
On Nov 27, 2012, at 8: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.
Right, and the question doesn't even make sense at the "image" level because images don't have locations.
One "purist" answer would be to write your own collision detection, based on whatever data type you're using for your model. If it has two fields "player-posn" and "monster-posn", you can do a distance formula calculation to find out how close those two points are to one another, and decide whether it's close enough to constitute a "collision". If it has a list of posns (or structs that contain posns), you can do that for each pair of elements in the list.
But that's a pain, however much one might learn from the process. If you just want to get a game with collision detection up and running, it would be nice if the image and/or universe libraries provided some support. An easy example would be rects-intersect?: call it on the bounding rectangles of your two translated images, and it tells you whether they intersect. (A student could write that easily enough, and use it henceforth.) Spiffier, in case you wanted to treat irregularly-shaped objects properly, would be a function images-intersect? that takes in two translated images and tells whether there is a pixel location for which both have nonzero alpha channels.
How do you represent a "translated image"? It could be an image and a posn, or it could be just an image formed by place-image onto an all-transparent background.
> 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.
And vice versa, of course: there are lots of things that are easy in universe, but very difficult in Scratch.
Stephen Bloch
sbloch at adelphi.edu