mirror of
https://github.com/raysan5/raylib.git
synced 2025-12-15 19:05:34 +00:00
REXM: ADDED: shapes_ball_physics
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [shapes] example - physics bouncing balls
|
||||
* raylib [shapes] example - ball physics
|
||||
*
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Example originally created with raylib 5.5
|
||||
* Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
|
||||
*
|
||||
* Example contributed by David Buzatto (@davidbuzatto) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
@@ -15,9 +15,10 @@
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "raylib.h"
|
||||
|
||||
#define MAX_BALLS 5000 // Maximum quantity of balls
|
||||
|
||||
@@ -42,12 +43,12 @@ int main(void)
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - physics bouncing balls");
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - ball physics");
|
||||
|
||||
Ball balls[MAX_BALLS] = {{
|
||||
.pos = {GetScreenWidth()/2, GetScreenHeight()/2},
|
||||
.vel = {200, 200},
|
||||
.ppos = {0},
|
||||
.pos = { GetScreenWidth()/2, GetScreenHeight()/2 },
|
||||
.vel = { 200, 200 },
|
||||
.ppos = { 0 },
|
||||
.radius = 40,
|
||||
.friction = 0.99,
|
||||
.elasticity = 0.9,
|
||||
@@ -55,7 +56,7 @@ int main(void)
|
||||
.grabbed = false
|
||||
}};
|
||||
|
||||
int ballQuantity = 1;
|
||||
int ballCount = 1;
|
||||
Ball *grabbedBall = NULL; // A pointer to the current ball that is grabbed
|
||||
Vector2 pressOffset = {0}; // Mouse press offset relative to the ball that grabbedd
|
||||
|
||||
@@ -75,8 +76,8 @@ int main(void)
|
||||
// Checks if a ball was grabbed
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
for (int i = ballQuantity - 1; i >= 0; i--) {
|
||||
|
||||
for (int i = ballCount - 1; i >= 0; i--)
|
||||
{
|
||||
Ball *ball = &balls[i];
|
||||
pressOffset.x = mousePos.x - ball->pos.x;
|
||||
pressOffset.y = mousePos.y - ball->pos.y;
|
||||
@@ -89,7 +90,6 @@ int main(void)
|
||||
grabbedBall = ball;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,37 +104,38 @@ int main(void)
|
||||
}
|
||||
|
||||
// Creates a new ball
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) || (IsKeyDown(KEY_LEFT_CONTROL) && IsMouseButtonDown(MOUSE_BUTTON_RIGHT))) {
|
||||
if (ballQuantity < MAX_BALLS) {
|
||||
balls[ballQuantity++] = (Ball) {
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) || (IsKeyDown(KEY_LEFT_CONTROL) && IsMouseButtonDown(MOUSE_BUTTON_RIGHT)))
|
||||
{
|
||||
if (ballCount < MAX_BALLS)
|
||||
{
|
||||
balls[ballCount++] = (Ball){
|
||||
.pos = mousePos,
|
||||
.vel = {GetRandomValue(-300, 300), GetRandomValue(-300, 300)},
|
||||
.ppos = {0},
|
||||
.vel = { GetRandomValue(-300, 300), GetRandomValue(-300, 300) },
|
||||
.ppos = { 0 },
|
||||
.radius = 20 + GetRandomValue(0, 30),
|
||||
.friction = 0.99,
|
||||
.elasticity = 0.9,
|
||||
.color = {GetRandomValue(0, 255), GetRandomValue(0, 255), GetRandomValue(0, 255), 255},
|
||||
.color = { GetRandomValue(0, 255), GetRandomValue(0, 255), GetRandomValue(0, 255), 255 },
|
||||
.grabbed = false
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Shake balls
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE)) {
|
||||
for (int i = 0; i < ballQuantity; i++) {
|
||||
Ball *ball = &balls[i];
|
||||
if (!ball->grabbed) {
|
||||
ball->vel = (Vector2) {GetRandomValue(-2000, 2000), GetRandomValue(-2000, 2000)};
|
||||
}
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE))
|
||||
{
|
||||
for (int i = 0; i < ballCount; i++)
|
||||
{
|
||||
if (!balls[i].grabbed) balls[i].vel = (Vector2){ GetRandomValue(-2000, 2000), GetRandomValue(-2000, 2000) };
|
||||
}
|
||||
}
|
||||
|
||||
// Changes gravity
|
||||
gravity += GetMouseWheelMove() * 5;
|
||||
gravity += GetMouseWheelMove()*5;
|
||||
|
||||
// Updates each ball state
|
||||
for (int i = 0; i < ballQuantity; i++) {
|
||||
|
||||
for (int i = 0; i < ballCount; i++)
|
||||
{
|
||||
Ball *ball = &balls[i];
|
||||
|
||||
// The ball is not grabbed
|
||||
@@ -145,48 +146,47 @@ int main(void)
|
||||
ball->pos.y += ball->vel.y * delta;
|
||||
|
||||
// Does the ball hit the screen right boundary?
|
||||
if (ball->pos.x + ball->radius >= screenWidth)
|
||||
if ((ball->pos.x + ball->radius) >= screenWidth)
|
||||
{
|
||||
ball->pos.x = screenWidth - ball->radius; // Ball repositioning
|
||||
ball->vel.x = -ball->vel.x * ball->elasticity; // Elasticity makes the ball lose 10% of its velocity on hit
|
||||
ball->vel.x = -ball->vel.x*ball->elasticity; // Elasticity makes the ball lose 10% of its velocity on hit
|
||||
}
|
||||
// Does the ball hit the screen left boundary?
|
||||
else if (ball->pos.x - ball->radius <= 0)
|
||||
else if ((ball->pos.x - ball->radius) <= 0)
|
||||
{
|
||||
ball->pos.x = ball->radius;
|
||||
ball->vel.x = -ball->vel.x * ball->elasticity;
|
||||
ball->vel.x = -ball->vel.x*ball->elasticity;
|
||||
}
|
||||
|
||||
// The same for y axis
|
||||
if (ball->pos.y + ball->radius >= screenHeight)
|
||||
if ((ball->pos.y + ball->radius) >= screenHeight)
|
||||
{
|
||||
ball->pos.y = screenHeight - ball->radius;
|
||||
ball->vel.y = -ball->vel.y * ball->elasticity;
|
||||
ball->vel.y = -ball->vel.y*ball->elasticity;
|
||||
}
|
||||
else if (ball->pos.y - ball->radius <= 0)
|
||||
else if ((ball->pos.y - ball->radius) <= 0)
|
||||
{
|
||||
ball->pos.y = ball->radius;
|
||||
ball->vel.y = -ball->vel.y * ball->elasticity;
|
||||
ball->vel.y = -ball->vel.y*ball->elasticity;
|
||||
}
|
||||
|
||||
// Friction makes the ball lose 1% of its velocity each frame
|
||||
ball->vel.x = ball->vel.x * ball->friction;
|
||||
ball->vel.x = ball->vel.x*ball->friction;
|
||||
// Gravity affects only the y axis
|
||||
ball->vel.y = ball->vel.y * ball->friction + gravity;
|
||||
|
||||
ball->vel.y = ball->vel.y*ball->friction + gravity;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Ball repositioning using the mouse position
|
||||
ball->pos.x = mousePos.x - pressOffset.x;
|
||||
ball->pos.y = mousePos.y - pressOffset.y;
|
||||
|
||||
// While the ball is grabbed, recalculates its velocity
|
||||
ball->vel.x = (ball->pos.x - ball->ppos.x) / delta;
|
||||
ball->vel.y = (ball->pos.y - ball->ppos.y) / delta;
|
||||
ball->vel.x = (ball->pos.x - ball->ppos.x)/delta;
|
||||
ball->vel.y = (ball->pos.y - ball->ppos.y)/delta;
|
||||
ball->ppos = ball->pos;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
@@ -195,19 +195,18 @@ int main(void)
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
for (int i = 0; i < ballQuantity; i++)
|
||||
for (int i = 0; i < ballCount; i++)
|
||||
{
|
||||
Ball *ball = &balls[i];
|
||||
DrawCircleV(ball->pos, ball->radius, ball->color);
|
||||
DrawCircleLinesV(ball->pos, ball->radius, BLACK);
|
||||
DrawCircleV(balls[i].pos, balls[i].radius, balls[i].color);
|
||||
DrawCircleLinesV(balls[i].pos, balls[i].radius, BLACK);
|
||||
}
|
||||
|
||||
DrawText("grab a ball by pressing with the mouse and throw it by releasing", 10, 10, 20, DARKGRAY);
|
||||
DrawText("right click to create new balls (keep left control pressed to create a lot)", 10, 30, 20, DARKGRAY);
|
||||
DrawText("use mouse wheel to change gravity", 10, 50, 20, DARKGRAY);
|
||||
DrawText("middle click to shake", 10, 70, 20, DARKGRAY);
|
||||
DrawText(TextFormat("ball quantity: %d", ballQuantity), 10, GetScreenHeight() - 55, 20, BLACK);
|
||||
DrawText(TextFormat("gravity: %.2f", gravity), 10, GetScreenHeight() - 35, 20, BLACK);
|
||||
DrawText("grab a ball by pressing with the mouse and throw it by releasing", 10, 10, 10, DARKGRAY);
|
||||
DrawText("right click to create new balls (keep left control pressed to create a lot)", 10, 30, 10, DARKGRAY);
|
||||
DrawText("use mouse wheel to change gravity", 10, 50, 10, DARKGRAY);
|
||||
DrawText("middle click to shake", 10, 70, 10, DARKGRAY);
|
||||
DrawText(TextFormat("BALL COUNT: %d", ballCount), 10, GetScreenHeight() - 70, 20, BLACK);
|
||||
DrawText(TextFormat("GRAVITY: %.2f", gravity), 10, GetScreenHeight() - 40, 20, BLACK);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user