Per Pixel Collision


motorollin

Member
Joined
Jul 31, 2007
Messages
163
I've just been reading the Lazy Foo Per Pixel Collision tutorial. There is a section in the sprite constructor headed "Initialize the collision boxes' width and height". I'm wondering how you would do this if your sprite has multiple frames of animation? The problem would be that when the frame changes the collision boxes would no longer match. What is the best way to handle this?

TIA
 
TBH I thought that would be the case. Isn't setting the collision areas of every object each frame very resource hungry? Also, is there a function or library which can automatically detect and set collision areas?
 
You would have to do some little tests to see which collision methods would be most benificial...

In CromoZome I simply do Euclydian distance tests as that works out best when combined with a Quadtree partitioning system.

You could do bounding box collisions, or rectangular, as per the Lazyfoo tutorials or you could use collision maps (a Black and white image of the sprites to use in testing...). There are many ways of testing collisions so it really depends on what you are doing in the game as to which you would want to use...

It might be an idea to just have a general collision box that applies to all frames, then put in a better collision algorithm once you have the game running. I'm not sure, but you have to think about it :)
 
You might want to break it into two arrays, the entities and the sprites so they don't go in lock step like the June Taylor dancers. Just give each one a separate hot zone.

CODE

// cows are entities connected to an animation sequence by index into the sprite array
typedef struct
{
short x; // virtual cow pasture coordinates
short y;
short z; // layer
short sprite; // what kind of cow am i
short action; // index into array of function pointers defining behavior
short rection; // according to numpad compass - 5 stationary
short curframe; // the current frame - what this sprite is doing now
short dist; // tracks when this = sprite->odom curframe++
short speed; // distance to mooove on it's turn
short state; // ok/sucked/sucking/roadkill/cruising
} COW;

// sprite is an animation sequence defined by an array of frame coordinates
// and parameters on how to display them used by truck, cow etc.
typedef struct
{
short odom; // times shown before incrementing curframe
short opaque; // transparency
short framenum; // number of frames
short noise; // index to racket object array of sounds
RECT hot; // coordinates in bitmap used in collision detection
RECT *frame; // framenum sized array of frame rectangles of surface -left top right bottom
} SPRITE;
 
Back
Top