mirror of
https://github.com/raysan5/raylib.git
synced 2025-12-25 10:22:33 -05:00
REXM: ADDED: shapes_ball_physics
This commit is contained in:
@ -560,6 +560,7 @@ CORE = \
|
||||
core/core_world_screen
|
||||
|
||||
SHAPES = \
|
||||
shapes/shapes_ball_physics \
|
||||
shapes/shapes_basic_shapes \
|
||||
shapes/shapes_bouncing_ball \
|
||||
shapes/shapes_bullet_hell \
|
||||
|
||||
@ -548,6 +548,7 @@ CORE = \
|
||||
core/core_world_screen
|
||||
|
||||
SHAPES = \
|
||||
shapes/shapes_ball_physics \
|
||||
shapes/shapes_basic_shapes \
|
||||
shapes/shapes_bouncing_ball \
|
||||
shapes/shapes_bullet_hell \
|
||||
@ -864,6 +865,9 @@ core/core_world_screen: core/core_world_screen.c
|
||||
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
|
||||
|
||||
# Compile SHAPES examples
|
||||
shapes/shapes_ball_physics: shapes/shapes_ball_physics.c
|
||||
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
|
||||
|
||||
shapes/shapes_basic_shapes: shapes/shapes_basic_shapes.c
|
||||
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
|
||||
|
||||
|
||||
@ -17,7 +17,7 @@ You may find it easier to use than other toolchains, especially when it comes to
|
||||
- `zig build [module]` to compile all examples for a module (e.g. `zig build core`)
|
||||
- `zig build [example]` to compile _and run_ a particular example (e.g. `zig build core_basic_window`)
|
||||
|
||||
## EXAMPLES COLLECTION [TOTAL: 200]
|
||||
## EXAMPLES COLLECTION [TOTAL: 201]
|
||||
|
||||
### category: core [47]
|
||||
|
||||
@ -73,7 +73,7 @@ Examples using raylib [core](../src/rcore.c) module platform functionality: wind
|
||||
| [core_text_file_loading](core/core_text_file_loading.c) | <img src="core/core_text_file_loading.png" alt="core_text_file_loading" width="80"> | ⭐☆☆☆ | 5.5 | 5.6 | [Aanjishnu Bhattacharyya](https://github.com/NimComPoo-04) |
|
||||
| [core_compute_hash](core/core_compute_hash.c) | <img src="core/core_compute_hash.png" alt="core_compute_hash" width="80"> | ⭐⭐☆☆ | 5.6-dev | 5.6-dev | [Ramon Santamaria](https://github.com/raysan5) |
|
||||
|
||||
### category: shapes [36]
|
||||
### category: shapes [37]
|
||||
|
||||
Examples using raylib shapes drawing functionality, provided by raylib [shapes](../src/rshapes.c) module.
|
||||
|
||||
@ -115,6 +115,7 @@ Examples using raylib shapes drawing functionality, provided by raylib [shapes](
|
||||
| [shapes_math_angle_rotation](shapes/shapes_math_angle_rotation.c) | <img src="shapes/shapes_math_angle_rotation.png" alt="shapes_math_angle_rotation" width="80"> | ⭐☆☆☆ | 5.6-dev | 5.6 | [Kris](https://github.com/krispy-snacc) |
|
||||
| [shapes_rlgl_color_wheel](shapes/shapes_rlgl_color_wheel.c) | <img src="shapes/shapes_rlgl_color_wheel.png" alt="shapes_rlgl_color_wheel" width="80"> | ⭐⭐⭐☆ | 5.6-dev | 5.6-dev | [Robin](https://github.com/RobinsAviary) |
|
||||
| [shapes_rlgl_triangle](shapes/shapes_rlgl_triangle.c) | <img src="shapes/shapes_rlgl_triangle.png" alt="shapes_rlgl_triangle" width="80"> | ⭐⭐☆☆ | 5.6-dev | 5.6-dev | [Robin](https://github.com/RobinsAviary) |
|
||||
| [shapes_ball_physics](shapes/shapes_ball_physics.c) | <img src="shapes/shapes_ball_physics.png" alt="shapes_ball_physics" width="80"> | ⭐⭐☆☆ | 5.6-dev | 5.6-dev | [David Buzatto](https://github.com/davidbuzatto) |
|
||||
|
||||
### category: textures [28]
|
||||
|
||||
|
||||
@ -90,6 +90,7 @@ shapes;shapes_lines_drawing;★☆☆☆;5.6-dev;5.6;2025;2025;"Robin";@RobinsAv
|
||||
shapes;shapes_math_angle_rotation;★☆☆☆;5.6-dev;5.6;2025;2025;"Kris";@krispy-snacc
|
||||
shapes;shapes_rlgl_color_wheel;★★★☆;5.6-dev;5.6-dev;2025;2025;"Robin";@RobinsAviary
|
||||
shapes;shapes_rlgl_triangle;★★☆☆;5.6-dev;5.6-dev;2025;2025;"Robin";@RobinsAviary
|
||||
shapes;shapes_ball_physics;★★☆☆;5.6-dev;5.6-dev;2025;2025;"David Buzatto";@davidbuzatto
|
||||
textures;textures_logo_raylib;★☆☆☆;1.0;1.0;2014;2025;"Ramon Santamaria";@raysan5
|
||||
textures;textures_srcrec_dstrec;★★★☆;1.3;1.3;2015;2025;"Ramon Santamaria";@raysan5
|
||||
textures;textures_image_drawing;★★☆☆;1.4;1.4;2016;2025;"Ramon Santamaria";@raysan5
|
||||
|
||||
@ -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();
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 23 KiB |
Reference in New Issue
Block a user