physics_demo.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*******************************************************************************************
  2. *
  3. * Physac - Physics demo
  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 demo");
  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 }, 500, 100, 10);
  30. floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  31. // Create obstacle circle physics body
  32. PhysicsBody circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
  33. circle->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  34. //--------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. if (IsKeyPressed('R')) // Reset physics input
  41. {
  42. ResetPhysics();
  43. floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
  44. floor->enabled = false;
  45. circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
  46. circle->enabled = false;
  47. }
  48. // Physics body creation inputs
  49. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) CreatePhysicsBodyPolygon(GetMousePosition(), GetRandomValue(20, 80), GetRandomValue(3, 8), 10);
  50. else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) CreatePhysicsBodyCircle(GetMousePosition(), GetRandomValue(10, 45), 10);
  51. // Destroy falling physics bodies
  52. int bodiesCount = GetPhysicsBodiesCount();
  53. for (int i = bodiesCount - 1; i >= 0; i--)
  54. {
  55. PhysicsBody body = GetPhysicsBody(i);
  56. if (body != NULL && (body->position.y > screenHeight*2)) DestroyPhysicsBody(body);
  57. }
  58. //----------------------------------------------------------------------------------
  59. // Draw
  60. //----------------------------------------------------------------------------------
  61. BeginDrawing();
  62. ClearBackground(BLACK);
  63. DrawFPS(screenWidth - 90, screenHeight - 30);
  64. // Draw created physics bodies
  65. bodiesCount = GetPhysicsBodiesCount();
  66. for (int i = 0; i < bodiesCount; i++)
  67. {
  68. PhysicsBody body = GetPhysicsBody(i);
  69. if (body != NULL)
  70. {
  71. int vertexCount = GetPhysicsShapeVerticesCount(i);
  72. for (int j = 0; j < vertexCount; j++)
  73. {
  74. // Get physics bodies shape vertices to draw lines
  75. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  76. Vector2 vertexA = GetPhysicsShapeVertex(body, j);
  77. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  78. Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
  79. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  80. }
  81. }
  82. }
  83. DrawText("Left mouse button to create a polygon", 10, 10, 10, WHITE);
  84. DrawText("Right mouse button to create a circle", 10, 25, 10, WHITE);
  85. DrawText("Press 'R' to reset example", 10, 40, 10, WHITE);
  86. DrawText("Physac", logoX, logoY, 30, WHITE);
  87. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  88. EndDrawing();
  89. //----------------------------------------------------------------------------------
  90. }
  91. // De-Initialization
  92. //--------------------------------------------------------------------------------------
  93. ClosePhysics(); // Unitialize physics
  94. CloseWindow(); // Close window and OpenGL context
  95. //--------------------------------------------------------------------------------------
  96. return 0;
  97. }