Fixed basic collision mechanics
This commit is contained in:
parent
dcceccb571
commit
cadfbdc0fa
26
breakout.c
26
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) + ((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
|
|
||||||
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)
|
if (yMid)
|
||||||
(obj->Momentum).y = -(obj->Momentum).y;
|
|
||||||
else
|
|
||||||
(obj->Momentum).x = -(obj->Momentum).x;
|
(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;
|
return true;
|
||||||
} /* BALL_CollideWithRect */
|
} /* BALL_CollideWithRect */
|
||||||
|
Loading…
Reference in New Issue
Block a user