0
votes

I have 3 box2d bodies. All of them have user data. Their userData's tags are shown below.

BODY 1: Tag = 1
BODY 2: Tag = 1
BODY 3: Tag = 2

Further in my code, I have implemented contact listener to detect contacts between bodies and I have put condition that Body 3 will be destroyed either collision between BODY1 and BODY3 or BODY2 and BODY3

But when BODY1 and BODY2 collide with BODY3 at the same time, I am getting EXC_BAD_ACCESS. I know why this error appears : it is because there is no body to remove, as it is removed at first contact.

Anyone know how I can solve this error?

1

1 Answers

1
votes

You could put a condition to check if your colliding body is == NULL.

If it's not, destroy it. If it is, it's that it has already been destroyed.

EDIT :

To keep your specific tag system, you could pass an NSDictionary as user datas of each of your body :

bodyDef.userData = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithInt:theBodyTag], @"tag",
                             [NSNumber numberWithInt:theUniqueID], @"ID", 
                             nil];

Then, when colliding you could check the bodies user datas and know if you're in presence of your body 3 or not.

if ([(id)body1->GetUserData() objectForKey:@"ID"] == 3) {
        if ([(id)body2->GetUserData() objectForKey:@"ID"] == 1 || 
            [(id)body2->GetUserData() objectForKey:@"ID"] == 2) {
                Feed an array with the body to destroy and destroy it after your collision checks !
                [myQueue addObject:[(id)body2->GetUserData() objectForKey:@"ID"]];
        }
}

After the collision routine, iterate over all your bodies and destroy the ones that have your queue objects as unique ID.

Please note that I didn't check of this code works actually, but this is the idea.