Advertisement
Guest User

Swept AABB response

a guest
Jan 11th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function resolveCollisionX(a, b, dX, dY) {
  2.   var sum = createAABB(b.x, b.y, b.width + a.width, b.height + a.height);
  3.  
  4.   var time = 1;
  5.  
  6.   if (dX > 0) {
  7.     if (a.x + dX > sum.x - sum.halfWidth) {
  8.       time = ((sum.x - sum.halfWidth) - a.x) / dX;
  9.      
  10.       if (a.y + dY * time > sum.y - sum.halfHeight && a.y + dY * time < sum.y + sum.halfHeight && time >= 0) {
  11.         return time;
  12.       }
  13.     }
  14.   } else if (dX < 0) {
  15.     if (a.x + dX < sum.x + sum.halfWidth) {
  16.       time = ((sum.x + sum.halfWidth) - a.x) / dX;
  17.      
  18.       if (a.y + dY * time > sum.y - sum.halfHeight && a.y + dY * time < sum.y + sum.halfHeight && time >= 0) {
  19.         return time;
  20.       }
  21.     }
  22.   }
  23.  
  24.   return 1;
  25. }
  26.  
  27. function resolveCollisionY(a, b, dX, dY) {
  28.   var sum = createAABB(b.x, b.y, b.width + a.width, b.height + a.height);
  29.  
  30.   var time = 1;
  31.  
  32.   if (dY > 0) {
  33.     if (a.y + dY > sum.y - sum.halfHeight) {
  34.       time = ((sum.y - sum.halfHeight) - a.y) / dY;
  35.      
  36.       if (a.x + dX * time > sum.x - sum.halfWidth && a.x + dX * time < sum.x + sum.halfWidth && time >= 0) {
  37.         return time;
  38.       }
  39.     }
  40.   } else if (dY < 0) {
  41.     if (a.y + dY < sum.y + sum.halfHeight) {
  42.       time = ((sum.y + sum.halfHeight) - a.y) / dY;
  43.      
  44.       if (a.x + dX * time > sum.x - sum.halfWidth && a.x + dX * time < sum.x + sum.halfWidth && time >= 0) {
  45.         return time;
  46.       }
  47.     }
  48.   }
  49.  
  50.   return 1;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement