title: How to drag and drop body using onTouchListener and still being in PhysicsWorld? AndEngine
body: <p>I want to drag and drop body with its sprite using touch. I do know how to drag and drop sprite,  but when I try to move body it... doesn't work, body stays still. What's more, sometimes when I touch body the whole app crashes :x
  Can anyone tell me how to drag and drop physic body that collides with other bodies during movement of finger? I've been searching for 3 days and im depressed :[</p>
  
  <p>I've created 3 similar bodies. One is dynamic(simulation of ball bouncing like in Hockey Game), two of them are kinematic(Will be moveable by players). I will show you declaration of the body I want to move.
  I don't know if any code is needed to show you but I implemented some interfaces found in really bad tutorial. :s</p>
  
  <pre><code>public class MainActivity extends SimpleBaseGameActivity implements IOnSceneTouchListener, IOnAreaTouchListener
  </code></pre>
  
  <p>Declaration:</p>
  
  <pre><code>//Declaration:
           final Sprite face = new Sprite(CAMERA_WIDTH/2+200f, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager()) {
              @Override
              public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                  this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
                  return true;
              }
          };
          final FixtureDef MyFixtureDef = PhysicsFactory.createFixtureDef(0.2f,0.4f,0.6f);
  
          this.scene.registerUpdateHandler(this.mPhysicsWorld);
          face.setScale(3);
  </code></pre>
  
  <p>Body:</p>
  
  <pre><code>final Body facebody = PhysicsFactory.createCircleBody(
          this.mPhysicsWorld, face, BodyType.KinematicBody,
          CIRCLE_FIXTURE_DEF);
      this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face,
          facebody, true, true));
      facebody.setUserData("player1");
  </code></pre>
  
  <p>Methods:</p>
  
  <p>I tried MouseJoint</p>
  
  <pre><code>  public MouseJoint createMouseJoint(IAreaShape face, float x, float y) {
  final Body boxBody = this.mPhysicsWorld.getPhysicsConnectorManager()
      .findBodyByShape(face);
  
  Vector2 v = boxBody.getWorldPoint(new Vector2(x / 32, y / 32));
  
  MouseJointDef mjd = new MouseJointDef();
  // mjd.bodyA = ballbody;
  mjd.bodyB = boxBody;
  mjd.dampingRatio = 0.2f;
  mjd.frequencyHz = 30;
  mjd.maxForce = (float) (200.0f * boxBody.getMass());
  mjd.collideConnected = true;
  mjd.target.set(v);
  return (MouseJoint) this.mPhysicsWorld.createJoint(mjd);
  }
  </code></pre>
  
  <p>nor onAreaTouched?</p>
  
  <pre><code>@Override
  public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
      if(pSceneTouchEvent.isActionDown()) {
          final IAreaShape face = (IAreaShape) pTouchArea;
          if(this.mMouseJointActive == null) {
              //this.mEngine.vibrate(100);
              this.mMouseJointActive = this.createMouseJoint(face, pTouchAreaLocalX, pTouchAreaLocalY);
          }
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>here just guessing</p>
  
  <pre><code>        @Override
          public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
              // TODO Auto-generated method stub
              if(pSceneTouchEvent.isActionDown()) {
                  Debug.d("here!");
  return true;
              }
              return false;
          }
  
      }
  </code></pre>
  
  <p>Question about class:</p>
  
  <p>If I declare class player inside MainActivity:</p>
  
  <pre><code> class Player {
  final Body facebody;
  final Sprite face = new Sprite(MainActivity.CAMERA_WIDTH / 2+200f,240, MainActivity.mFaceTextureRegion ,getVertexBufferObjectManager()) {
          @Override
          public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
              final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
  
          switch (pSceneTouchEvent.getAction()) {
          case TouchEvent.ACTION_MOVE:
              // Here 'body' refers to the Body object associated with
              // this sprite
              facebody.setTransform(pSceneTouchEvent.getX(),
                  pSceneTouchEvent.getY(), facebody.getAngle());
              break;
          default:
              break;
          }
          return true;
          }
      };
  
      Player(Body f) {
          facebody = f;
      }
  
      }
  </code></pre>
  
  <p>Everything is perfect but,it yells that <code>player.face</code> can't be declared in
  OnCreateScene in this declaration:</p>
  
  <pre><code>final Player player= new Player( PhysicsFactory.createCircleBody(   this.mPhysicsWorld, player.face, BodyType.KinematicBody,CIRCLE_FIXTURE_DEF));
  </code></pre>
  
  <p>How shall I solve this?</p>