How to handle a collision that is happening to fast?

Official forum for the Chipmunk2D Physics Library.
Ichigo
Posts: 13
Joined: Wed May 02, 2012 7:23 am
Contact:

How to handle a collision that is happening to fast?

Post 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;
}
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to handle a collision that is happening to fast?

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Ichigo
Posts: 13
Joined: Wed May 02, 2012 7:23 am
Contact:

Re: How to handle a collision that is happening to fast?

Post 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? =)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to handle a collision that is happening to fast?

Post 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 *.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Ichigo
Posts: 13
Joined: Wed May 02, 2012 7:23 am
Contact:

Re: How to handle a collision that is happening to fast?

Post by Ichigo »

When i run the application i get this:

[cpCCSprite prepareToDie]: unrecognized selector sent to instance 0x95c0ec
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to handle a collision that is happening to fast?

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Ichigo
Posts: 13
Joined: Wed May 02, 2012 7:23 am
Contact:

Re: How to handle a collision that is happening to fast?

Post 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.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to handle a collision that is happening to fast?

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Ichigo
Posts: 13
Joined: Wed May 02, 2012 7:23 am
Contact:

Re: How to handle a collision that is happening to fast?

Post 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?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to handle a collision that is happening to fast?

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests