Question

Here's the problem: I have a figure, that has both sprite and body. This figure is dragable, and on the touch event's Action Down I need body to be disconnected so it could not collide with other bodies while being dragged. Then on Action Up body should be connected to the sprite. There are two methods I've defined to solve this problem.

protected void connectBody() {
            if (!bodyConnected) {
                    connector = new PhysicsConnector(mSprite, mBody, true, true);
                    mPhysicsWorld.registerPhysicsConnector(connector);
                    bodyConnected = true;
            }
    }

protected void disconnectBody() {
            if (bodyConnected) {
                    if (connector != null) {
                            mPhysicsWorld.unregisterPhysicsConnector(connector);
                    }
                    bodyConnected = false;
            }
    }

However, this doesn't work. When I drag the figure it collides with other figures. Can anyone help me solve this one? Thanks a lot in advance.

Was it helpful?

Solution

I played around with it couple of days ago and this was what I did to drag an object.

public boolean onAreaTouched( final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea,final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
        if(pSceneTouchEvent.isActionDown()) {
            final AnimatedSprite area = (AnimatedSprite) pTouchArea;
            PhysicsConnector tPhysicsConnector;
            tPhysicsConnector = physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(area);
            physicsWorld.unregisterPhysicsConnector(tPhysicsConnector);
            physicsWorld.destroyBody(tPhysicsConnector.getBody()); 

        }

OTHER TIPS

I stumbled across this posting facing the same issue, and although lordraiders answer helped me get on the right track, I found a better solution and decided to post it here. The solution does not require destroying/recreating any bodies. Just do the following:

if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN){
    physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(self).getBody().setActive(false);
}

if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_UP){
    physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(self).getBody().setActive(true);
}

So this way you're just setting the objects active state on/off and are free to drag the object around without the physics forces fighting your drag action. You may also want to reset/clear all current forces applied to the object otherwise when you drop the object, it will resume whatever action it was previously engaged in. For example, if you picked up a ball rolling down a hill, when you release it, it would begin moving as if going down hill because those forces were saved in the state of the object when you disabled the physical body. You can do this like so:

physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(self).getBody().setAngularVelocity(0);

physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(self).getBody().setLinearVelocity(0, 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top