SHARE
TWEET

New Collision Function

a guest Sep 30th, 2012 40 Never
  1. function rect_collision(x1, y1, w1, h1, x2, y2, h2, w2) {
  2.     var top1 = y1;
  3.     var bottom1 = y1 + h1;
  4.     var left1 = x1;
  5.     var right1 = x1 + w1;
  6.     var top2 = y2;
  7.     var bottom2 = y2 + h2;
  8.     var left2 = x2;
  9.     var right2 = x2 + w2;
  10.     //console.log('----' + top1, bottom1, left1, right1, top2, bottom2, left2, right2 + '----');
  11.  
  12.     //console.log('top 1: ' + top1);
  13.     //console.log('bottom 1: ' + bottom1);
  14.     //console.log('top 2: ' + top2);
  15.     //console.log('bottom 2: ' + bottom2);
  16.     //console.log('x1: ' + x1);
  17.     //console.log('x2: ' + x2);
  18.     //console.log('y2: ' + y2);
  19.     //console.log('h2: ' + h2);
  20.     //console.log(x1, y1, w1, h1, x2, y2, h2, w2);
  21.     //console.log('------------------------------------------------')
  22.         if !(x1 + w1 < x2 ||
  23.            x2 + w2 < x1 ||
  24.            y1 + h1 < y2 ||
  25.            y2 + h2 < y1) return true;  
  26.         }
  27.    
  28.     return false;    
  29. }
RAW Paste Data
Top