physics_demo.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*******************************************************************************************
  2. *
  3. * Physac - Physics demo
  4. *
  5. * NOTE 1: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations.
  6. * NOTE 2: Physac requires static C library linkage to avoid dependency on MinGW DLL (-static -lpthread)
  7. *
  8. * Use the following line to compile:
  9. *
  10. * gcc -o $(NAME_PART).exe $(FILE_NAME) -s $(RAYLIB_DIR)\raylib\raylib_icon -static -lraylib -lpthread
  11. * -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition
  12. *
  13. * Copyright (c) 2017 Victor Fisac
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #define PHYSAC_IMPLEMENTATION
  18. #include "physac.h"
  19. int main()
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. int screenWidth = 800;
  24. int screenHeight = 450;
  25. SetConfigFlags(FLAG_MSAA_4X_HINT);
  26. InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics demo");
  27. // Physac logo drawing position
  28. int logoX = screenWidth - MeasureText("Physac", 30) - 10;
  29. int logoY = 15;
  30. // Initialize physics and default physics bodies
  31. InitPhysics();
  32. // Create floor rectangle physics body
  33. PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
  34. floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  35. // Create obstacle circle physics body
  36. PhysicsBody circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
  37. circle->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  38. SetTargetFPS(60);
  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. ResetPhysics();
  48. floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
  49. floor->enabled = false;
  50. circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
  51. circle->enabled = false;
  52. }
  53. // Physics body creation inputs
  54. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) CreatePhysicsBodyPolygon(GetMousePosition(), GetRandomValue(20, 80), GetRandomValue(3, 8), 10);
  55. else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) CreatePhysicsBodyCircle(GetMousePosition(), GetRandomValue(10, 45), 10);
  56. // Destroy falling physics bodies
  57. int bodiesCount = GetPhysicsBodiesCount();
  58. for (int i = bodiesCount - 1; i >= 0; i--)
  59. {
  60. PhysicsBody body = GetPhysicsBody(i);
  61. if (body != NULL && (body->position.y > screenHeight*2)) DestroyPhysicsBody(body);
  62. }
  63. //----------------------------------------------------------------------------------
  64. // Draw
  65. //----------------------------------------------------------------------------------
  66. BeginDrawing();
  67. ClearBackground(BLACK);
  68. DrawFPS(screenWidth - 90, screenHeight - 30);
  69. // Draw created physics bodies
  70. bodiesCount = GetPhysicsBodiesCount();
  71. for (int i = 0; i < bodiesCount; i++)
  72. {
  73. PhysicsBody body = GetPhysicsBody(i);
  74. if (body != NULL)
  75. {
  76. int vertexCount = GetPhysicsShapeVerticesCount(i);
  77. for (int j = 0; j < vertexCount; j++)
  78. {
  79. // Get physics bodies shape vertices to draw lines
  80. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  81. Vector2 vertexA = GetPhysicsShapeVertex(body, j);
  82. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  83. Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
  84. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  85. }
  86. }
  87. }
  88. DrawText("Left mouse button to create a polygon", 10, 10, 10, WHITE);
  89. DrawText("Right mouse button to create a circle", 10, 25, 10, WHITE);
  90. DrawText("Press 'R' to reset example", 10, 40, 10, WHITE);
  91. DrawText("Physac", logoX, logoY, 30, WHITE);
  92. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  93. EndDrawing();
  94. //----------------------------------------------------------------------------------
  95. }
  96. // De-Initialization
  97. //--------------------------------------------------------------------------------------
  98. ClosePhysics(); // Unitialize physics
  99. CloseWindow(); // Close window and OpenGL context
  100. //--------------------------------------------------------------------------------------
  101. return 0;
  102. }