// Updates the input, moving ike to the corresponding key presses
  private void updateInput() {
    final Vector2 vel = ikeBody.getLinearVelocity();
    final Vector2 pos = ikeBody.getPosition();

    if (ike.movingLeft && vel.x > -MAX_VELOCITY.x)
      ikeBody.applyLinearImpulse(-0.80f, 0, pos.x, pos.y, true);
    if (ike.movingRight && vel.x < MAX_VELOCITY.x)
      ikeBody.applyLinearImpulse(0.80f, 0, pos.x, pos.y, true);
  }
 public void applySteering(Body body) {
   // For Kinematic & Static bodies, we set the velocity
   BodyType type = body.getType();
   if (type == BodyType.KinematicBody || type == BodyType.StaticBody) {
     body.setLinearVelocity(velocity);
     body.setAngularVelocity(rotation);
   } else {
     // body is dynamic, so we apply an impulse
     body.applyLinearImpulse(velocity, body.getWorldCenter());
     body.applyAngularImpulse(rotation);
   }
 }
  private void processPhysicsMovement(
      Body body, InputComponent ic, Position pc, Velocity vc, Rotation rc, int id) {
    if (body == null) {
      logger.error("ERR: cannot process movement for ent#" + id + "; body == null");
      lifeCycleMapper.get(id).kill();
      return;
    }
    body.setLinearVelocity(0, 0);

    // accelerate
    Vector2 force = ic.direction.cpy().nor().scl(ic.speed);
    body.applyLinearImpulse(force, body.getWorldCenter(), true);

    // update entity
    pc.position.set(body.getPosition());
    vc.velocity.set(body.getLinearVelocity());
    rc.angle = MathUtils.radiansToDegrees * body.getAngle();
  }
示例#4
0
 @Override
 public boolean touchUp(int x, int y, int arg2, int arg3) {
   Vector3 coordinates = new Vector3(x, y, 0);
   camera.unproject(coordinates);
   if (lastTouchPolygon != null) {
     Vector2 impulse =
         new Vector2(coordinates.x, coordinates.y)
             .sub(lastTouchCoordinates)
             .scl(lastTouchPolygon.getMass());
     Log.log(
         "LiveMode.touchUp",
         "applying impulse: " + impulse + " on body: " + lastTouchPolygon.getPosition());
     lastTouchPolygon.applyLinearImpulse(
         impulse, lastTouchPolygonLocalCoordinates.add(lastTouchPolygon.getPosition()), true);
     lastTouchCoordinates = null;
     lastTouchPolygon = null;
     lastTouchPolygonLocalCoordinates = null;
   }
   return false;
 }
示例#5
0
文件: Mario.java 项目: B1z/SuperMario
 public void hit(Enemy enemy) {
   if (enemy instanceof Turtle && ((Turtle) enemy).currentState == Turtle.State.STANDING_SHELL)
     ((Turtle) enemy)
         .kick(
             enemy.b2body.getPosition().x > b2body.getPosition().x
                 ? Turtle.KICK_RIGHT
                 : Turtle.KICK_LEFT);
   else {
     if (marioIsBig) {
       marioIsBig = false;
       timeToRedefineMario = true;
       setBounds(getX(), getY(), getWidth(), getHeight() / 2);
       MarioBros.manager.get("audio/sounds/powerdown.wav", Sound.class).play();
     } else {
       MarioBros.manager.get("audio/music/mario_music.ogg", Music.class).stop();
       MarioBros.manager.get("audio/sounds/mariodie.wav", Sound.class).play();
       marioIsDead = true;
       Filter filter = new Filter();
       filter.maskBits = MarioBros.NOTHING_BIT;
       for (Fixture fixture : b2body.getFixtureList()) fixture.setFilterData(filter);
       b2body.applyLinearImpulse(new Vector2(0, 4f), b2body.getWorldCenter(), true);
     }
   }
 }
示例#6
0
 public void jumpBody(Body body, float height) {
   body.applyLinearImpulse(new Vector2(0, height), body.getWorldCenter(), true);
 }
 void playerJump(Nothing ignored) {
   if (isPlayerOnFloor) {
     playerBody.applyLinearImpulse(Constants.JUMP_IMPULSE, playerBody.getPosition(), true);
   }
 }
 void playerMoveLeft(Nothing ignored) {
   System.out.println(backEdge);
   if (playerBody.getLinearVelocity().x > -10 && playerBody.getPosition().x > backEdge + 3)
     playerBody.applyLinearImpulse(Constants.WALKING_FORCE_LEFT, new Vector2(0f, 0f), true);
 }
 void playerMoveRight(Nothing ignored) {
   if (playerBody.getLinearVelocity().x < 10)
     playerBody.applyLinearImpulse(Constants.WALKING_FORCE_RIGHT, new Vector2(0f, 0f), true);
 }
示例#10
0
文件: Mario.java 项目: B1z/SuperMario
 public void jump() {
   b2body.applyLinearImpulse(new Vector2(0, 4f), b2body.getWorldCenter(), true);
   currentState = State.JUMPING;
 }
  /**
   * 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;
        }
    }
  }