Trouble getting a physics body using touch events on ipad

Share your projects and Chipmunk enhancements.
Post Reply
21species
Posts: 2
Joined: Thu Apr 04, 2013 6:35 am
Contact:

Trouble getting a physics body using touch events on ipad

Post by 21species »

Hi,

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 = this.space.pointQueryFirst(point, GRABABLE_MASK_BIT, cp.NO_GROUP);
            if(shape){
                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);
                this.space.addConstraint(mouseJoint);
            }
        }
    },

    onTouchesEnded:function (pTouch,e){

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

    },
    onTouchesMoved:function(pTouch,e){

        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

Amit
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

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

Post by slembcke »

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! -> http://howlingmoonsoftware.com/wordpress/
21species
Posts: 2
Joined: Thu Apr 04, 2013 6:35 am
Contact:

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

Post by 21species »

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 !!
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests