Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Message from discussion Strategies for collision detection.
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ernesto Corvi  
View profile  
 More options Dec 3 2008, 6:03 pm
From: Ernesto Corvi <erne...@on-core.com>
Date: Wed, 3 Dec 2008 15:03:42 -0800 (PST)
Local: Wed, Dec 3 2008 6:03 pm
Subject: Re: Strategies for collision detection.
Sorry I didn't answer earlier guys, been pretty busy at work.
This technique I'm going to describe works for background collision
detection. Inter-sprite collision detection should be done by checking
the sprite boundaries themselves.
I'm also not going to post code, but give you a general idea and
function names so you guys can look them up and craft something
together yourselves.

Say we want to create an arbitrary collision map. What we want, is to
have a 'collision bitmap' that we can look up quickly to know if we
collided against anything or not.
The first part is to create the collision mask image. You can just
paint a PNG in photoshop, and we'll say that anything with alpha 0
means no collision and anything with alpha != 0 collides.
So you go to photoshop and create a PNG the size of your level, and
paint the areas that should collide, and leave transparent the areas
you don't want collisions in. We'll call this
image levelmask.png

When you're loading your level, you also create your collision array,
like this:

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];

}

And that's it. The actual detection part is what would be time
consuming and it's just a simple lookup at this point.
If you have questions, feel free to ask.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.