SHARE
TWEET

Dart collision

a guest Oct 15th, 2013 181 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Collision {
  2.   static CanvasElement canvas = new CanvasElement();
  3.  
  4.   static void init() {
  5.     query('body').append(canvas);
  6.   }
  7.  
  8.   static bool collides(BoundingBox a, BoundingBox b) {
  9.     List<Vector2> A = a.getPoints();
  10.     List<Vector2> B = b.getPoints();
  11.     Vector2 Axis1 = new Vector2(A[1].x-A[0].x,A[1].y-A[0].y);
  12.     Vector2 Axis2 = new Vector2(A[1].x-A[2].x,A[1].y-A[2].y);
  13.     Vector2 Axis3 = new Vector2(B[0].x-B[3].x,B[0].y-B[3].y);
  14.     Vector2 Axis4 = new Vector2(B[0].x-B[1].x,B[0].y-B[1].y);
  15.    
  16.     return _projectionOverlap(Axis1,A,B) &&
  17.         _projectionOverlap(Axis2,A,B) &&
  18.         _projectionOverlap(Axis3,A,B) &&
  19.         _projectionOverlap(Axis4,A,B);
  20.   }
  21.  
  22.   static bool _projectionOverlap (Vector2 v,List<Vector2> A, List<Vector2> B) {
  23.     List aVal = _projectPoints(v,A);
  24.     double aMin = aVal[0];
  25.     double aMax = aVal[1];
  26.    
  27.     List bVal = _projectPoints(v,B);
  28.     double bMin = bVal[0];
  29.     double bMax = bVal[1];
  30.    
  31.     if (aMin <= bMin&& bMin <= aMax)
  32.       return true;
  33.     if (aMin <= bMax&& bMax <= aMax)
  34.       return true;
  35.    
  36.     return false;
  37.   }
  38.  
  39.   static List _projectPoints(Vector2 v, List<Vector2> A) {
  40.     double aMin;
  41.     double aMax;
  42.     A.forEach((Vector2 p) {
  43.       num proj = (p.x*v.x+p.y*v.y)/(Math.pow(p.x,2)+Math.pow(p.y,2));
  44.       num x = proj*v.x;
  45.       num y = proj*v.y;
  46.      
  47.       num val = (x*v.x)+(y*v.y);
  48.      
  49.       if (aMin == null) {
  50.         aMin = val;
  51.         aMax = val;
  52.       } else {
  53.         aMin = Math.min(val, aMin);
  54.         aMax = Math.max(val, aMax);
  55.       }
  56.      
  57.     });
  58.    
  59.     return [aMin,aMax];
  60.   }
  61. }
  62.  
  63. class BoundingBox {
  64.   CollidableEntity parent;
  65.  
  66.   int offsetX;
  67.   int offsetY;
  68.   int width;
  69.   int height;
  70.   double rotation;
  71.   BoundingBox (this.parent,this.offsetX,this.offsetY,this.width,this.height,[this.rotation=0.0]);
  72.  
  73.   List<Vector2> getPoints() {
  74.     Matrix2 translation = new Matrix2.rotation(rotation);
  75.    
  76.     int x = offsetX+parent.x;
  77.     int y = offsetY+parent.y;
  78.     Vector2 move = new Vector2(x.toDouble(),y.toDouble());
  79.    
  80.     Vector2 topLeft = translation.transform(new Vector2(-(width/2),-(height/2)))+move;
  81.     Vector2 topRight = translation.transform(new Vector2((width/2),-(height/2)))+move;
  82.     Vector2 bottomLeft = translation.transform(new Vector2(-(width/2),(height/2)))+move;
  83.     Vector2 bottomRight = translation.transform(new Vector2((width/2),(height/2)))+move;
  84.    
  85.     return [topLeft,topRight,bottomRight,bottomLeft];
  86.   }
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top