Beispiel #1
0
 public boolean processGestureEvent(MTGestureEvent ge) {
   DragEvent de = (DragEvent) ge;
   try {
     Body body = (Body) comp.getUserData("box2d");
     MouseJoint mouseJoint;
     Vector3D to = new Vector3D(de.getTo());
     // Un-scale position from mt4j to box2d
     PhysicsHelper.scaleDown(to, scale);
     switch (de.getId()) {
       case DragEvent.GESTURE_STARTED:
         comp.sendToFront();
         body.wakeUp();
         body.setXForm(new Vec2(to.x, to.y), body.getAngle());
         mouseJoint = PhysicsHelper.createDragJoint(world, body, to.x, to.y);
         comp.setUserData(comp.getID(), mouseJoint);
         break;
       case DragEvent.GESTURE_UPDATED:
         mouseJoint = (MouseJoint) comp.getUserData(comp.getID());
         if (mouseJoint != null) {
           boolean onCorrectGameSide =
               ((MTComponent) de.getTarget()).containsPointGlobal(de.getTo());
           // System.out.println(((MTComponent)de.getTargetComponent()).getName()  + " Contains
           // " + to + " -> " + contains);
           if (onCorrectGameSide) {
             mouseJoint.setTarget(new Vec2(to.x, to.y));
           }
         }
         break;
       case DragEvent.GESTURE_ENDED:
         mouseJoint = (MouseJoint) comp.getUserData(comp.getID());
         if (mouseJoint != null) {
           comp.setUserData(comp.getID(), null);
           // Only destroy the joint if it isnt already (go through joint list and check)
           for (Joint joint = world.getJointList(); joint != null; joint = joint.getNext()) {
             JointType type = joint.getType();
             switch (type) {
               case MOUSE_JOINT:
                 MouseJoint mj = (MouseJoint) joint;
                 if (body.equals(mj.getBody1()) || body.equals(mj.getBody2())) {
                   if (mj.equals(mouseJoint)) {
                     world.destroyJoint(mj);
                   }
                 }
                 break;
               default:
                 break;
             }
           }
         }
         mouseJoint = null;
         break;
       default:
         break;
     }
   } catch (Exception e) {
     System.err.println(e.getMessage());
   }
   return false;
 }
Beispiel #2
0
 @Override
 public boolean onTouchEvent(MotionEvent event) {
   Vec2 p = new Vec2(event.getX(), event.getY());
   switch (event.getAction()) {
     case MotionEvent.ACTION_DOWN:
       break;
     case MotionEvent.ACTION_UP:
       if (mouseJoint != null) {
         myWorld.getWorld().destroyJoint(mouseJoint);
         mouseJoint = null;
       }
       break;
     case MotionEvent.ACTION_MOVE:
       if (mouseJoint != null) {
         mouseJoint.setTarget(p);
         break;
       }
       queryAABB.lowerBound.set(p.x - .001f, p.y - .001f);
       queryAABB.upperBound.set(p.x + .001f, p.y + .001f);
       callback.point.set(p);
       callback.fixture = null;
       myWorld.getWorld().queryAABB(callback, queryAABB);
       if (callback.fixture != null) {
         Body body = callback.fixture.getBody();
         XKPPhysicBody physicBody = (XKPPhysicBody) body.getUserData();
         if (!physicBody.getMouseJoint()) break;
         MouseJointDef def = new MouseJointDef();
         def.bodyA = groundBody;
         def.bodyB = body;
         def.target.set(p);
         def.maxForce = 3000f * body.getMass();
         def.dampingRatio = 0;
         def.frequencyHz = 1000;
         mouseJoint = (MouseJoint) myWorld.getWorld().createJoint(def);
         body.setAwake(true);
       }
       break;
   }
   return super.onTouchEvent(event);
 }