Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Insertion sort for Sweep and Prune
- public void insertionSort(Comparable[] a)
- {
- for( int p = 1; p < ballCount; p++ )
- {
- Comparable tmp = a[ p ];
- int j = p;
- for( ; j > 0 && tmp.compareTo( a[ j - 1 ] ) < 0; j-- )
- a[ j ] = a[ j - 1 ];
- a[ j ] = tmp;
- }
- }
- public void checkCollisions()
- {
- insertionSort(balls);
- // Check for collision with walls
- for (int i = 0; i < ballCount; i++)
- {
- // System.out.println("Ball #" + i + ": " + (balls[i].position.getX() - balls[i].getRadius()));
- if (balls[i].position.getX() - balls[i].getRadius() < 0)
- {
- balls[i].position.setX(balls[i].getRadius()); // Place ball against edge
- balls[i].velocity.setX(-(balls[i].velocity.getX() * Constants.restitution)); // Reverse direction and account for friction
- balls[i].velocity.setY(balls[i].velocity.getY() * Constants.restitution);
- }
- else if (balls[i].position.getX() + balls[i].getRadius() > getWidth()) // Right Wall
- {
- balls[i].position.setX(getWidth() - balls[i].getRadius()); // Place ball against edge
- balls[i].velocity.setX(-(balls[i].velocity.getX() * Constants.restitution)); // Reverse direction and account for friction
- balls[i].velocity.setY((balls[i].velocity.getY() * Constants.restitution));
- }
- if (balls[i].position.getY() - balls[i].getRadius() < 0) // Top Wall
- {
- balls[i].position.setY(balls[i].getRadius()); // Place ball against edge
- balls[i].velocity.setY(-(balls[i].velocity.getY() * Constants.restitution)); // Reverse direction and account for friction
- balls[i].velocity.setX((balls[i].velocity.getX() * Constants.restitution));
- }
- else if (balls[i].position.getY() + balls[i].getRadius() > getHeight()) // Bottom Wall
- {
- balls[i].position.setY(getHeight() - balls[i].getRadius()); // Place ball against edge
- balls[i].velocity.setY(-(balls[i].velocity.getY() * Constants.restitution)); // Reverse direction and account for friction
- balls[i].velocity.setX((balls[i].velocity.getX() * Constants.restitution));
- }
- // Ball to Ball collision
- for(int j = i + 1; j < ballCount; j++)
- {
- if ((balls[i].position.getX() + balls[i].getRadius()) < (balls[j].position.getX() - balls[j].getRadius()))
- break;
- if((balls[i].position.getY() + balls[i].getRadius()) < (balls[j].position.getY() - balls[j].getRadius()) ||
- (balls[j].position.getY() + balls[j].getRadius()) < (balls[i].position.getY() - balls[i].getRadius()))
- continue;
- balls[i].resolveCollision(balls[j]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement