Example #1
0
  public void render(OrthographicCamera camera) {
    // update the world with a fixed time step
    world.step(Gdx.app.getGraphics().getDeltaTime() * 10, 8, 3);
    if (mouseJoint != null) mouseJoint.setTarget(bob.getBody().getWorldCenter());

    // Destroy mouseJoint? (drop item)
    if (destroyMousejoint == true) {
      if (!world.isLocked()) {
        world.destroyJoint(mouseJoint);
        mouseJoint = null;
        destroyMousejoint = false;
      }
    }

    // Delete any bodies up for deletion
    if (!bodiesToDelete.isEmpty()) {
      // Make sure it is safe to delete!!
      if (!world.isLocked()) {
        for (Body body : bodiesToDelete) {
          world.destroyBody(body);
          body.setUserData(null);
          body = null;
        }
        bodiesToDelete.clear(); // Don't forget to clear the null bodies!
      }
    }

    // Create any bodies up for creation
    if (!bodiesToCreate.isEmpty()) {
      // Make sure it is safe to create!!
      if (!world.isLocked()) {
        for (BodyDef body : bodiesToCreate) {
          world.createBody(body);
        }
        bodiesToCreate.clear(); // Don't forget to clear!
      }
    }

    // Create any joints up for creation
    if (!jointsToCreate.isEmpty()) {
      // Make sure it is safe to create!!
      if (!world.isLocked()) {
        for (JointDef body : jointsToCreate) {
          mouseJoint = (MouseJoint) world.createJoint(body);
        }
        jointsToCreate.clear(); // Don't forget to clear!
      }
    }
    // render the world using the debug renderer
    renderer.render(world, camera.combined);
  }
Example #2
0
 public void jumpBob(Vector2 jump) {
   bob.getBody().applyLinearImpulse(jump, bob.getBody().getWorldCenter(), true);
 }
Example #3
0
 // Bob-specific methods ************************************************************
 public void moveBob(Vector2 direction) {
   bob.getBody().applyForceToCenter(direction, true);
 }
Example #4
0
  public void createPhysicsWorld() {

    xEngine = new ExplosionEngine();

    // create the debug renderer
    renderer = new Box2DDebugRenderer();

    // create the world
    world = new World(new Vector2(0, -3), true);

    bob.setBody(createPhysicsObject(bob.getBounds(), ModelType.PLAYER, -1).getBody());

    // Create our enemy's physical representations
    for (Map.Entry<Civilian, Integer> civilian : level.getEnemies().entrySet()) {
      civilian
          .getKey()
          .setFixture(
              createPhysicsObject(
                  civilian.getKey().getBounds(), ModelType.ENEMY, civilian.getValue()));
    }

    // Create our item's physical representations
    for (Map.Entry<Item, Integer> item : level.getItems().entrySet()) {
      item.getKey()
          .setFixture(
              createPhysicsObject(item.getKey().getBounds(), ModelType.ITEM, item.getValue()));
    }

    // Create our level out of blocks
    for (Block block : level.getBlocks()) {
      createBoxes(block.getBounds());
    }

    //	createDestruction();

    world.setContactListener(
        new ContactListener() {
          @Override
          public void beginContact(Contact contact) {

            // Get our UserData object, and check for contacts
            UserData dataA = (UserData) contact.getFixtureA().getUserData();
            UserData dataB = (UserData) contact.getFixtureA().getUserData();

            /** Used for determining if character can jump or not * */
            if (dataA.modelType == ModelType.FOOT_SENSOR) {
              if (dataA.parentId == -1) { // PlayerCharacter's foot
                bob.setNumFootContacts(1);
                bob.canJump(true);
              }
            }
            if (dataB.modelType == ModelType.FOOT_SENSOR) {
              if (dataB.parentId == -1) { // PlayerCharacter's foot
                bob.setNumFootContacts(1);
              }
            }

            if (dataA.modelType == ModelType.LEFT_SIDE_SENSOR) {
              if (dataA.parentId == -1) { // PlayerCharacter's foot
                bob.setNumLeftSideContacts(1);
                bob.canJump(true);
              }
            }
            if (dataB.modelType == ModelType.LEFT_SIDE_SENSOR) {
              if (dataB.parentId == -1) { // PlayerCharacter's foot
                bob.setNumLeftSideContacts(1);
              }
            }

            if (dataA.modelType == ModelType.RIGHT_SIDE_SENSOR) {
              if (dataA.parentId == -1) { // PlayerCharacter's foot
                bob.setNumRightSideContacts(1);
                bob.canJump(true);
              }
            }
            if (dataB.modelType == ModelType.RIGHT_SIDE_SENSOR) {
              if (dataB.parentId == -1) { // PlayerCharacter's foot
                bob.setNumRightSideContacts(1);
              }
            }

            /** Touching an item (used for picking up items) * */
            if (dataA.modelType == ModelType.PLAYER && dataB.modelType == ModelType.ITEM
                || dataB.modelType == ModelType.ITEM && dataA.modelType == ModelType.PLAYER) {

              MouseJointDef def = new MouseJointDef();
              def.bodyA = contact.getFixtureA().getBody();
              def.bodyB = contact.getFixtureB().getBody();
              dataA = (UserData) def.bodyA.getUserData();
              def.collideConnected = false;
              def.target.set(testPoint.x, testPoint.y);

              if (dataA.modelType == ModelType.PLAYER) {
                def.target.set(def.bodyA.getWorldCenter());
                def.maxForce = 1000.0f * def.bodyA.getMass();
              } else {
                def.target.set(def.bodyB.getWorldCenter());
                def.maxForce = 1000.0f * def.bodyB.getMass();
              }
              jointsToCreate.add(def);
            }
          }

          @Override
          public void endContact(Contact contact) {
            // Get our UserData object, and check for contacts
            UserData dataA = (UserData) contact.getFixtureA().getUserData();
            UserData dataB = (UserData) contact.getFixtureA().getUserData();

            /** Used for determining if character can jump or not * */
            if (dataA.modelType == ModelType.FOOT_SENSOR) {
              if (dataA.parentId == -1) { // PlayerCharacter's foot
                bob.setNumFootContacts(-1);
              }
            }
            if (dataB.modelType == ModelType.FOOT_SENSOR) {
              if (dataB.parentId == -1) { // PlayerCharacter's foot
                bob.setNumFootContacts(-1);
              }
            }
          }

          @Override
          public void preSolve(Contact contact, Manifold oldManifold) {}

          @Override
          public void postSolve(Contact contact, ContactImpulse impulse) {}
        });
  }