physics_restitution.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*******************************************************************************************
  2. *
  3. * Physac - Physics restitution
  4. *
  5. * NOTE: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations.
  6. * The file pthreadGC2.dll is required to run the program; you can find it in 'src\external'
  7. *
  8. * Copyright (c) 2016 Victor Fisac
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define PHYSAC_IMPLEMENTATION
  13. #include "..\src\physac.h"
  14. int main()
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. SetConfigFlags(FLAG_MSAA_4X_HINT);
  21. InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics restitution");
  22. SetTargetFPS(60);
  23. // Physac logo drawing position
  24. int logoX = screenWidth - MeasureText("Physac", 30) - 10;
  25. int logoY = 15;
  26. // Initialize physics and default physics bodies
  27. InitPhysics();
  28. // Create floor rectangle physics body
  29. PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
  30. floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  31. floor->restitution = 1;
  32. // Create circles physics body
  33. PhysicsBody circleA = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.25f, screenHeight/2 }, 30, 10);
  34. circleA->restitution = 0;
  35. PhysicsBody circleB = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.5f, screenHeight/2 }, 30, 10);
  36. circleB->restitution = 0.5f;
  37. PhysicsBody circleC = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.75f, screenHeight/2 }, 30, 10);
  38. circleC->restitution = 1;
  39. //--------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. if (IsKeyPressed('R')) // Reset physics input
  46. {
  47. // Reset circles physics bodies position and velocity
  48. circleA->position = (Vector2){ screenWidth*0.25f, screenHeight/2 };
  49. circleA->velocity = (Vector2){ 0, 0 };
  50. circleB->position = (Vector2){ screenWidth*0.5f, screenHeight/2 };
  51. circleB->velocity = (Vector2){ 0, 0 };
  52. circleC->position = (Vector2){ screenWidth*0.75f, screenHeight/2 };
  53. circleC->velocity = (Vector2){ 0, 0 };
  54. }
  55. //----------------------------------------------------------------------------------
  56. // Draw
  57. //----------------------------------------------------------------------------------
  58. BeginDrawing();
  59. ClearBackground(BLACK);
  60. DrawFPS(screenWidth - 90, screenHeight - 30);
  61. // Draw created physics bodies
  62. int bodiesCount = GetPhysicsBodiesCount();
  63. for (int i = 0; i < bodiesCount; i++)
  64. {
  65. PhysicsBody body = GetPhysicsBody(i);
  66. int vertexCount = GetPhysicsShapeVerticesCount(i);
  67. for (int j = 0; j < vertexCount; j++)
  68. {
  69. // Get physics bodies shape vertices to draw lines
  70. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  71. Vector2 vertexA = GetPhysicsShapeVertex(body, j);
  72. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  73. Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
  74. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  75. }
  76. }
  77. DrawText("Restitution amount", (screenWidth - MeasureText("Restitution amount", 30))/2, 75, 30, WHITE);
  78. DrawText("0", circleA->position.x - MeasureText("0", 20)/2, circleA->position.y - 7, 20, WHITE);
  79. DrawText("0.5", circleB->position.x - MeasureText("0.5", 20)/2, circleB->position.y - 7, 20, WHITE);
  80. DrawText("1", circleC->position.x - MeasureText("1", 20)/2, circleC->position.y - 7, 20, WHITE);
  81. DrawText("Press 'R' to reset example", 10, 10, 10, WHITE);
  82. DrawText("Physac", logoX, logoY, 30, WHITE);
  83. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  84. EndDrawing();
  85. //----------------------------------------------------------------------------------
  86. }
  87. // De-Initialization
  88. //--------------------------------------------------------------------------------------
  89. ClosePhysics(); // Unitialize physics
  90. CloseWindow(); // Close window and OpenGL context
  91. //--------------------------------------------------------------------------------------
  92. return 0;
  93. }