From bb164e51d8e763faa58d00627e8ba7e9fd6e3317 Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Thu, 11 Jan 2018 19:03:12 +0100 Subject: [PATCH] Paddle collision works --- breakout.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/breakout.c b/breakout.c index 09a6619..d2bf4cf 100644 --- a/breakout.c +++ b/breakout.c @@ -22,7 +22,7 @@ Paddle paddle; SDL_Texture * PADDLE_Texture; SDL_Rect * PADDLE_SourceRects; Uint8 * PADDLE_MoveLeftKeys, * PADDLE_MoveRightKeys; -double BALL_Speed = 10.0f; +double BALL_Speed = 15.0f; int PADDLE_Speed = 10; bool BREAKOUT_IsInit = false; bool BALL_IsInit = false; @@ -86,7 +86,7 @@ Ball BALL_CreateDefault(){ return (Ball) { .Location = (Vector) {.x = BREAKOUT_BoxWidth / 2, .y = BREAKOUT_BoxHeight / 2 }, - .Momentum = (Vector) {.x = 0.0f, .y = 6.0f }, + .Momentum = (Vector) {.x = 0.0f, .y = BALL_Speed }, .TargetRect = (SDL_Rect) {.x = 0, .y = 0, .w = 50, .h = 50 }, .Size = 25.0f, .Rotation = rotation, @@ -142,16 +142,23 @@ bool RECT_Collide(SDL_Rect * rect1, SDL_Rect * rect2){ return true; } -void BALL_Update(Ball * obj, Paddle * paddle){ - Vector lastMomentum = (obj->Momentum); - Vector lastLocation = (obj->Location); +void BALL_SteerMomentum(Ball * obj, Paddle * paddle){ + int paddleHalfLen = ((paddle->TargetRect).w / 2.0f); + double offset = (((obj->TargetRect).x) + (obj->Size)) - ((paddle->TargetRect).x + paddleHalfLen); + offset *= 60.0f; + offset /= (double)(paddleHalfLen); + printf("Offset = %.2f\n", offset); + (obj->Momentum) = getScaledDirectionalUnitVector(offset, BALL_Speed); +} + +void BALL_Update(Ball * obj, Paddle * paddle){ (obj->Rotation) += (obj->RotationValue); - (obj->Location) = vectorAdd((obj->Location), lastMomentum); + (obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); if (BALL_CollideWithRect(obj, &(paddle->TargetRect))) { - (obj->Location) = vectorSub((obj->Location), lastMomentum); // Maybe remove this - // BALL_SteerMomentum(obj, &(paddle->TargetRect)); + (obj->Location) = vectorSub((obj->Location), (obj->Momentum)); // Maybe remove this + BALL_SteerMomentum(obj, paddle); (obj->Location) = vectorAdd((obj->Location), (obj->Momentum)); // Maybe remove this } @@ -211,7 +218,9 @@ Paddle PADDLE_CreateDefault(){ void PADDLE_Draw(SDL_Renderer * renderer, Paddle * obj){ // printf("Paddle drawn at (%d|%d)!\n", (obj->TargetRect).x, (obj->TargetRect).x); - SDL_RenderCopy(renderer, PADDLE_Texture, PADDLE_SourceRects + (obj->TextureIndex), &(obj->TargetRect)); + // SDL_RenderCopy(renderer, PADDLE_Texture, PADDLE_SourceRects + (obj->TextureIndex), &(obj->TargetRect)); + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderDrawRect(renderer, &(obj->TargetRect)); } bool KeyPressed(Uint8 * keystate, Uint8 * keyArray){