From 625b70e0d749d9af03ac7356696d3179d2faf7d0 Mon Sep 17 00:00:00 2001 From: FelipeFTN Date: Mon, 24 Apr 2023 18:38:08 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=B1=20polishing=20balls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/ball_medium.png | Bin 0 -> 671 bytes assets/ball_medium.xcf | Bin 0 -> 745 bytes src/ball/ball.cpp | 34 +++++++++++++++++++--------------- src/ball/ball.h | 16 +++++++++------- src/ball/getter.cpp | 12 ++++++++++++ src/ball/setter.cpp | 4 ++++ src/main.cpp | 10 +++++----- 7 files changed, 49 insertions(+), 27 deletions(-) create mode 100644 assets/ball_medium.png create mode 100644 assets/ball_medium.xcf diff --git a/assets/ball_medium.png b/assets/ball_medium.png new file mode 100644 index 0000000000000000000000000000000000000000..5f414b70337d43b2543f174f25104071876ce5b9 GIT binary patch literal 671 zcmV;Q0$}}#P)EX>4Tx04R}tkv&MmKpe$i(@I4u4hAXWkfG{g7Zq`=RVYG*P%E_RU_SZkM+H^bh|{W*Vj)BONgw~P>leu-ldA%S z91EyHgXH?b{@{1FR&i?5ONu0co)^dY7zM(+K&#<6-^Y&AIst;uz?I(d*P6iWC+Urj z7CQn4wteSad^gZEa<4bO1wgWnpw> zWFU8GbZ8()Nlj2!fese{004JML_t(o!|m6*5rj|-L(yyV3ASd1ZkplqHh~!`Fp1QF zj+HsMQ&kgi(36xj0J5qQK+@S%&QlH$_w@lRz}93~CG7xFRf7+cd>jJ|Fu(u<3^2d| z0}L?0fCLsv0j!O^GcW-eukSd3t8;ltBVXme&gq{I`40oK9nG1*C+GkG002ovPDHLk FV1g`W7GnSa literal 0 HcmV?d00001 diff --git a/assets/ball_medium.xcf b/assets/ball_medium.xcf new file mode 100644 index 0000000000000000000000000000000000000000..bcf7a2379bc290144472c56843c4e0ea208fc272 GIT binary patch literal 745 zcmZva%SyvQ6ozM#wrXqfE(BbRH)t1u<_Q$L5Ep{4kfupGl1oXVwkut@bMFhd_3hjV z?f9QGfhqWh|D5m4IhiCwLK#oR%nQWxz;PHPDieRgdobq+2k_<)OJwK}Zd5+5GR+;q zKX5n1*0z_%v7Z#E88!EZneP_QAE>iij>^~#eLKwLnAT|eyO$U9$QOAU z$#K8k3qmiA(oFQqWM|>X6@&6}`e={-X*;64s6ET4t|yaFI0IDkwW=!Yr$iP)Cf>x) z>5eoAa=#Geyv8eBAEtNK#2>YyMgbtxw>59vC_-ii1*iqnc`Epn`05Q2XAaXk-LzFP z5?@|L9*O-z~BYSCa#bWH<&ZYhrccF8-I G0= 110.f && GetY() <= screenHeight - 125.f) { canCollide = false; } else { canCollide = true; } - if(GetX() >= screenWidth - 30 && canCollide) { + if(GetX() >= screenWidth - GetWidth() - GetWidth()/2.f && canCollide) { SetSpeed(Vector2{GetSpeed().x - blockSpeed * GetSpeed().x, GetSpeed().y}); - } - if(GetY() >= screenHeight - 30 && canCollide) { + } // change to Width if u have any error + if(GetY() >= screenHeight - GetHeight() - GetHeight()/2.f && canCollide) { SetSpeed(Vector2{GetSpeed().x, GetSpeed().y - blockSpeed * GetSpeed().y}); } if(GetY() <= 15 && canCollide) { @@ -67,4 +69,6 @@ void Ball::Collision() { if(ballSpeed.x > 0.f && IsKeyUp(KEY_A) && IsKeyUp(KEY_D)) { ballSpeed.x -= 0.01f * ballSpeed.x; } if(ballSpeed.y > 0.f && IsKeyUp(KEY_W) && IsKeyUp(KEY_S)) { ballSpeed.y -= 0.01f * ballSpeed.y; } + + // Collisons again ball } diff --git a/src/ball/ball.h b/src/ball/ball.h index 5cb6511..0a4701d 100644 --- a/src/ball/ball.h +++ b/src/ball/ball.h @@ -7,29 +7,31 @@ class Ball { public: Ball(int sWidth, int sHeight, int x, int y, int id); void Draw(); - void Move(); - void Collision(); - + void Move(float deltaTime); + void Collision(Ball ball); Vector2 GetInitialPosition(); + Rectangle GetCollisionRec(); + float GetWidth(); + float GetHeight(); float GetX(); float GetY(); Vector2 GetSpeed(); void SetSpeed(Vector2 speed); void SetPosition(Vector2 position); + void SetAcceleration(float acceleration); private: - Texture2D ball = LoadTexture("assets/ball.png"); + Texture2D ball = LoadTexture("assets/ball_medium.png"); int id = 0; int screenWidth = 512; int screenHeight = 512; bool canCollide = true; - int ballWidth = ball.width; - int ballHeight = ball.height; Vector2 ballInitialPos { 50, 50 }; Vector2 ballSpeed { 0.f, 0.f }; + float ballAcceleration = 10.0f; Vector2 ballPos { 50, 50 }; - Rectangle ballRec { 0, 0, (float)ballWidth, (float)ballHeight }; + Rectangle ballRec { 0, 0, GetWidth(), GetHeight() }; }; #endif diff --git a/src/ball/getter.cpp b/src/ball/getter.cpp index 853eab9..4880d81 100644 --- a/src/ball/getter.cpp +++ b/src/ball/getter.cpp @@ -8,6 +8,14 @@ float Ball::GetY() { return ballPos.y; } +float Ball::GetWidth() { + return ball.width; +} + +float Ball::GetHeight() { + return ball.height; +} + Vector2 Ball::GetSpeed() { return ballSpeed; } @@ -15,3 +23,7 @@ Vector2 Ball::GetSpeed() { Vector2 Ball::GetInitialPosition() { return ballInitialPos; } + +Rectangle Ball::GetCollisionRec() { + return Rectangle{GetX(), GetY(), GetWidth(), GetHeight()}; +} diff --git a/src/ball/setter.cpp b/src/ball/setter.cpp index 2b14e22..ad4c000 100644 --- a/src/ball/setter.cpp +++ b/src/ball/setter.cpp @@ -7,3 +7,7 @@ void Ball::SetPosition(Vector2 position) { void Ball::SetSpeed(Vector2 speed) { ballSpeed = {speed.x, speed.y}; } + +void Ball::SetAcceleration(float acceleration) { + ballAcceleration = acceleration; +} diff --git a/src/main.cpp b/src/main.cpp index a6650fe..d895b3d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,7 @@ int main() { Map map{}; Ball ball_0{screenWidth, screenHeight, 50, 50, 0}; - Ball ball_1{screenWidth, screenHeight, 250, 250, 1}; + Ball ball_1{screenWidth, screenHeight, screenWidth - 75, screenHeight - 75, 1}; SetTargetFPS(60); @@ -19,17 +19,17 @@ int main() { while (!WindowShouldClose()) { - ball_0.Collision(); - ball_1.Collision(); + ball_0.Collision(ball_1); + ball_1.Collision(ball_0); BeginDrawing(); map.Draw(); ball_0.Draw(); - ball_0.Move(); + ball_0.Move(GetFrameTime()); ball_1.Draw(); - ball_1.Move(); + ball_1.Move(GetFrameTime()); EndDrawing(); }