上传文件至 /
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
#include "raylib.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define WIDTH 1000
|
||||
#define HIGHT 600
|
||||
|
||||
#define NUM_BALL 15
|
||||
|
||||
#define R 30
|
||||
|
||||
|
||||
typedef struct{
|
||||
float x, y, r, vx, vy;
|
||||
}Ball;
|
||||
|
||||
|
||||
void DrawBall(Ball *ball){
|
||||
|
||||
DrawCircle(ball->x,ball->y,ball->r,WHITE);
|
||||
|
||||
}
|
||||
|
||||
void Updateing(Ball *ball){
|
||||
ball->x += ball->vx;
|
||||
ball->y += ball->vy;
|
||||
|
||||
|
||||
}
|
||||
|
||||
void ElasticCollision(Ball* ball1, Ball* ball2) {
|
||||
double dx = ball2->x - ball1->x;
|
||||
double dy = ball2->y - ball1->y;
|
||||
|
||||
double dvx = ball2->vx - ball1->vx;
|
||||
double dvy = ball2->vy - ball1->vy;
|
||||
|
||||
double dot_product = dx * dvx + dy * dvy;
|
||||
|
||||
if (dot_product >= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
double distance_squared = dx * dx + dy * dy;
|
||||
|
||||
if (distance_squared < 1e-10) {
|
||||
return;
|
||||
}
|
||||
|
||||
double factor = dot_product / distance_squared;
|
||||
|
||||
double v1x_new = ball1->vx + factor * dx;
|
||||
double v1y_new = ball1->vy + factor * dy;
|
||||
double v2x_new = ball2->vx - factor * dx;
|
||||
double v2y_new = ball2->vy - factor * dy;
|
||||
|
||||
ball1->vx = v1x_new;
|
||||
ball1->vy = v1y_new;
|
||||
ball2->vx = v2x_new;
|
||||
ball2->vy = v2y_new;
|
||||
}
|
||||
|
||||
void CollidesWalls(Ball *ball){
|
||||
if (ball->x - ball->r <= 0){
|
||||
ball->x = ball->r;
|
||||
ball->vx = -ball->vx;
|
||||
}
|
||||
|
||||
if (ball->x + ball->r >= WIDTH){
|
||||
ball->x = WIDTH - ball->r;
|
||||
ball->vx = -ball->vx;
|
||||
|
||||
}
|
||||
|
||||
if (ball->y - ball->r <= 0){
|
||||
ball->y = ball->r;
|
||||
ball->vy = -ball->vy;
|
||||
}
|
||||
|
||||
if (ball->y + ball->r >= HIGHT){
|
||||
ball->y = HIGHT - ball->r;
|
||||
ball->vy = -ball->vy;
|
||||
}
|
||||
}
|
||||
|
||||
void CollidesBalls(Ball *currentball,Ball balls[]){
|
||||
|
||||
for (int i = 0;i<NUM_BALL;i++){
|
||||
|
||||
if (currentball == &balls[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Ball *otherball = &balls[i];
|
||||
|
||||
Vector2 c1 = {currentball->x, currentball->y};
|
||||
Vector2 c2 = {otherball->x, otherball->y};
|
||||
|
||||
if (CheckCollisionCircles(c1, currentball->r, c2, otherball->r)){
|
||||
|
||||
ElasticCollision(currentball,otherball);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DrawBalls(Ball balls[]){
|
||||
for (int i = 0;i<NUM_BALL;i++){
|
||||
DrawBall(balls+i);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateingBalls(Ball balls[]){
|
||||
for(int i = 0;i<NUM_BALL;i++){
|
||||
Updateing(balls+i);
|
||||
CollidesWalls(balls+i);
|
||||
CollidesBalls(balls+i,balls);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
InitWindow(WIDTH,HIGHT, "Ball Simulation");
|
||||
|
||||
SetTargetFPS(60);
|
||||
Ball balls[NUM_BALL];
|
||||
|
||||
for (int i = 0; i < NUM_BALL; i++) {
|
||||
balls[i].r = R;
|
||||
balls[i].x = GetRandomValue(balls[i].r, WIDTH - balls[i].r);
|
||||
balls[i].y = GetRandomValue(balls[i].r, HIGHT - balls[i].r);
|
||||
balls[i].vx = GetRandomValue(-3, 3);
|
||||
balls[i].vy = GetRandomValue(-3, 3);
|
||||
|
||||
if (balls[i].vx == 0) balls[i].vx = 2;
|
||||
if (balls[i].vy == 0) balls[i].vy = 2;
|
||||
}
|
||||
|
||||
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
UpdateingBalls(balls);
|
||||
BeginDrawing();
|
||||
ClearBackground(BLACK);
|
||||
UpdateingBalls(balls);
|
||||
DrawBalls(balls);
|
||||
DrawFPS(2,2);
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user