digrach

checkForCollision

Apr 3rd, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1.     public boolean checkForColision(Particle p) {
  2.         // Field to scan is the Particle's current y
  3.         // minus particleSize and plus particleSize.
  4.  
  5.         int startRowIndex = (int) (p.getPosy() - p.getHeight() - 1);
  6.         if (startRowIndex < 0) {
  7.             startRowIndex = 0;
  8.         }
  9.         int endRowIndex = (int) (p.getPosy() + p.getHeight() - 1);
  10.         if (endRowIndex > checkList.length - 2) {
  11.             endRowIndex = checkList.length - 2;
  12.         }
  13.  
  14.         for (int x = startRowIndex; x <= endRowIndex; x++) {
  15.  
  16.             // Get current list and check if it's not null.
  17.             List<Particle> l = checkList[x];
  18.             if (l != null) {
  19.  
  20.                 // Loop through each Particle in the list.
  21.                 for (int i = 0; i < l.size(); i++) {
  22.                     // Get current Particle in the list.
  23.                     Particle cp = l.get(i);
  24.                     // If the current Particle in the list is the one
  25.                     // we are checking against (self), return.
  26.                     if (cp == p) {
  27.                         return false;
  28.                     }
  29.  
  30.                     if ((cp.getPosx() + cp.getWidth()) >= (p.getPosx())
  31.                             && (cp.getPosx()) <= (p.getPosx() + p.getWidth())) {
  32.  
  33.                         double collisionSize = 20;
  34.                        
  35.                         p.setWidth(collisionSize);
  36.                         p.setHeight(collisionSize);
  37.                         cp.setWidth(collisionSize);
  38.                         cp.setHeight(collisionSize);
  39.                         System.out
  40.                                 .println("***************************************************COLLIDE********");
  41.                         return true;
  42.                     }
  43.                 }
  44.  
  45.             }
  46.  
  47.         }
  48.         // Nothing found, return false.
  49.         System.out.println("*****************NO COLLISION");
  50.  
  51.         return false;
  52.     }
Advertisement
Add Comment
Please, Sign In to add comment