Guest

Untitled

By: a guest on Feb 23rd, 2009  |  syntax: Java  |  size: 2.59 KB  |  hits: 117  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. // Insertion sort for Sweep and Prune
  2.         public void insertionSort(Comparable[] a)
  3.         {
  4.                  for( int p = 1; p < ballCount; p++ )
  5.              {
  6.                  Comparable tmp = a[ p ];
  7.                  int j = p;
  8.  
  9.                  for( ; j > 0 && tmp.compareTo( a[ j - 1 ] ) < 0; j-- )
  10.                      a[ j ] = a[ j - 1 ];
  11.  
  12.                  a[ j ] = tmp;
  13.                  }
  14.         }
  15.  
  16.  
  17.         public void checkCollisions()
  18.         {
  19.                 insertionSort(balls);
  20.  
  21.  
  22.                 // Check for collision with walls
  23.                 for (int i = 0; i < ballCount; i++)
  24.                 {
  25.                 //      System.out.println("Ball #" + i + ": " + (balls[i].position.getX() - balls[i].getRadius()));
  26.  
  27.                         if (balls[i].position.getX() - balls[i].getRadius() < 0)
  28.                         {
  29.                                 balls[i].position.setX(balls[i].getRadius()); // Place ball against edge
  30.                                 balls[i].velocity.setX(-(balls[i].velocity.getX() * Constants.restitution)); // Reverse direction and account for friction
  31.                                 balls[i].velocity.setY(balls[i].velocity.getY() * Constants.restitution);
  32.                         }
  33.                         else if (balls[i].position.getX() + balls[i].getRadius() > getWidth()) // Right Wall
  34.                         {
  35.                                 balls[i].position.setX(getWidth() - balls[i].getRadius());              // Place ball against edge
  36.                                 balls[i].velocity.setX(-(balls[i].velocity.getX() * Constants.restitution)); // Reverse direction and account for friction
  37.                                 balls[i].velocity.setY((balls[i].velocity.getY() * Constants.restitution));
  38.                         }
  39.  
  40.                         if (balls[i].position.getY() - balls[i].getRadius() < 0)                                // Top Wall
  41.                         {
  42.                                 balls[i].position.setY(balls[i].getRadius());                           // Place ball against edge
  43.                                 balls[i].velocity.setY(-(balls[i].velocity.getY() * Constants.restitution)); // Reverse direction and account for friction
  44.                                 balls[i].velocity.setX((balls[i].velocity.getX() * Constants.restitution));
  45.                         }
  46.                         else if (balls[i].position.getY() + balls[i].getRadius() > getHeight()) // Bottom Wall
  47.                         {
  48.                                 balls[i].position.setY(getHeight() - balls[i].getRadius());             // Place ball against edge
  49.                                 balls[i].velocity.setY(-(balls[i].velocity.getY() * Constants.restitution));    // Reverse direction and account for friction
  50.                                 balls[i].velocity.setX((balls[i].velocity.getX() * Constants.restitution));
  51.                         }
  52.  
  53.                         // Ball to Ball collision
  54.                         for(int j = i + 1; j < ballCount; j++)
  55.                         {
  56.                                 if ((balls[i].position.getX() + balls[i].getRadius()) < (balls[j].position.getX() - balls[j].getRadius()))
  57.                                                 break;
  58.  
  59.                                 if((balls[i].position.getY() + balls[i].getRadius()) < (balls[j].position.getY() - balls[j].getRadius()) ||
  60.                                    (balls[j].position.getY() + balls[j].getRadius()) < (balls[i].position.getY() - balls[i].getRadius()))
  61.                                                 continue;
  62.  
  63.                                 balls[i].resolveCollision(balls[j]);
  64.  
  65.                         }
  66.                 }
  67.  
  68.         }