Guest User

Untitled

a guest
Dec 25th, 2010
316
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function triangleTest(rect, vertex0, vertex1, vertex2)
  2. {
  3.     /*
  4.     This function borrowed faithfully from a wonderfl (:3) discussion on
  5.     calculating triangle collision with AABBs on the following blog:
  6.     http://sebleedelisle.com/2009/05/super-fast-trianglerectangle-intersection-test/
  7.    
  8.     This particular optimization best suits my purposes and was contributed
  9.     to the discussion by someone from http://lab9.fr/
  10.     */
  11.    
  12.     var l = rect.x;
  13.     var r = rect.w;
  14.     var t = rect.y;
  15.     var b = rect.h;
  16.    
  17.     var x0 = vertex0.x;
  18.     var y0 = vertex0.y;
  19.     var x1 = vertex1.x;
  20.     var y1 = vertex1.y;
  21.     var x2 = vertex2.x;
  22.     var y2 = vertex2.y;
  23.    
  24.     var c, m, s;
  25.    
  26.     var b0 = ((x0 > l) ? 1 : 0) | (((y0 > t) ? 1 : 0) << 1) |
  27.         (((x0 > r) ? 1 : 0) << 2) | (((y0 > b) ? 1 : 0) << 3);
  28.     if (b0 == 3) return true;
  29.    
  30.     var b1 = ((x1 > l) ? 1 : 0) | (((y1 > t) ? 1 : 0) << 1) |
  31.         (((x1 > r) ? 1 : 0) << 2) | (((y1 > b) ? 1 : 0) << 3);
  32.     if (b1 == 3) return true;
  33.    
  34.     var b2 = ((x2 > l) ? 1 : 0) | (((y2 > t) ? 1 : 0) << 1) |
  35.         (((x2 > r) ? 1 : 0) << 2) | (((y2 > b) ? 1 : 0) << 3);
  36.     if (b2 == 3) return true;
  37.    
  38.     var i0 = b0 ^ b1;
  39.     if (i0 != 0)
  40.     {
  41.         m = (y1-y0) / (x1-x0);
  42.         c = y0 -(m * x0);
  43.         if (i0 & 1) { s = m * l + c; if ( s > t && s < b) return true; }
  44.         if (i0 & 2) { s = (t - c) / m; if ( s > l && s < r) return true; }
  45.         if (i0 & 4) { s = m * r + c; if ( s > t && s < b) return true; }
  46.         if (i0 & 8) { s = (b - c) / m; if ( s > l && s < r) return true; }
  47.     }
  48.    
  49.     var i1 = b1 ^ b2;
  50.     if (i1 != 0)
  51.     {
  52.         m = (y2-y1) / (x2-x1);
  53.         c = y1 -(m * x1);
  54.         if (i1 & 1) { s = m * l + c; if ( s > t && s < b) return true; }
  55.         if (i1 & 2) { s = (t - c) / m; if ( s > l && s < r) return true; }
  56.         if (i1 & 4) { s = m * r + c; if ( s > t && s < b) return true; }
  57.         if (i1 & 8) { s = (b - c) / m; if ( s > l && s < r) return true; }
  58.     }
  59.    
  60.     var i2 = b0 ^ b2;
  61.     if (i2 != 0)
  62.     {
  63.         m = (y2-y0) / (x2-x0);
  64.         c = y0 -(m * x0);
  65.         if (i2 & 1) { s = m * l + c; if ( s > t && s < b) return true; }
  66.         if (i2 & 2) { s = (t - c) / m; if ( s > l && s < r) return true; }
  67.         if (i2 & 4) { s = m * r + c; if ( s > t && s < b) return true; }
  68.         if (i2 & 8) { s = (b - c) / m; if ( s > l && s < r) return true; }
  69.     }
  70.    
  71.     return false;
  72. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×