Browse Source

[examples] Update `shapes_bouncing_ball` with gravity (#5217)

* [examples] Add shapes_bouncing_ball with gravity

* gravity could be enabled/disabled
Jopestpe 2 months ago
parent
commit
0e2e8ce225
1 changed files with 14 additions and 4 deletions
  1. 14 4
      examples/shapes/shapes_bouncing_ball.c

+ 14 - 4
examples/shapes/shapes_bouncing_ball.c

@@ -4,8 +4,10 @@
 *
 *
 *   Example complexity rating: [★☆☆☆] 1/4
 *   Example complexity rating: [★☆☆☆] 1/4
 *
 *
-*   Example originally created with raylib 2.5, last time updated with raylib 2.5
+*   Example originally created with raylib 2.5, last time updated with raylib 5.6
 *
 *
+*   Example contributed by Jopestpe (@jopestpe)
+* 
 *   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
 *   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
 *   BSD-like license that allows static linking with closed source software
 *   BSD-like license that allows static linking with closed source software
 *
 *
@@ -31,7 +33,9 @@ int main(void)
     Vector2 ballPosition = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
     Vector2 ballPosition = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
     Vector2 ballSpeed = { 5.0f, 4.0f };
     Vector2 ballSpeed = { 5.0f, 4.0f };
     int ballRadius = 20;
     int ballRadius = 20;
+    float gravity = 0.2f;
 
 
+    bool useGravity = true;
     bool pause = 0;
     bool pause = 0;
     int framesCounter = 0;
     int framesCounter = 0;
 
 
@@ -43,16 +47,19 @@ int main(void)
     {
     {
         // Update
         // Update
         //-----------------------------------------------------
         //-----------------------------------------------------
+        if (IsKeyPressed(KEY_G)) useGravity = !useGravity;
         if (IsKeyPressed(KEY_SPACE)) pause = !pause;
         if (IsKeyPressed(KEY_SPACE)) pause = !pause;
-
+        
         if (!pause)
         if (!pause)
         {
         {
             ballPosition.x += ballSpeed.x;
             ballPosition.x += ballSpeed.x;
             ballPosition.y += ballSpeed.y;
             ballPosition.y += ballSpeed.y;
 
 
+            if (useGravity) ballSpeed.y += gravity;
+            
             // Check walls collision for bouncing
             // Check walls collision for bouncing
             if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
             if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
-            if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f;
+            if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -0.95f;
         }
         }
         else framesCounter++;
         else framesCounter++;
         //-----------------------------------------------------
         //-----------------------------------------------------
@@ -65,12 +72,15 @@ int main(void)
 
 
             DrawCircleV(ballPosition, (float)ballRadius, MAROON);
             DrawCircleV(ballPosition, (float)ballRadius, MAROON);
             DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY);
             DrawText("PRESS SPACE to PAUSE BALL MOVEMENT", 10, GetScreenHeight() - 25, 20, LIGHTGRAY);
+            
+            if (useGravity) DrawText("GRAVITY: ON (Press G to disable)", 10, GetScreenHeight() - 50, 20, DARKGREEN);
+            else DrawText("GRAVITY: OFF (Press G to enable)", 10, GetScreenHeight() - 50, 20, RED);
 
 
             // On pause, we draw a blinking message
             // On pause, we draw a blinking message
             if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY);
             if (pause && ((framesCounter/30)%2)) DrawText("PAUSED", 350, 200, 30, GRAY);
 
 
             DrawFPS(10, 10);
             DrawFPS(10, 10);
-
+            
         EndDrawing();
         EndDrawing();
         //-----------------------------------------------------
         //-----------------------------------------------------
     }
     }