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; }
public GameFieldHalfDragListener(MTComponent dragComp) { this.comp = dragComp; if (comp.getUserData("box2d") == null) { throw new RuntimeException("GameFieldHalfDragListener has to be given a physics object!"); } }