Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float colliderCalculate(COLLIDER *col1, COLLIDER *col2)
- {
- //construct difference vector
- float posx = col1->pos->x - col2->pos->x;
- float posy = col1->pos->y - col2->pos->y;
- float velx = col1->vel->x - col2->vel->x;
- float vely = col1->vel->y - col2->vel->y;
- float a = pow(velx,2) + pow(vely,2); //component a of quadratic formula
- float b = 2*posx*velx + 2*posy*vely; //component b of quadratic formula
- float c = pow(posx,2)+ pow(posy,2) - pow(col1->radius + col2->radius, 2); //component c of quadratic formula
- float d = pow(b,2) - 4*a*c; //discriminant of quadratic formula
- if(d < 0) return -1; //no collision detected
- float t = (-b - sqrt(d))/(2*a); //find first collision, ignore second
- if(t >= 0 && t <= 1) return t; //collision occurs during this frame, return time collision occurs
- else return -1; //collision occurs either before or after frame, return -1 signalling no collision detected
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement