You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
5.4 KiB

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