Revision: 47317
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at June 5, 2011 05:33 by shalomfriss
Initial Code
//1) Load the image: UIImage *collisionImage = [UIImage imageNamed:@"levelmask.png"]; //2) Get the image size: int width = (int)collisionImage.size.width; int height = (int)collisionImage.size.height; //3) Allocate our array for quick lookups and clear it: unsigned char *collisionMap = (unsigned char *)malloc( width * height ); memset( collisionMap, 0, width * height ); //4) Get access to the raw bits of the image CFDataRef imageData = CGDataProviderCopyData( CGImageGetDataProvider ( collisionImage.CGImage ) ); const UInt32 *pixels = (const UInt32*)CFDataGetBytePtr( imageData ); //5) Build our collision map: for( j = 0; j < (width * height); j++ ) if ( pixels[j] & 0xff000000 ) collisionMap[j] |= 1; //6) Release what we no longer need: CFRelease( imageData ); //Now, whenever you need to see if you hit something, you call a routine //that does: -(int)collisionForPoint:(CGPoint)point { int x = (int)point.x; int y = (int)point.y; // TODO: check for boundaries here return collisionMap[(y*width)+x]; }
Initial URL
Initial Description
Initial Title
Pixel perfect collision detection
Initial Tags
iphone
Initial Language
iPhone