Page 1 of 2
How to handle a collision that is happening to fast?
Posted: Thu May 10, 2012 7:22 am
by Ichigo
Hello there, my problem is:
I have bullets. That are being shot at 0.5 seconds. When they collide to the wall i want them to desappear. The problem is when i have more than one bullet on the screen. The handler doesn't treat the collision.
It's like it can't be called successfully.
My code is:
Code: Select all
- (BOOL) handleCollision:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space
{
if (moment == COLLISION_BEGIN || moment == COLLISION_PRESOLVE) {
CP_ARBITER_GET_SHAPES(arb, bullet, wall);
[self prepareToDie];
[self die];
}
return YES;
}
Re: How to handle a collision that is happening to fast?
Posted: Thu May 10, 2012 9:51 am
by slembcke
You are using Space Manager right? What is "self" in that method? I'm guessing that it's probably your layer.
You generally put the collision handler on your game state/scene/controller object. I assume you want "self" to refer to the bullet, but it doesn't work like that. When you want to access a specific object, you need to do that through the data pointers on the shapes or bodies.
Re: How to handle a collision that is happening to fast?
Posted: Thu May 10, 2012 10:50 am
by Ichigo
Yes, i'm using SpaceManager and yes the "self" refers to the bullet.
The truth is i've tried to use the data pointers before. Initially when i define shape->data i do like this:
Code: Select all
shape->data = self //(Self being the bullet object).
But then, when i try to get that data, i always just get a cpShape. Never the object itself (in this case, the Bullet)
I think you're saying that i should do like this:
Code: Select all
- (BOOL) handleCollision:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space
{
if (moment == COLLISION_BEGIN || moment == COLLISION_PRESOLVE) {
CP_ARBITER_GET_SHAPES(arb, bullet, wall);
Bullet *b = (Bullet *)bullet->data /*but this only gives me a cpShape. Even if i do cpShape(instance)->data it still gives me a cpShape.*/
//And if the things work like i think it would, i should be able to do:
[b prepareTodie];
[b die];
// This happening would be like magic. Believe me, i also want to use the cpPointersData.
}
return YES;
}
The only think i can think of is that, i must doing something wrong. Can you help me? =)
Re: How to handle a collision that is happening to fast?
Posted: Thu May 10, 2012 2:56 pm
by slembcke
Code: Select all
Bullet *b = (Bullet *)bullet->data
Should work just fine. What exactly is the compiler error? I have no clue why it would say otherwise, the data point is of type cpDataPointer which defaults to void *.
Re: How to handle a collision that is happening to fast?
Posted: Thu May 10, 2012 3:46 pm
by Ichigo
When i run the application i get this:
[cpCCSprite prepareToDie]: unrecognized selector sent to instance 0x95c0ec
Re: How to handle a collision that is happening to fast?
Posted: Thu May 10, 2012 3:55 pm
by slembcke
That means your shape's data pointer was pointing to a cpCCSprite and not a Bullet.
I think cpCCSprite is a SpaceManager thing.
Re: How to handle a collision that is happening to fast?
Posted: Thu May 10, 2012 4:55 pm
by Ichigo
But that is what doesn't make any sense, cuz i'm doing:
shape->data = self;
Being the shape a cpShape instance.
And the self a reference to the Bullet class.
I really don't understand. But tell me this, is this is way it' can detect multiple colisions? or is it because of that CCD (Continue Collision Detection) thing?
The collisions normally happened one second after another so.. it's certanly not for that.
Re: How to handle a collision that is happening to fast?
Posted: Thu May 10, 2012 9:04 pm
by slembcke
Yes, but are you sure it's not being overwritten later by SpaceManager? I have little idea how SpaceManager works as I didn't write it, and haven't looked at it much. I know that it provides a sprite class that synchronizes sprites to shapes by using the shape's data pointer.
I'm not quite sure I understood the CCD question, but Chipmunk does not perform CCD.
Re: How to handle a collision that is happening to fast?
Posted: Fri May 11, 2012 5:10 am
by Ichigo
I'm not sure if the space manager change it or not.
Anyway, the question about CCD was because you still didn't answer my thread question.
How can i make sure that the detecion is always detect between the bullet and the wall?
Re: How to handle a collision that is happening to fast?
Posted: Fri May 11, 2012 7:34 am
by slembcke
Oh, I misunderstood the question then.
If you want to guaranteed detection of the bullet collision, you'll want to use segment queries (raycasts) instead of rigid bodies for the bullets. They are simpler, faster and more accurate anyway.