Example #1
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);
 }
Example #2
0
 private void onLayoutStarted() {
   // create body ground
   BodyDef bodyDef = new BodyDef();
   groundBody = myWorld.getWorld().createBody(bodyDef);
   // TODO: determine real screen dimensions (width & height)
   ddView = new XKPDDView(this, 320, 480);
   layoutapplication.addView(ddView);
   XKPPhysicBody body_1 = new XKPPhysicBody(ActjBox2d_1.myWorld.getWorld(), ShapeType.CIRCLE);
   body_1.setSensor(false);
   body_1.setDensity(1f);
   body_1.setFriction(0.5f);
   body_1.setRestitution(0.200000003f);
   body_1.setBodyType(BodyType.DYNAMIC);
   body_1.setPosition(50, 200);
   body_1.createBody(20);
   XKPPhysicBody body_2 = new XKPPhysicBody(ActjBox2d_1.myWorld.getWorld(), ShapeType.POLYGON);
   body_2.setSensor(false);
   body_2.setDensity(1f);
   body_2.setFriction(0.5f);
   body_2.setRestitution(0.200000003f);
   body_2.setBodyType(BodyType.DYNAMIC);
   body_2.setPosition(30, 24);
   body_2.createBody(20, 30);
   XKPPhysicBody body_3 = new XKPPhysicBody(ActjBox2d_1.myWorld.getWorld(), ShapeType.CIRCLE);
   body_3.setSensor(false);
   body_3.setDensity(1f);
   body_3.setFriction(0.5f);
   body_3.setRestitution(0.200000003f);
   body_3.setBodyType(BodyType.DYNAMIC);
   body_3.setPosition(100, 60);
   body_3.createBody(30);
   XKPPhysicBody body_4 = new XKPPhysicBody(ActjBox2d_1.myWorld.getWorld(), ShapeType.POLYGON);
   body_4.setSensor(false);
   body_4.setDensity(1f);
   body_4.setFriction(0.5f);
   body_4.setRestitution(0.200000003f);
   body_4.setBodyType(BodyType.DYNAMIC);
   body_4.setPosition(150, 180);
   body_4.createBody(50, 20);
   XKPPhysicBody body_5 = new XKPPhysicBody(ActjBox2d_1.myWorld.getWorld(), ShapeType.CIRCLE);
   body_5.setSensor(false);
   body_5.setDensity(1f);
   body_5.setFriction(0.5f);
   body_5.setRestitution(0.200000003f);
   body_5.setBodyType(BodyType.DYNAMIC);
   body_5.setPosition(200, 100);
   body_5.createBody(10);
   XKPPhysicBody floor_1 = new XKPPhysicBody(ActjBox2d_1.myWorld.getWorld(), ShapeType.POLYGON);
   floor_1.setSensor(false);
   floor_1.setDensity(1f);
   floor_1.setFriction(0.5f);
   floor_1.setRestitution(0.200000003f);
   floor_1.setBodyType(BodyType.STATIC);
   floor_1.setPosition(0, 0);
   floor_1.createBody(10, 400);
   XKPPhysicBody floor_2 = new XKPPhysicBody(ActjBox2d_1.myWorld.getWorld(), ShapeType.POLYGON);
   floor_2.setSensor(false);
   floor_2.setDensity(1f);
   floor_2.setFriction(0.5f);
   floor_2.setRestitution(0.200000003f);
   floor_2.setBodyType(BodyType.STATIC);
   floor_2.setPosition(0, 0);
   floor_2.createBody(300, 10);
   XKPPhysicBody floor_3 = new XKPPhysicBody(ActjBox2d_1.myWorld.getWorld(), ShapeType.POLYGON);
   floor_3.setSensor(false);
   floor_3.setDensity(1f);
   floor_3.setFriction(0.5f);
   floor_3.setRestitution(0.200000003f);
   floor_3.setBodyType(BodyType.STATIC);
   floor_3.setPosition(300, 10);
   floor_3.createBody(10, 400);
   XKPPhysicBody floor_4 = new XKPPhysicBody(ActjBox2d_1.myWorld.getWorld(), ShapeType.POLYGON);
   floor_4.setSensor(false);
   floor_4.setDensity(1f);
   floor_4.setFriction(0.5f);
   floor_4.setRestitution(0.200000003f);
   floor_4.setBodyType(BodyType.STATIC);
   floor_4.setPosition(10, 400);
   floor_4.createBody(300, 10);
   mHandler = new android.os.Handler();
   mHandler.post(mRunnableWorld);
 }