Exemple #1
0
  public void beginContact(Contact contact) {
    if (!contact.isTouching()) {
      return;
    }

    Fixture fixture0 = contact.getFixtureA();
    Fixture fixture1 = contact.getFixtureB();

    Item item0 = (Item) fixture0.getUserData();
    Item item1 = (Item) fixture1.getUserData();

    item0.contacted(item1);
  }
  @Override
  public void endContact(Contact contact) {
    Fixture fixA = contact.getFixtureA();
    Fixture fixB = contact.getFixtureB();

    if ((fixA.isSensor()) && (fixA.getUserData() != null)) {
      B2Controller b2c = (B2Controller) fixA.getUserData();
      b2c.removeBody(fixB.getBody());
    } else if ((fixB.isSensor()) && (fixB.getUserData() != null)) {
      B2Controller b2c = (B2Controller) fixB.getUserData();
      b2c.removeBody(fixA.getBody());
    }
  }
  // called when two fixtures no longer collide
  @Override
  public void endContact(Contact c) {
    Fixture fa = c.getFixtureA();
    Fixture fb = c.getFixtureB();

    if (fa == null || fb == null) {
      return;
    }

    if (fa.getUserData() != null && fa.getUserData().equals("foot")) {
      numFootContacts--;
    }
    if (fb.getUserData() != null && fb.getUserData().equals("foot")) {
      numFootContacts--;
    }
  }
  @Override
  public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
    UserData data = (UserData) fixture.getUserData();
    if (data.modelType == ModelType.WALL) {}

    // TODO Auto-generated method stub
    return 0;
  }
  @Override
  public void beginContact(Contact contact) {
    Fixture fa = contact.getFixtureA();
    Fixture fb = contact.getFixtureB();

    // System.out.println("Hello");

    if (fa == null || fb == null) {
      return;
    }

    WorldObject wo1 = (WorldObject) fa.getUserData();
    WorldObject wo2 = (WorldObject) fb.getUserData();

    if (wo1 == null || wo2 == null) {
      return;
    }

    int id1 = wo1.getCId();
    int id2 = wo2.getCId();

    for (ContactListener listener : listeners) {
      // System.out.println("Testing");
      int mask1 = listener.firstMask;
      int mask2 = listener.secondMask;
      /*System.out.format("id1|id2\nmask1|mask2\n%8s|%8s\n%8s|%8s\n",
      Integer.toBinaryString(id1), Integer.toBinaryString(id2),
      Integer.toBinaryString(mask1),
      Integer.toBinaryString(mask2));*/
      if ((id1 & mask1) != 0 && (id2 & mask2) != 0) {
        // System.out.println("Fire normal");
        listener.action.onContact(fa, fb);
      }
      if ((id1 & mask2) != 0 && (id2 & mask1) != 0) {
        // System.out.println("Fire reverse");
        listener.action.onContact(fb, fa);
      }
    }
  }
 private boolean checkSensor(Fixture sensor, Fixture other) {
   final SensorType type = (SensorType) sensor.getUserData();
   final Object sensorData = sensor.getBody().getUserData();
   final Object otherData = other.getBody().getUserData();
   if (type == null) return false;
   switch (type) {
     case SCORE:
       if (otherData instanceof Player) {
         events.queueEvent(EventType.PLAYER_SCORED, sensorData);
       }
       return true;
     default:
       break;
   }
   return false;
 }
  // called when two fixtures start to collide
  @Override
  public void beginContact(Contact c) {
    Fixture fa = c.getFixtureA();
    Fixture fb = c.getFixtureB();

    if (fa == null || fb == null) {
      return;
    }

    if (fa.getUserData() != null && fa.getUserData().equals("foot")) {
      numFootContacts++;
    }
    if (fb.getUserData() != null && fb.getUserData().equals("foot")) {
      numFootContacts++;
    }
    if (fa.getUserData() != null && fa.getUserData().equals("crystal")) {
      bodiesToRemove.add(fa.getBody());
    }
    if (fb.getUserData() != null && fb.getUserData().equals("crystal")) {
      bodiesToRemove.add(fb.getBody());
    }
  }
  /**
   * Method is called on every collision between two Box2D bodies. Behaviour of certain body
   * collisions are defined in this method. Collisions between bodies are defined and accessed by
   * means of bitwise or-ing their category bits.
   */
  @Override
  public void beginContact(Contact c) {
    Fixture fa = c.getFixtureA();
    Fixture fb = c.getFixtureB();

    int collisionDefinition = fa.getFilterData().categoryBits | fb.getFilterData().categoryBits;

    switch (collisionDefinition) {
        // foot collides with floor
      case B2DVars.BIT_PLAYER_FOOT | B2DVars.BIT_BLOCKS:
        numJumps++;
        break;

        // Player collides with student, deal damage to player or kill enemy
      case B2DVars.BIT_PLAYER | B2DVars.BIT_STUDENT:
        System.out.println("hit!");
        playerStudentCollisionCount++;

        Vector2 playerPosition, playerVelocity, studentPosition;
        Body player, student;

        if (fa.getUserData().equals("player")) {
          player = fa.getBody();
          playerPosition = fa.getBody().getPosition();
          playerVelocity = fa.getBody().getLinearVelocity();
          student = fb.getBody();
          studentPosition = fb.getBody().getPosition();
        } else {
          player = fb.getBody();
          playerPosition = fb.getBody().getPosition();
          playerVelocity = fb.getBody().getLinearVelocity();
          student = fa.getBody();
          studentPosition = fa.getBody().getPosition();
        }

        // Player hits enemy from above, 0.1 offset is to ensure player is significantly above enemy
        if (playerPosition.y - 0.1 > studentPosition.y && playerVelocity.y <= 0) {
          Game.ass.getAudio("kill").play(1.0f);
          bodiesToKill.add(student);
          player.setLinearVelocity(player.getLinearVelocity().x, 2f);
        } else {
          ((Player) player.getUserData()).damagePlayer();
          Game.ass.getAudio("hit").play(1.0f);
          Vector2 point = new Vector2(playerPosition.x, playerPosition.y);

          if (playerPosition.x < studentPosition.x) {
            player.applyLinearImpulse(new Vector2(-30f, 0), point, true);
          } else {
            player.applyLinearImpulse(new Vector2(30f, 0), point, true);
          }
        }
        break;

        // player collides with coin
      case B2DVars.BIT_PLAYER | B2DVars.BIT_COINS:
        if (fa.getUserData().equals("coin")) {
          // delete coin
          bodiesToKill.add(fa.getBody());
          Game.ass.getAudio("coin").play(1.0f);
        } else {
          bodiesToKill.add(fb.getBody());
          Game.ass.getAudio("coin").play(1.0f);
        }
        break;

        // player collides with end of level region
      case B2DVars.BIT_PLAYER | B2DVars.BIT_END:
        if (fa.getUserData().equals("end")) {
          Player playerB = (Player) fb.getBody().getUserData();
          if (playerB.getNumCoins() == playerB.getNumCoinsToCollect()) {
            playerB.setLevelCompleteBool(true);
            break;
          }
          playerB.setLevelCompleteBool(false);
          break;
        } else {
          Player playerA = (Player) fa.getBody().getUserData();
          if (playerA.getNumCoins() == playerA.getNumCoinsToCollect()) {
            playerA.setLevelCompleteBool(true);
            break;
          }
          playerA.setLevelCompleteBool(false);
          break;
        }
    }
  }