Exemplo n.º 1
0
 @Override
 public void run() {
   for (Ball e : balls) {
     e.update();
   }
   repaint();
 }
  @Override
  public void update(float pDeltaTime) {
    mGame.getInput().getMouseEvents();
    mGame.getInput().getKeyEvents();

    int len = mBalls.size();
    for (int i = 0; i < len; i++) {
      Ball firstBall = mBalls.get(i);

      for (int j = 0; j < len; j++) {
        Ball secondBall = mBalls.get(i);
        if (secondBall == firstBall
            || !OverlapTester.overlapCircles(firstBall.bounds, secondBall.bounds)) {
          continue;
        }
        System.out.println("overlap");
        float newVelocityX1 =
            (firstBall.velocity.x * (firstBall.mass - secondBall.mass)
                    + (2 * secondBall.velocity.x * secondBall.mass))
                / (firstBall.mass + secondBall.mass);
        float newVelocityY1 =
            (firstBall.velocity.y * (firstBall.mass - secondBall.mass)
                    + (2 * secondBall.velocity.y * secondBall.mass))
                / (firstBall.mass + secondBall.mass);
        float newVelocityX2 =
            (secondBall.velocity.x * (secondBall.mass - firstBall.mass)
                    + (2 * firstBall.velocity.x * firstBall.mass))
                / (firstBall.mass + secondBall.mass);
        float newVelocityY2 =
            (secondBall.velocity.y * (secondBall.mass - firstBall.mass)
                    + (2 * firstBall.velocity.y * firstBall.mass))
                / (firstBall.mass + secondBall.mass);

        firstBall.velocity.add(newVelocityX1, newVelocityY1);
        secondBall.velocity.add(newVelocityX2, newVelocityY2);
      }

      // firstBall.velocity.add(mGravity.x * pDeltaTime, mGravity.y * pDeltaTime);

      firstBall.update(pDeltaTime);
      if (firstBall.getX() < firstBall.getWidth() / 2) {
        firstBall.setX(firstBall.getWidth() / 2);
        firstBall.velocity.x *= -1;
      }
      if (firstBall.getX() > mWidth - firstBall.getWidth() / 2) {
        firstBall.setX(mWidth - firstBall.getWidth() / 2);
        firstBall.velocity.x *= -1;
      }
      if (firstBall.getY() < firstBall.getWidth() / 2) {
        firstBall.setY(firstBall.getWidth() / 2);
        firstBall.velocity.y *= -1;
      }
      if (firstBall.getY() > mHeight - firstBall.getHeight() / 2) {
        firstBall.setY(mHeight - firstBall.getHeight() / 2);
        firstBall.velocity.y *= -1;
      }
    }
  }
Exemplo n.º 3
0
 private void logic(int delta) {
   ball.update(delta);
   bat.update(delta);
   if (ball.getX() <= bat.getX() + bat.getWidth()
       && ball.getX() >= bat.getX()
       && ball.getY() >= bat.getY()
       && ball.getY() <= bat.getY() + bat.getHeight()) {
     ball.setDX(0.3);
   }
 }
Exemplo n.º 4
0
  /** Update all of the Ball's and draw them */
  public void update() {

    if (balls.size() != 0) {

      for (int i = 0; i < balls.size(); i++) {
        Ball b = (Ball) balls.get(i);
        b.update();
        b.attract = kelly;
        b.drawBall();
      }
    }
  }
Exemplo n.º 5
0
  public void update(float mouseX, float mouseY) {
    Vector d = new Vector(0, 0);
    for (int i = balls.size() - 1; i >= 0; i--) {
      Ball ball = balls.get(i);
      d.x = mouseX - ball.pos.x;
      d.y = mouseY - ball.pos.y;
      if (d.magSquared() < 100 * 100) {
        Vector.sub(ball.pos, d, ball.goal);
      } else {
        ball.goal.set(ball.startPos);
      }

      ball.update();
    }
  }
Exemplo n.º 6
0
  public void draw() {
    background(0);
    fill(0, 255, 0);
    stroke(255);
    // get kinect color image
    context.update();
    // scale to an arbitrary size and position (e.g. scale down 75%, and align to bottom / center)
    skeleton.updateSkeleton();

    if (onePlayer) {
      paddles[0].update(skeleton.getScreenCoords(1, paddleSelector1).y);
      paddles[1].update(skeleton.getScreenCoords(1, paddleSelector2).y);
    } else {
      paddles[0].update(skeleton.getScreenCoords(1, paddleSelector1).y);
      try {
        paddles[1].update(skeleton.getScreenCoords(2, paddleSelector2).y);
      } catch (Exception e) {
      }
    }

    for (int i = 0; i < paddles.length; i++) {

      paddles[i].drawPaddle();
    }
    ball.update();
    ball.checkCollision(paddles);
    ball.drawBall();

    /*PVector rHand = skeleton.getScreenCoords(1, SimpleOpenNI.SKEL_RIGHT_HAND) ;
    PVector lHand = skeleton.getScreenCoords(1, SimpleOpenNI.SKEL_LEFT_HAND) ;
    fill(255, 0, 0);
    ellipse(rHand.x, rHand.y, 20, 20);
    ellipse(lHand.x, lHand.y, 20, 20);*/
    float offset = 80;
    text(
        str(paddles[0].points),
        width - (offset + (0.5f * textWidth(str(paddles[0].points)))),
        height - 50);
    text(str(paddles[1].points), offset, height - 50);
    fill(0, 255, 0, 255 - ((255 / (timer + 1)) * (timer + 1)));

    if (timer > 0) {
      String txt = "POINT";
      text(txt, (width / 2) - (0.5f * textWidth(txt)), height / 2);
      timer--;
    }
  }