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) > (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 */