コード例 #1
0
 /** Convenience accessor */
 public Vector2f getOffset(Vector2f ret) {
   if (ret == null) {
     ret = new Vector2f(ox, oy);
   } else {
     ret.set(ox, oy);
   }
   return ret;
 }
コード例 #2
0
 /** Convenience accessor */
 public Vector2f getLocation(Vector2f ret) {
   if (ret == null) {
     ret = new Vector2f(x, y);
   } else {
     ret.set(x, y);
   }
   return ret;
 }
コード例 #3
0
ファイル: TouchPong.java プロジェクト: abom/multitouchj
  private boolean bounceBall(BounceableControl control) {
    /*
    log.debug("Cheking...");
    log.debug("\tcontrol: "+control.getPosition().toString()+", ball: "+ball.getPosition().toString());
    log.debug("\tvector: x="+ballUnitVector.getX()+", y="+ballUnitVector.getY());
    */

    boolean isHit = false;
    if ((control.isVisible()) && (control.isHit(ball.getPosition()))) {
      log.debug("bounced!!!!!!!!!!!!!!");

      float angleDelta = 90.0f - control.getRotation();

      // get current angle of ball vector
      Point previousBallPosition = new Point(0.0f, 0.0f);
      Point currentBallPosition = new Point(ballUnitVector.getX(), ballUnitVector.getY());
      // float ballVectorMagnitude = PointUtility.getDistance(previousBallPosition,
      // currentBallPosition);
      float currentBallAngle = PointUtility.getAngle(previousBallPosition, currentBallPosition);
      log.debug("\t\tcurrentBallAngle=" + currentBallAngle);

      // get new vector
      float targetBallAngle = (currentBallAngle + angleDelta);
      float newX = (float) Math.cos(Math.toRadians(targetBallAngle));
      float newY = (float) Math.sin(Math.toRadians(targetBallAngle));
      log.debug("\t\tnew vector: x=" + newX + ", y=" + newY);
      newX = (-1 * newX);

      // back to original angle
      previousBallPosition.set(0.0f, 0.0f);
      currentBallPosition.set(newX, newY);
      log.debug("\t\tnewX=" + newX + ", newY=" + newY);
      float newAngle = PointUtility.getAngle(previousBallPosition, currentBallPosition);
      targetBallAngle = (newAngle - angleDelta);
      log.debug("\t\tnew angle=" + newAngle + ", targetBallAngle=" + targetBallAngle);

      newX = (float) Math.cos(Math.toRadians(targetBallAngle));
      newY = (float) Math.sin(Math.toRadians(targetBallAngle));
      ballUnitVector.set(newX, newY);
      ballUnitVector = ballUnitVector.normalise(null);
      log.debug("\t\tfinal: x=" + ballUnitVector.getX() + ", y=" + ballUnitVector.getY());

      isHit = true;
    }

    return isHit;
  }
コード例 #4
0
ファイル: TouchPong.java プロジェクト: abom/multitouchj
  private void onGameStateStart() {
    wonControl.setVisible(false);
    lostControl.setVisible(false);

    Random random = new Random(new Date().getTime());
    float ballAngle = (float) random.nextInt(360);
    float ballX = (float) Math.cos(Math.toRadians(ballAngle));
    float ballY = (float) Math.sin(Math.toRadians(ballAngle));
    if (ballX == 0.0f) {
      ballX = 0.5f;
    }
    if (ballY == 0.0f) {
      ballY = 0.5f;
    }

    log.debug("angle: " + ballAngle + ", x: " + ballX + ", y: " + ballY);
    ballUnitVector.set(ballX, ballY);
    ballUnitVector = ballUnitVector.normalise(null);

    gameState = GameState.Playing;
  }
コード例 #5
0
ファイル: InputComponent.java プロジェクト: mttr/euclidean
  @Override
  public void update(BaseObject parent) {
    Vector2f acceleration = ((GameObject) parent).getAcceleration();
    acceleration.set(0, 0);

    if (sys.keyboard.isKeyPressed(ButtonActions.MOVE_UP)) {
      acceleration.y = ACCELERATION;
    }
    if (sys.keyboard.isKeyPressed(ButtonActions.MOVE_DOWN)) {
      acceleration.y = -ACCELERATION;
    }
    if (sys.keyboard.isKeyPressed(ButtonActions.MOVE_LEFT)) {
      acceleration.x = -ACCELERATION;
    }
    if (sys.keyboard.isKeyPressed(ButtonActions.MOVE_RIGHT)) {
      acceleration.x = ACCELERATION;
    }

    if (sys.currentTime - lastTime < fireRate) {
      return;
    }

    // FIXME I really shouldn't be using hardcoded values to figure out where to
    // shoot the bullets from.
    // FIXME I also REALLY REALLY need to get my vectors sorted out so that I don't have to
    // resort to this ugliness.
    bulletVelocity.set(
        ((GameObject) parent).getVelocity().x, ((GameObject) parent).getVelocity().x);
    bulletPosition.set(16f, 0f);

    if (sys.keyboard.isKeyPressed(ButtonActions.FIRE_LEFT)) {
      bulletVelocity.x -= BULLET_VELOCITY;
      bulletPosition.y = 16f;
      bulletPosition.x = 0f;
    }

    if (sys.keyboard.isKeyPressed(ButtonActions.FIRE_RIGHT)) {
      bulletVelocity.x += BULLET_VELOCITY;
      bulletPosition.y = 16f;
      bulletPosition.x = 32f;
    }

    if (sys.keyboard.isKeyPressed(ButtonActions.FIRE_UP)) {
      bulletVelocity.y += BULLET_VELOCITY;
      bulletPosition.y = 32f;
    }

    if (sys.keyboard.isKeyPressed(ButtonActions.FIRE_DOWN)) {
      bulletVelocity.y -= BULLET_VELOCITY;
      bulletPosition.y = 0f;
    }

    if (bulletVelocity.x != 0f || bulletVelocity.y != 0f) {
      Vector2f position = ((GameObject) parent).getPosition();
      bulletPosition.x += position.x;
      bulletPosition.y += position.y;

      GameObject bullet = bulletPool.obtain();

      if (bullet != null) {
        bullet.setPosition(bulletPosition.x, bulletPosition.y);

        bulletVelocity.rotate(bulletCounter * spreadDegree);

        bullet.setVelocity(bulletVelocity.x, bulletVelocity.y);

        sys.manager.add(bullet);

        if (bulletCounter == spreadCount / 2) {
          bulletCounter = -bulletCounter;
        } else {
          bulletCounter++;
        }
      }
    }
    lastTime = sys.currentTime;
  }