Page 1 of 1

Touches Crashes

Posted: Thu Dec 01, 2011 4:34 pm
by bizpo
I used the following code to register touches for my Chimpmunk bodies. I find that if I drag a finger from off the iPad screen onto the iPad screen the app crashes. At first I thought it was reacting (crashing) to having two fingers on the screen - but I have isolated it to having one finger being dragged from off the screen - back on the screen - very strange. I used this code based on the awesome RW tutorial.

This is the error message

Aborting due to Chipmunk error: Unsolvable constraint.
Failed condition: determinant != 0.0
Source:CatNap/libs/Chipmunk/include/chipmunk/constraints/util.h:123
(gdb)



- Inside the init method

Code: Select all

        
mouse = cpMouseNew(space);
        self.isTouchEnabled = YES;

Code: Select all

- (void)registerWithTouchDispatcher {
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
    cpMouseGrab(mouse, touchLocation, false);
    return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
    cpMouseMove(mouse, touchLocation);
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    cpMouseRelease(mouse);
}

- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event {
    cpMouseRelease(mouse);    
}

Re: Touches Crashes

Posted: Thu Dec 01, 2011 6:31 pm
by slembcke
Make sure that you aren't accidentally grabbing a static shape when the finger comes back on screen. I would guess this is what is happening.

While I didn't write cpMouse, I assume it works similarly to my own mouse code. Creating a joint between an infinite mass object controlled by the mouse and whatever you grab. If you create a joint between two infinite mass objects you end up generating an unstoppable force and trying to apply it to an unmovable object. That is what the assertion is catching.

Re: Touches Crashes

Posted: Fri Dec 02, 2011 4:54 pm
by bizpo
This is it - I removed the ground coded below and now it does not crash. Any recommendations for coding the ground so that it doesn't cause this problem? Thanks!

Code: Select all

// Add new method
- (void)createBottom {    
    // 1
    CGSize winSize = [CCDirector sharedDirector].winSize; 
    CGPoint lowerLeft = ccp(0, 0);
    CGPoint lowerRight = ccp(winSize.width, 0);
    
    // 2
    cpBody *groundBody = cpBodyNewStatic();
    
    // 3
    float radius = 10.0;
    cpShape *groundShape = cpSegmentShapeNew(groundBody, lowerLeft, lowerRight, radius);
    
    // 4
    groundShape->e = 0.5; // elasticity
    groundShape->u = 1.0; // friction 
    
    // 5
    cpSpaceAddShape(space, groundShape);    
}

Re: Touches Crashes

Posted: Fri Dec 02, 2011 5:42 pm
by bizpo
I suppose the true question now is - how to add walls and objects that create obstacles for movable bodies without causing this problem. Thanks

Re: Touches Crashes

Posted: Fri Dec 02, 2011 5:48 pm
by slembcke
I usually use the layers or groups to filter out queries to static shapes: https://github.com/slembcke/Chipmunk-Ph ... emo.c#L351

Alternatively, instead of using cpSpacePointQueryFirst() you can use cpSpacePointQuery() to get a list of all shapes at that point and filter them any way you want.

Re: Touches Crashes

Posted: Sun Dec 11, 2011 1:17 am
by bizpo
I haven't been able to code this yet. If you get a chance , I'd love to see the first part of your tutorial with the boxes and floor filter out the floor for mouse joints. Thanks.

Re: Touches Crashes

Posted: Sun Dec 11, 2011 2:51 pm
by slembcke
That's what the link I posted above was. It's the code I use in the demo application for filtering the shapes you want to be grabbable.