Advertisement
Guest User

Untitled

a guest
Oct 29th, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <raylib.h>
  4. #include <raymath.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <chrono>
  8. #include <list>
  9.  
  10. using namespace std;
  11.  
  12. class Ball {
  13. public:
  14. Vector2 position;
  15. Vector2 velocity;
  16. float radius;
  17. Color color;
  18. };
  19.  
  20. void updateBall(Ball &ball, float gravity, float frictionCoefficient, float deltaTime, float screenWidth, float screenHeight, float bounciness, std::list<Ball> &balls) {
  21.  
  22. for (auto &otherBall : balls) {
  23. // Avoid checking the ball against itself
  24. if (&otherBall == &ball) {
  25. continue;
  26. }
  27.  
  28. // Calculate the distance between the two balls
  29. float distance = Vector2Distance(ball.position, otherBall.position);
  30.  
  31. // Check if a collision has occurred
  32. if (distance < (ball.radius + otherBall.radius)) {
  33. ball.velocity.x = -(ball.velocity.x);
  34. otherBall.velocity.x = -(otherBall.velocity.x);
  35. ball.velocity.y = -(ball.velocity.y);
  36. otherBall.velocity.y = -(otherBall.velocity.y);
  37. }
  38. }
  39.  
  40. ball.velocity.y -= gravity;
  41.  
  42. // Apply friction if the ball is at the top or bottom of the screen
  43. ball.velocity.x *= (ball.position.y >= screenHeight - ball.radius || ball.position.y <= ball.radius) ? frictionCoefficient : 1;
  44.  
  45. // Update the ball's position based on its velocity
  46. ball.position.x += ball.velocity.x * deltaTime;
  47. ball.position.y += -ball.velocity.y * deltaTime;
  48.  
  49. // Clamp the ball's position within the screen bounds
  50. ball.position.x = Clamp(ball.position.x, ball.radius, screenWidth - ball.radius);
  51. ball.position.y = Clamp(ball.position.y, ball.radius, screenHeight - ball.radius);
  52.  
  53. // Check for collisions with screen edges and apply bounciness
  54. ball.velocity.x *= (ball.position.x == ball.radius || ball.position.x == screenWidth - ball.radius) ? -bounciness : 1;
  55. ball.velocity.y *= (ball.position.y == ball.radius || ball.position.y == screenHeight - ball.radius) ? -bounciness : 1;
  56.  
  57. }
  58.  
  59. int randRange(int lo, int hi) {
  60. auto now = chrono::system_clock::now();
  61. int milliseconds = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch()).count();
  62. srand(milliseconds);
  63. if (lo == 0) {
  64. return rand() % hi;
  65. } else {
  66. return rand() % lo + hi;
  67. }
  68. }
  69.  
  70. Color randColor() {
  71. auto now = chrono::system_clock::now();
  72. int milliseconds = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch()).count();
  73. srand(milliseconds);
  74. int r = rand() % 255;
  75. int g = rand() % 255;
  76. int b = rand() % 255;
  77. int a = rand() % 255;
  78. unsigned char rc = (unsigned char) r;
  79. unsigned char gc = (unsigned char) g;
  80. unsigned char bc = (unsigned char) b;
  81. unsigned char ac = (unsigned char) a;
  82. return Color{rc, gc, bc, ac};
  83. }
  84.  
  85. Color randColorOpaque() {
  86. auto now = chrono::system_clock::now();
  87. int milliseconds = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch()).count();
  88. srand(milliseconds);
  89. int r = rand() % 255;
  90. int g = rand() % 255;
  91. int b = rand() % 255;
  92. unsigned char rc = (unsigned char) r;
  93. unsigned char gc = (unsigned char) g;
  94. unsigned char bc = (unsigned char) b;
  95. unsigned char ac = (unsigned char) 255;
  96. return Color{rc, gc, bc, ac};
  97. }
  98.  
  99. Ball newBall(Vector2 position, Vector2 velocity, float radius, Color color) {
  100. Ball tempBall;
  101. tempBall.position = position;
  102. tempBall.velocity = velocity;
  103. tempBall.radius = radius;
  104. tempBall.color = color;
  105. return tempBall;
  106. }
  107.  
  108. int main() {
  109. SetConfigFlags(FLAG_WINDOW_RESIZABLE);
  110.  
  111. int screenWidth = 900;
  112. int screenHeight = 900;
  113.  
  114. InitWindow(screenWidth, screenHeight, "cppPhysics");
  115. SetTargetFPS(120);
  116.  
  117. const float gravity = 9.81f;
  118. const float frictionCoefficient = 0.981f;
  119. const float bounciness = 0.7f;
  120. float accumulatedDelta;
  121.  
  122. Ball ballOne = newBall({100, 100}, {400, -450}, randRange(5, 40), randColorOpaque());
  123. Ball ballTwo = newBall({50, 200}, {400, 450}, randRange(5, 50), randColor());
  124. Ball ballThree = newBall({200, 50}, {300, -450}, randRange(10, 30), randColor());
  125.  
  126. std::list<Ball> balls;
  127. balls.push_back(ballOne);
  128. balls.push_back(ballTwo);
  129. balls.push_back(ballThree);
  130.  
  131. while (!WindowShouldClose()) {
  132. if (IsWindowResized()) {
  133. screenWidth = GetScreenWidth();
  134. screenHeight = GetScreenHeight();
  135. }
  136.  
  137. accumulatedDelta += GetFrameTime(); // accumulatedDelta is a member value
  138. while(accumulatedDelta > 0.01)
  139. {
  140. for (auto &ball : balls) {
  141. updateBall(ball, gravity, frictionCoefficient, 0.01, screenWidth, screenHeight, bounciness, balls);
  142. }
  143. accumulatedDelta -= 0.01;
  144. }
  145.  
  146. if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
  147. for (auto &ball : balls) {
  148. ball.velocity.y += randRange(-400, 1000);
  149. ball.velocity.x += randRange(-400, 1000);
  150. }
  151. }
  152.  
  153. BeginDrawing();
  154. ClearBackground(BLACK);
  155.  
  156. for (auto &ball : balls) {
  157. DrawCircleV(ball.position, ball.radius, ball.color);
  158. }
  159.  
  160. EndDrawing();
  161. }
  162.  
  163. CloseWindow();
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement