Esempio n. 1
0
    // Handles all ball-ball collisions
    void handleBallBallCollisions() {

      for (int i = 0; i < _scene.numChildren(); i++)
        for (int j = 0; j < _scene.numChildren(); j++) {

          if (i == j) continue;

          Object3d b1 = _scene.getChildAt(i);
          Object3d b2 = _scene.getChildAt(j);

          // if (b1 == _room || b2 == _room)
          // continue;

          if (testBallBallCollision(b1, b2)) {
            // Make the balls reflect off of each other

            Number3d displacement = Number3d.subtract(b1.position(), b2.position());
            displacement.normalize();
            float objDot = Number3d.dot(b1.velocity(), displacement);
            displacement.multiply(objDot);
            displacement.multiply(2f);
            b1.velocity().subtract(displacement);

            displacement = Number3d.subtract(b1.position(), b2.position());
            displacement.normalize();
            objDot = Number3d.dot(b2.velocity(), displacement);
            displacement.multiply(objDot);
            displacement.multiply(2f);
            b2.velocity().subtract(displacement);
          }
        }
    }
Esempio n. 2
0
    void checkWallCollision(Object3d obj, Wall wall, double elapsed) {
      if (testBallWallCollision(obj, wall)) {

        // Make the ball reflect off of the wall
        Number3d dir = wallDirection(wall);
        dir.normalize();

        float objDot = Number3d.dot(obj.velocity(), dir);
        dir.multiply(objDot);
        dir.multiply(2f);
        obj.velocity().subtract(dir);

        Object3d.applyImpactCORSolid(obj);
      }
    }