physics_shatter.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*******************************************************************************************
  2. *
  3. * Physac - Body shatter
  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. #if defined(PLATFORM_WEB)
  20. #include <emscripten/emscripten.h>
  21. #endif
  22. //----------------------------------------------------------------------------------
  23. // Global Variables Definition
  24. //----------------------------------------------------------------------------------
  25. int screenWidth = 800;
  26. int screenHeight = 450;
  27. // Physac logo drawing position
  28. int logoX = 0;
  29. int logoY = 15;
  30. PhysicsBody body;
  31. //----------------------------------------------------------------------------------
  32. // Module Functions Declaration
  33. //----------------------------------------------------------------------------------
  34. void UpdateDrawFrame(void); // Update and Draw one frame
  35. //----------------------------------------------------------------------------------
  36. // Main Enry Point
  37. //----------------------------------------------------------------------------------
  38. int main()
  39. {
  40. // Initialization
  41. //--------------------------------------------------------------------------------------
  42. SetConfigFlags(FLAG_MSAA_4X_HINT);
  43. InitWindow(screenWidth, screenHeight, "Physac [raylib] - Body shatter");
  44. // Physac logo drawing position
  45. logoX = screenWidth - MeasureText("Physac", 30) - 10;
  46. // Initialize physics and default physics bodies
  47. InitPhysics();
  48. SetPhysicsGravity(0, 0);
  49. // Create random polygon physics body to shatter
  50. body = CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
  51. #if defined(PLATFORM_WEB)
  52. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  53. #else
  54. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  55. //--------------------------------------------------------------------------------------
  56. // Main game loop
  57. while (!WindowShouldClose()) // Detect window close button or ESC key
  58. {
  59. UpdateDrawFrame();
  60. }
  61. #endif
  62. // De-Initialization
  63. //--------------------------------------------------------------------------------------
  64. ClosePhysics(); // Uninitialize physics
  65. CloseWindow(); // Close window and OpenGL context
  66. //--------------------------------------------------------------------------------------
  67. return 0;
  68. }
  69. //----------------------------------------------------------------------------------
  70. // Module Functions Definition
  71. //----------------------------------------------------------------------------------
  72. void UpdateDrawFrame(void)
  73. {
  74. // Update
  75. //----------------------------------------------------------------------------------
  76. if (IsKeyPressed('R')) // Reset physics input
  77. {
  78. ResetPhysics();
  79. // Create random polygon physics body to shatter
  80. body = CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
  81. }
  82. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) // Physics shatter input
  83. {
  84. // Note: some values need to be stored in variables due to asynchronous changes during main thread
  85. int count = GetPhysicsBodiesCount();
  86. for (int i = count - 1; i >= 0; i--)
  87. {
  88. PhysicsBody currentBody = GetPhysicsBody(i);
  89. if (currentBody != NULL) PhysicsShatter(currentBody, GetMousePosition(), 10/currentBody->inverseMass);
  90. }
  91. }
  92. //----------------------------------------------------------------------------------
  93. // Draw
  94. //----------------------------------------------------------------------------------
  95. BeginDrawing();
  96. ClearBackground(BLACK);
  97. // Draw created physics bodies
  98. int bodiesCount = GetPhysicsBodiesCount();
  99. for (int i = 0; i < bodiesCount; i++)
  100. {
  101. PhysicsBody currentBody = GetPhysicsBody(i);
  102. int vertexCount = GetPhysicsShapeVerticesCount(i);
  103. for (int j = 0; j < vertexCount; j++)
  104. {
  105. // Get physics bodies shape vertices to draw lines
  106. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  107. Vector2 vertexA = GetPhysicsShapeVertex(currentBody, j);
  108. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  109. Vector2 vertexB = GetPhysicsShapeVertex(currentBody, jj);
  110. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  111. }
  112. }
  113. DrawText("Left mouse button in polygon area to shatter body\nPress 'R' to reset example", 10, 10, 10, WHITE);
  114. DrawText("Physac", logoX, logoY, 30, WHITE);
  115. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  116. EndDrawing();
  117. //----------------------------------------------------------------------------------
  118. }