[examples] Add shapes_bouncing_ball with gravity

This commit is contained in:
Jopestpe 2025-09-29 21:23:43 -03:00 committed by GitHub
parent 0e57a572b4
commit fefbac4458
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,7 +4,9 @@
* *
* 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,6 +33,7 @@ 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 pause = 0; bool pause = 0;
int framesCounter = 0; int framesCounter = 0;
@ -50,9 +53,11 @@ int main(void)
ballPosition.x += ballSpeed.x; ballPosition.x += ballSpeed.x;
ballPosition.y += ballSpeed.y; ballPosition.y += ballSpeed.y;
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))) ballSpeed.y *= -0.95f;
} }
else framesCounter++; else framesCounter++;
//----------------------------------------------------- //-----------------------------------------------------