Advertisement
Guest User

Untitled

a guest
May 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. float colliderCalculate(COLLIDER *col1, COLLIDER *col2)
  2. {
  3. //construct difference vector
  4. float posx = col1->pos->x - col2->pos->x;
  5. float posy = col1->pos->y - col2->pos->y;
  6. float velx = col1->vel->x - col2->vel->x;
  7. float vely = col1->vel->y - col2->vel->y;
  8.  
  9. float a = pow(velx,2) + pow(vely,2); //component a of quadratic formula
  10. float b = 2*posx*velx + 2*posy*vely; //component b of quadratic formula
  11. float c = pow(posx,2)+ pow(posy,2) - pow(col1->radius + col2->radius, 2); //component c of quadratic formula
  12. float d = pow(b,2) - 4*a*c; //discriminant of quadratic formula
  13.  
  14. if(d < 0) return -1; //no collision detected
  15.  
  16. float t = (-b - sqrt(d))/(2*a); //find first collision, ignore second
  17.  
  18. if(t >= 0 && t <= 1) return t; //collision occurs during this frame, return time collision occurs
  19. else return -1; //collision occurs either before or after frame, return -1 signalling no collision detected
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement