Trouble getting a physics body using touch events on ipad

Share your projects and Chipmunk enhancements.

Trouble getting a physics body using touch events on ipad

Postby 21species » Wed Apr 24, 2013 11:01 am


I created a demo with touch events which works fine on the browser and the ipad simulator using cocos2dx - javascript bindings, but it does not return the polyshape or body under the touch point. It just returns a shape.

These are my touch events.
Code: Select all

onTouchesBegan:function (pTouch,pEvent){

        self.mouse = this.touch2point(pTouch[0].getLocation().x, pTouch[0].getLocation().y);
        this.mouseBody.p = this.touch2point(pTouch[0].getLocation().x, pTouch[0].getLocation().y);

        if(!self.mouseJoint) {
            var point = this.touch2point(pTouch[0].getLocation().x, pTouch[0].getLocation().y);
            var shape =, GRABABLE_MASK_BIT, cp.NO_GROUP);
                var body = shape.body;
                var mouseJoint = self.mouseJoint = new cp.PivotJoint(this.mouseBody, body, this.touch2point(pTouch[0].getLocation().x, pTouch[0].getLocation().y));

                mouseJoint.maxForce = 50000;
                mouseJoint.errorBias = Math.pow(1 - 0.15, 60);

    onTouchesEnded:function (pTouch,e){

        self.mouse = this.touch2point(pTouch[0].getLocation().x, pTouch[0].getLocation().y);
        if(self.mouseJoint) {
            self.mouseJoint = null;


        self.mouse = this.touch2point(pTouch[0].getLocation().x, pTouch[0].getLocation().y);
        var newPoint = v.lerp(this.mouseBody.p, self.mouse, 0.25);
        this.mouseBody.v = v.mult(v.sub(newPoint, this.mouseBody.p), 60);
        //this.mouseBody.p = cc.p(newPoint.x,140);
        this.mouseBody.p = newPoint;


After that, any call to shape.getbody() crashes the application coz it doesn't get the body under the touch point.

In the xcode debugger this is the error
Thread 1: EXC_BAD_ACCESS ( code=1,address=0x4) and it stops on line CP_DefineShapeStructGetter(cpBody*, body, Body)

Is there an issue with my touch events coz it works fine in the browser and on the simulator.

Cheers and thanks

Posts: 2
Joined: Thu Apr 04, 2013 6:35 am

Re: Trouble getting a physics body using touch events on ipa

Postby slembcke » Wed Apr 24, 2013 11:48 am

Hmm. I'm not very familiar with the Cocos-JS bindings. Riq also made the JS bindings for Chipmunk to go along with that, so I don't have a lot of experience what that either.

I think the best advice I can give at this point is to put a breakpoint on cpSpacePointQueryFirst, step through it and see if the return value seems like a valid shape. It sounds like the EXC_BAD_ACCESS is happening when you are trying to dereference a invalid shape pointer. I'm not exactly certain how Chipmunk could return such a wacky value though unless the entire space had already been freed.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! ->
User avatar
Site Admin
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Trouble getting a physics body using touch events on ipa

Postby 21species » Wed Apr 24, 2013 1:21 pm

Many Thanks for the tip . I did try that and it was giving all the values but was quite unclear wt was going wrong.

What would be really great and helpful is if Riq could do just a quick sample of a single physics circle object with a drag using a mouse joint on touch down .. i am sure that'll solve the issue .. the cocos 2dx chipmunk samples dont have mouse drag function .. hence searching all over.

hope its not too much to ask ??! :?

Cheers and thanx again !!
Posts: 2
Joined: Thu Apr 04, 2013 6:35 am

Return to Community Creations

Who is online

Users browsing this forum: No registered users and 0 guests