From cadfbdc0fa9ee24cd38a0ebe583059648a0f1e09 Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Mon, 15 Jan 2018 16:02:21 +0100 Subject: [PATCH] Fixed basic collision mechanics --- breakout.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/breakout.c b/breakout.c index 6cb2e71..a475746 100644 --- a/breakout.c +++ b/breakout.c @@ -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) > (rect->y) + (rect->h)) return false; // 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 corner = (Vector) {0, 0 }; + Vector center = (Vector) {.x = ballCenter.x, .y = ballCenter.y }; + Vector corner; // Folgender Algorithmus ist gefickt, wenn der Ballmittelpunkt im rechteck liegt! double perpendicular, oldMomentum, angle; 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); else xMid = true; - if (!(yMid || xMid)) { // Ball collides with corner - perpendicular = vectorRotation(vectorSub(center, corner)); - angle = fabs(perpendicular - oldMomentum); - if (oldMomentum < perpendicular) - (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), BALL_Speed); - else - (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), BALL_Speed); - } else { // Ball collides with Edge - if (yMid) - (obj->Momentum).y = -(obj->Momentum).y; - else - (obj->Momentum).x = -(obj->Momentum).x; + if (yMid) + (obj->Momentum).x = -(obj->Momentum).x; + if (xMid) + (obj->Momentum).y = -(obj->Momentum).y; + if (yMid || xMid) { // Ball collides with Edge + } else { // Ball collides with corner + /* + * perpendicular = vectorRotation(vectorSub(center, corner)); + * angle = fabs(perpendicular - oldMomentum); + * if (oldMomentum < perpendicular) + * (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum + (2 * angle)), BALL_Speed); + * else + * (obj->Momentum) = getScaledDirectionalUnitVector((oldMomentum - (2 * angle)), BALL_Speed); + */ } return true; } /* BALL_CollideWithRect */