diff --git a/bin/assets/images/blocks_debug.png b/bin/assets/images/blocks_debug.png index c52b126..0819b9d 100644 Binary files a/bin/assets/images/blocks_debug.png and b/bin/assets/images/blocks_debug.png differ diff --git a/breakout.c b/breakout.c index 605bf2d..7270655 100644 --- a/breakout.c +++ b/breakout.c @@ -66,8 +66,8 @@ void BREAKOUT_ChangeSize(int width, int height){ } void BREAKOUT_Update(Uint8 * keystate){ + PADDLE_Update(&paddle, keystate); // Update paddle before ball because paddle is not static! BALL_Update(&ball, &paddle); - PADDLE_Update(&paddle, keystate); for (size_t i = 0; i < BlockCount; i++) { BLOCK_Update(blocks + i); } @@ -134,7 +134,7 @@ void BALL_Draw(SDL_Renderer * renderer, Ball * obj){ } bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){ - SDL_Point ballCenter = (SDL_Point) {.x = ((obj->TargetRect).x) + (obj->Size), .y = ((obj->TargetRect).y) + (obj->Size) }; + SDL_Point ballCenter = BALL_GetCenter(obj); // THIS IS CURRENTLY A RECTANGLE COLLIDE if (((obj->TargetRect).x) + ((obj->TargetRect).w) < (rect->x)) { @@ -149,7 +149,7 @@ bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect){ if (((obj->TargetRect).y) > (rect->y) + (rect->h)) { return false; } - // Folgender Algorithmus ist gefickt, wenn der Ballmittelpunkt + // Folgender Algorithmus ist gefickt, wenn der Ballmittelpunkt im rechteck liegt! if ((ballCenter.x) < (rect->x) || (ballCenter.x) > (rect->x) + (rect->w)) { (obj->Momentum).x = -(obj->Momentum).x; } @@ -182,26 +182,42 @@ void BALL_SteerMomentum(Ball * obj, Paddle * paddle){ offset *= 60.0f; offset /= (double)(paddleHalfLen); printf("Offset = %.2f\n", offset); - (obj->Momentum) = getScaledDirectionalUnitVector(offset, BALL_Speed); + (obj->Momentum) = getDirectionalUnitVector(offset); +} + +void RECT_SetTargetPos(SDL_Rect * rect, Vector * Location){ + rect->x = (int)round(Location->x); + rect->y = (int)round(Location->y); +} + +SDL_Point BALL_GetCenter(Ball * obj){ + return (SDL_Point) {.x = ((obj->TargetRect).x) + (obj->Size), .y = ((obj->TargetRect).y) + (obj->Size) }; } void BALL_Update(Ball * obj, Paddle * paddle){ Vector oldMomentum = obj->Momentum; + Vector oldLocation = obj->Location; + SDL_Point ballCenter = BALL_GetCenter(obj); - (obj->Rotation) += (obj->RotationValue); + (obj->Rotation) += (obj->RotationValue); // No effect on physics (obj->Location) = vectorAdd((obj->Location), oldMomentum); if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) { - (obj->Location) = vectorSub((obj->Location), oldMomentum); // Maybe remove this - BALL_SteerMomentum(obj, paddle); - (obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); // Maybe remove this + (obj->Location) = oldLocation; // Maybe remove this + BALL_SteerMomentum(obj, paddle); // Sets it to unit vector! + // Following assumes that the paddle position was udated before the ball was updated +// BUG/GLITCH: Make sure that the postition of the ball is not shited into the borders of the game! + while (RECT_Collide(&(obj->TargetRect), &(paddle->TargetRect))) { // Move away from rect in small steps + (obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); + RECT_SetTargetPos(&(obj->TargetRect), &(obj->Location)); + } + (obj->Momentum) = vectorScaleTo((obj->Momentum), BALL_Speed); } - oldMomentum = obj->Momentum; for (size_t i = 0; i < BlockCount; i++) { + oldMomentum = obj->Momentum; BALL_CollideWithRect(obj, &(blocks[i].TargetRect)); (obj->Location) = vectorSub((obj->Location), oldMomentum); // Maybe remove this (obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); - oldMomentum = obj->Momentum; } if ((obj->Location).y > BREAKOUT_BoxHeight) diff --git a/breakout.h b/breakout.h index 9148d7b..0ea3f74 100644 --- a/breakout.h +++ b/breakout.h @@ -40,6 +40,8 @@ void BALL_Draw(SDL_Renderer * renderer, Ball * obj); bool BALL_CollideWithRect(Ball * obj, SDL_Rect * rect); bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2); void BALL_SteerMomentum(Ball * obj, Paddle * paddle); +void RECT_SetTargetPos(SDL_Rect * rect, Vector * Location); +SDL_Point BALL_GetCenter(Ball * obj); void BALL_Update(Ball * obj, Paddle * paddle); void BALL_DestroyObject(Ball * obj); void BALL_Deinitialize();