Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Dart Implementation for detecting collision of two rotated rectangles
- class Collision {
- static bool collides(CollisionBox a, CollisionBox b) {
- //The getpoints function will return the coordinates of the box in this order:
- //0:topleft 1:topright 2:bottomright 3: bottomleft
- List<Vector2> A = a.getPoints();
- List<Vector2> B = b.getPoints();
- Vector2 Axis1 = new Vector2(A[1].x-A[0].x,A[1].y-A[0].y);
- Vector2 Axis2 = new Vector2(A[1].x-A[2].x,A[1].y-A[2].y);
- Vector2 Axis3 = new Vector2(B[0].x-B[3].x,B[0].y-B[3].y);
- Vector2 Axis4 = new Vector2(B[0].x-B[1].x,B[0].y-B[1].y);
- return _projectionOverlap(Axis1,A,B) &&
- _projectionOverlap(Axis2,A,B) &&
- _projectionOverlap(Axis3,A,B) &&
- _projectionOverlap(Axis4,A,B);
- }
- static bool _projectionOverlap (Vector2 v,List<Vector2> A, List<Vector2> B) {
- List aVal = _projectPoints(v,A);
- double aMin = aVal[0];
- double aMax = aVal[1];
- List bVal = _projectPoints(v,B);
- double bMin = bVal[0];
- double bMax = bVal[1];
- if (aMax < bMin || bMax < aMin)
- return false;
- else
- return true;
- }
- static List _projectPoints(Vector2 v, List<Vector2> A) {
- double aMin;
- double aMax;
- A.forEach((Vector2 p) {
- num val = (p.x*v.x)+(p.y*v.y); //dot product of p & v
- if (aMin == null) {
- aMin = val;
- aMax = val;
- } else {
- aMin = Math.min(val, aMin);
- aMax = Math.max(val, aMax);
- }
- });
- return [aMin,aMax];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement