From: Matthew Owens Date: Sat, 3 Oct 2020 12:00:09 +0000 (+0100) Subject: added ball bouncing X-Git-Url: https://git.owens.tech///git?a=commitdiff_plain;h=8011e369413e7d7cd72d655e6f3db8422e3e176f;p=pong.git added ball bouncing --- diff --git a/src/main.c b/src/main.c index c0cb765..20cd0a6 100644 --- a/src/main.c +++ b/src/main.c @@ -31,30 +31,31 @@ static const Vec2di baseWindowSize = { 1920, 1080 }; static Vec2di windowSize; static Vec2df windowScale; -Vbox pad, pad2, ball; +Vbox ball; +Vbox pads[2]; Vec2df vline[2]; static void paddle_init() { // pad - pad.r.w = 192 * windowScale.x; - pad.r.h = 32 * windowScale.y; + pads[0].r.w = 192 * windowScale.x; + pads[0].r.h = 32 * windowScale.y; - pad.r.x = (windowSize.x / 2) - (pad.r.w / 2); - pad.r.y = windowSize.y - ((windowSize.y / 8)); + pads[0].r.x = (windowSize.x / 2) - (pads[0].r.w / 2); + pads[0].r.y = windowSize.y - ((windowSize.y / 8)); - pad.c.r = pad.c.g = pad.c.b = 0x55; - pad.c.a = 0xff; + pads[0].c.r = pads[0].c.g = pads[0].c.b = 0x55; + pads[0].c.a = 0xff; - // pad2 - pad2.r.w = 192 * windowScale.x; - pad2.r.h = 32 * windowScale.y; + // pads[1] + pads[1].r.w = 192 * windowScale.x; + pads[1].r.h = 32 * windowScale.y; - pad2.r.x = (windowSize.x / 2) - (pad2.r.w / 2); - pad2.r.y = windowSize.y / 8 - pad2.r.h; + pads[1].r.x = (windowSize.x / 2) - (pads[1].r.w / 2); + pads[1].r.y = windowSize.y / 8 - pads[1].r.h; - pad2.c.r = pad2.c.g = pad2.c.b = 0x55; - pad2.c.a = 0xff; + pads[1].c.r = pads[1].c.g = pads[1].c.b = 0x55; + pads[1].c.a = 0xff; } static void ball_init() @@ -81,24 +82,26 @@ static void vbox_render(const Vbox *b) static void paddle_update(const Uint8* keyStates, float dt) { - pad.v.x = 0; + pads[0].v.x = 0; + pads[1].v.x = 0; - if(keyStates[SDL_SCANCODE_LEFT] || keyStates[SDL_SCANCODE_A]) { - pad.v.x = -speed; - } - - if(keyStates[SDL_SCANCODE_RIGHT] || keyStates[SDL_SCANCODE_D]) { - pad.v.x = speed; - } + if(keyStates[SDL_SCANCODE_RIGHT]) { pads[0].v.x += speed; } + if(keyStates[SDL_SCANCODE_LEFT]) { pads[0].v.x -= speed; } + if(keyStates[SDL_SCANCODE_D]) { pads[1].v.x += speed; } + if(keyStates[SDL_SCANCODE_A]) { pads[1].v.x -= speed; } - pad.r.x += pad.v.x * dt; + for(int i = 0; i < 2; ++i){ + pads[i].r.x += pads[i].v.x * dt; - // screen bounds check - if(pad.r.x + pad.r.w > windowSize.x) { pad.r.x = windowSize.x - pad.r.w; } - if(pad.r.x < 0 ) { pad.r.x = 0; } + // screen bounds check + if(pads[i].r.x + pads[i].r.w > windowSize.x){ + pads[i].r.x = windowSize.x - pads[i].r.w; + } + if(pads[i].r.x < 0 ) { pads[i].r.x = 0; } + } } -static void ball_update(float dt) +static void ball_update(const Uint8 *keyStates, float dt) { bool collisionResponded = false; float halfw = ball.r.w / 2; @@ -117,29 +120,40 @@ static void ball_update(float dt) else if (ball.v.y < 0) {vline[1].y -= halfh; } // are we going to collide with the paddle? - { - int willCollide = lr_collision(vline[0], vline[1], pad.r, NULL); + int collidingWith = -1; + for(int i = 0; i < 2; ++i) { + int willCollide = lr_collision(vline[0], vline[1], pads[i].r, NULL); // left/right/top/bottom switch(willCollide) { - //TODO: set ball loc to colliding wall, invert vel & skip vel add case 1: - ball.r.x = pad.r.x - ball.r.w; + ball.r.x = pads[i].r.x - ball.r.w; ball.v.x *= -1; break; case 2: - ball.r.x = pad.r.x + pad.r.w; + ball.r.x = pads[i].r.x + pads[i].r.w; ball.v.x *= -1; break; case 3: - ball.r.y = pad.r.y - ball.r.h; + ball.r.y = pads[i].r.y - ball.r.h; ball.v.y *= -1; break; case 4: - ball.r.y = pad.r.y + pad.r.h; + ball.r.y = pads[i].r.y + pads[i].r.h; ball.v.y *= -1; break; } - if(willCollide != 0) { collisionResponded = true; } + if(willCollide != 0){ + collisionResponded = true; + collidingWith = i; + } + } + + if(collidingWith == 0) { + if(keyStates[SDL_SCANCODE_RIGHT]) { ball.v.x += speed; } + if(keyStates[SDL_SCANCODE_LEFT]) { ball.v.x -= speed; } + } else if (collidingWith == 1) { + if(keyStates[SDL_SCANCODE_D]) { ball.v.x += speed; } + if(keyStates[SDL_SCANCODE_A]) { ball.v.x -= speed; } } // applying velocity if needed @@ -221,7 +235,7 @@ static void update() while(ts_phys_required(nPhysUpdates)){ paddle_update(keyStates, ts_delta()); - ball_update(ts_delta()); + ball_update(keyStates, ts_delta()); nPhysUpdates++; } } @@ -230,8 +244,8 @@ static void render() { SDL_SetRenderDrawColor(renderer, bgClr.r, bgClr.g, bgClr.b, bgClr.a); SDL_RenderClear(renderer); - vbox_render(&pad); - vbox_render(&pad2); + vbox_render(&pads[0]); + vbox_render(&pads[1]); vbox_render(&ball); SDL_SetRenderDrawColor(renderer, 0x00, 0xff, 0x00, 0xff); SDL_RenderDrawLine(renderer, vline[0].x, vline[0].y, vline[1].x, vline[1].y);