Touches Crashes

Official forum for the Chipmunk2D Physics Library.
Post Reply
bizpo
Posts: 12
Joined: Mon Nov 14, 2011 7:26 pm
Contact:

Touches Crashes

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

Re: Touches Crashes

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
bizpo
Posts: 12
Joined: Mon Nov 14, 2011 7:26 pm
Contact:

Re: Touches Crashes

Post 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);    
}
bizpo
Posts: 12
Joined: Mon Nov 14, 2011 7:26 pm
Contact:

Re: Touches Crashes

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

Re: Touches Crashes

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
bizpo
Posts: 12
Joined: Mon Nov 14, 2011 7:26 pm
Contact:

Re: Touches Crashes

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

Re: Touches Crashes

Post 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.
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 39 guests