Пример #1
0
  /* -------------------------------------------------------------*/
  private void gravityEffect(ArrayList movingObjects) {
    // find effect of gravity on this objects from all other objects
    for (int i = 0; i < movingObjects.size(); i++) {
      // reset variables
      double add_vx = 0.0;
      double add_vy = 0.0;

      SpaceObject object = (SpaceObject) movingObjects.get(i);

      if (object.getObjCount() != objectNum
          && // ignore yourself (distance = 0!)
          !object.isBullet()
          && // ignore bullets
          !object.isSpaceShip()) // ignore spaceships
      {

        // find distance vector between planet and ball
        int x = pos_x - object.getXPos();
        int y = pos_y - object.getYPos();

        double distance = Math.sqrt(x * x + y * y);

        // find effect of planet on velocity
        double add_vec = (SpaceObject.G * k * object.getMass() * ballMass) / (distance * distance);

        add_vx = -(x / distance) * add_vec;
        add_vy = -(y / distance) * add_vec;

        // add objects speeds onto spaceship speed (clip speed if too large)
        x_speed += add_vx;
        y_speed += add_vy;
      }
    }
  }
Пример #2
0
  /* -------------------------------------------------------------*/
  private void objectCollision(ArrayList movingObjects) {
    for (int i = 0; i < movingObjects.size(); i++) {
      // make sure not testing if collided with yourself :P
      if (((SpaceObject) movingObjects.get(i)).getObjCount() != objectNum) {
        SpaceObject object = (SpaceObject) movingObjects.get(i);

        // find distance vector between two objects
        int x = pos_x - object.getXPos();
        int y = pos_y - object.getYPos();

        double distance = Math.sqrt(x * x + y * y);

        // has it collided with the object?
        if (distance < (radius + object.getRadius())) {
          // has it collided with a BULLET (or MISSILE)?
          if (object.isBullet()) {
            // do nothing
          }
          // is it another SPACESHIP? (INSTANT DEATH)
          else if (object.isSpaceShip()) {
            // do nothing
          }
          // collided with anything else (e.g PLANET): (INSTANT DEATH)
          else {
            collision.play();
            kill(movingObjects); // object has died
          }
        }
      }
    } // end for loop
  }