Problem with collision impact

Official forum for the Chipmunk2D Physics Library.
Post Reply
TonyB
Posts: 14
Joined: Tue Oct 18, 2011 4:19 am
Contact:

Problem with collision impact

Post by TonyB »

Hi folks,

I'm using Chipmunk for my new project and seem to be having some inconsistencies in detecting collision impact.
The collision appears to happen, and the collision handler is invoked, but when I try to calculate the impact it often reports back as zero.

Here's the code for the collision detectors (much based on the AngryChipmunk example, but not in the Pro version).

Code: Select all

static void ApplyWoodDamage(cpSpace *space, wood *woodenBlock, cpArbiter *arb, GameLayerSimple *self) {

    if(cpArbiterIsFirstContact(arb)){

        cpFloat impact = cpvlength(cpArbiterTotalImpulse(arb));
        NSLog(@"Original impact %f", impact);
        
        impact = impact/FIXED_TIMESTAMP;
        NSLog(@"Divided impact %f", impact);
        
        NSLog(@"wood strength = %f", woodenBlock.wStrength);
                
        if(impact > 1.0f){
            NSLog(@"impact/500 = %f", impact/500.0f);
            woodenBlock.wStrength -= impact/500.0f;
            NSLog(@"wood strength after = %f", woodenBlock.wStrength);
            if(woodenBlock.wStrength < 0.0f){
                cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepRemoveWood, woodenBlock, self);
                NSLog(@"Need postStepCallback to remove wood");
            }
        }
    }
}

static void postsolve_wood(cpArbiter *arb, cpSpace *space, void *unused) {
    cpShape *a, *b; cpArbiterGetShapes(arb, &a, &b);
    ApplyWoodDamage(space, b->data, arb, unused);
}
My step: process updates the active & static hashes.

Am I missing something when it comes to the impulse/impact? Do I need to reset something after the first collision is detected?

Here's some output from the log.

Code: Select all

2011-11-16 21:07:39.674 bloksnroks[12342:1be03] Original impact 201.118423
2011-11-16 21:07:39.675 bloksnroks[12342:1be03] Divided impact 12067.104492
2011-11-16 21:07:39.676 bloksnroks[12342:1be03] wood strength = 4.000000
2011-11-16 21:07:39.677 bloksnroks[12342:1be03] impact/500 = 24.134209
2011-11-16 21:07:39.677 bloksnroks[12342:1be03] wood strength after = -20.134209
2011-11-16 21:07:39.680 bloksnroks[12342:1be03] Need postStepCallback to remove wood


2011-11-16 21:08:09.174 bloksnroks[12342:1be03] Original impact 0.000000
2011-11-16 21:08:09.175 bloksnroks[12342:1be03] Divided impact 0.000000
2011-11-16 21:08:09.175 bloksnroks[12342:1be03] wood strength = 24.000000
2011-11-16 21:08:09.623 bloksnroks[12342:1be03] Original impact 0.000000
2011-11-16 21:08:09.624 bloksnroks[12342:1be03] Divided impact 0.000000
2011-11-16 21:08:09.625 bloksnroks[12342:1be03] wood strength = 24.000000
As you can see the first impact is detected OK, but next time there is nothing. For the second collision an object drops from the top of the screen to a static object at the bottom of the screen, yet nothing.

Any help please?

Thanks,
Tony B

EDIT: Further log of inconsistent impact.
2011-11-16 21:21:03.676 bloksnroks[12553:1be03] Original impact 0.000000
2011-11-16 21:21:03.677 bloksnroks[12553:1be03] Divided impact 0.000000
2011-11-16 21:21:03.678 bloksnroks[12553:1be03] wood strength = 48.000000
2011-11-16 21:21:04.361 bloksnroks[12553:1be03] Original impact 0.000000
2011-11-16 21:21:04.362 bloksnroks[12553:1be03] Divided impact 0.000000
2011-11-16 21:21:04.362 bloksnroks[12553:1be03] wood strength = 48.000000
2011-11-16 21:21:04.643 bloksnroks[12553:1be03] Original impact 0.000000
2011-11-16 21:21:04.644 bloksnroks[12553:1be03] Divided impact 0.000000
2011-11-16 21:21:04.645 bloksnroks[12553:1be03] wood strength = 48.000000
2011-11-16 21:21:04.710 bloksnroks[12553:1be03] Original impact 106.263054
2011-11-16 21:21:04.710 bloksnroks[12553:1be03] Divided impact 6375.782715
2011-11-16 21:21:04.712 bloksnroks[12553:1be03] wood strength = 48.000000
2011-11-16 21:21:04.713 bloksnroks[12553:1be03] impact/500 = 12.751565
2011-11-16 21:21:04.714 bloksnroks[12553:1be03] wood strength after = 35.248436
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem with collision impact

Post by slembcke »

Are you using Chipmunk 5.x and have elastic iterations enabled? Elastic iterations caused problems with reading back collision impulses and were eventually deprecated.

Other than that I'm not really sure what else would cause that problem unless it really didn't apply any impulse. That can only happen if you brush past the side of an object without really hitting it.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
TonyB
Posts: 14
Joined: Tue Oct 18, 2011 4:19 am
Contact:

Re: Problem with collision impact

Post by TonyB »

Thanks for the quick reply.

It seems I have elastic iterations set to a value of 10, so I'm guessing I'm using a 5.x version of Chipmunk - is there an easy way to tell?

Regardless, I think it's time for me to update to 6.0.1.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem with collision impact

Post by slembcke »

Check cpVersionString. Also if you are running it in debug mode it will print out the version number to the console.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
TonyB
Posts: 14
Joined: Tue Oct 18, 2011 4:19 am
Contact:

Re: Problem with collision impact

Post by TonyB »

Ah! Seems I'm running 5.3.5 (that's how long ago I installed it and am only now just getting to use it!!)

Sorry to ask a dumb question, but is there an easy way to update the version?

I can see in my xcode project I have the Chipmunk libraries. Having tried to delete the old & drop in the new, I get countless compile errors - so I'm thinking the drag/drop didn't work.

Is there a command line I can execute to install/update the new libraries?
TonyB
Posts: 14
Joined: Tue Oct 18, 2011 4:19 am
Contact:

Re: Problem with collision impact

Post by TonyB »

Progress - am getting somewhere. I managed to load the new files using Finder rather than Xcode. Seemed to be a small problem cos of the sub-folders used.

Anyway, what's happened to the following two commands?

cpSpaceResizeStaticHash(space, 400, 200);
cpSpaceResizeActiveHash(space, 200, 200);

Why would these now tell me that the declaration is invalid?

Are the commands still the same in the updated version?
(Can I be cheeky and ask what they have been replaced with, if they have been?)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem with collision impact

Post by slembcke »

This will be helpful when updating:
https://github.com/slembcke/Chipmunk-Ph ... ERSION.txt

Chipmunk 6.x was a pretty big change from 5.x in a lot of places. In 5.x I had documented a lot of fields and functions as private, in Chipmunk 6.x they are actually not directly accessible any more.

The two biggest changes that trip people up are the cpSpaceResize*Hash() tuning functions and cpSpaceHashEach(). cpSpaceResize*Hash() is completely gone and unneeded. Chipmunk now has better collision detection performance without any tuning. Delete those lines of code and forget they ever existed. If you were doing something like cpSpaceHashEach(space->activeHash, someFunction, somePointer), that is all gone as well. You have to use cpSpaceEachShape(space, someFunction, somePointer) instead.

To upgrade, I would delete the old Chipmunk code out of your project entirely. Run the macosx/iphonestatic.command script in the Chipmunk download to make a static library and drag the folder it builds into your project. That way you can be sure that all the correct compiler settings for performance have been set correctly.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
TonyB
Posts: 14
Joined: Tue Oct 18, 2011 4:19 am
Contact:

Re: Problem with collision impact

Post by TonyB »

Thank slembcke. I've now managed to update to the latest version of Chipmunk and my project is back on track & working. Collisions - and more so impacts - are working as I would expect!

Just need to catch up on the latest changes now.

Thanks again!
Tony B
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 24 guests