Fixed basic collision mechanics

This commit is contained in:
Michael Chen 2018-01-15 16:02:21 +01:00
parent dcceccb571
commit cadfbdc0fa

View File

@ -140,8 +140,8 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
if (((obj->TargetRect).y) + ((obj->TargetRect).w) < (rect->y)) return false; if (((obj->TargetRect).y) + ((obj->TargetRect).w) < (rect->y)) return false;
if (((obj->TargetRect).y) > (rect->y) + (rect->h)) return false; if (((obj->TargetRect).y) > (rect->y) + (rect->h)) return false;
// Already returned with false if square ball hitbox didnt collide with rect // Already returned with false if square ball hitbox didnt collide with rect
Vector center = (Vector) {.x = ((obj->TargetRect).x) + (obj->Size), .y = ((obj->TargetRect).y) + (obj->Size) }; Vector center = (Vector) {.x = ballCenter.x, .y = ballCenter.y };
Vector corner = (Vector) {0, 0 }; Vector corner;
// Folgender Algorithmus ist gefickt, wenn der Ballmittelpunkt im rechteck liegt! // Folgender Algorithmus ist gefickt, wenn der Ballmittelpunkt im rechteck liegt!
double perpendicular, oldMomentum, angle; double perpendicular, oldMomentum, angle;
oldMomentum = fmod((double)(vectorRotation(obj->Momentum) + 180), 360.0f); oldMomentum = fmod((double)(vectorRotation(obj->Momentum) + 180), 360.0f);
@ -163,18 +163,20 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){
corner.x = (rect->x) + (rect->w); corner.x = (rect->x) + (rect->w);
else else
xMid = true; xMid = true;
if (!(yMid || xMid)) { // Ball collides with corner if (yMid)
perpendicular = vectorRotation(vectorSub(center, corner)); (obj->Momentum).x = -(obj->Momentum).x;
angle = fabs(perpendicular - oldMomentum); if (xMid)
if (oldMomentum < perpendicular) (obj->Momentum).y = -(obj->Momentum).y;
(obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), BALL_Speed); if (yMid || xMid) { // Ball collides with Edge
else } else { // Ball collides with corner
(obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), BALL_Speed); /*
} else { // Ball collides with Edge * perpendicular = vectorRotation(vectorSub(center, corner));
if (yMid) * angle = fabs(perpendicular - oldMomentum);
(obj->Momentum).y = -(obj->Momentum).y; * if (oldMomentum < perpendicular)
else * (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), BALL_Speed);
(obj->Momentum).x = -(obj->Momentum).x; * else
* (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), BALL_Speed);
*/
} }
return true; return true;
} /* BALL_CollideWithRect */ } /* BALL_CollideWithRect */