physics_shatter.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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] - Body shatter");
  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. SetPhysicsGravity(0, 0);
  33. // Create random polygon physics body to shatter
  34. PhysicsBody body = CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
  35. SetTargetFPS(60);
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. if (IsKeyPressed('R')) // Reset physics input
  43. {
  44. ResetPhysics();
  45. // Create random polygon physics body to shatter
  46. body = CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
  47. }
  48. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) // Physics shatter input
  49. {
  50. // Note: some values need to be stored in variables due to asynchronous changes during main thread
  51. int count = GetPhysicsBodiesCount();
  52. for (int i = count - 1; i >= 0; i--)
  53. {
  54. PhysicsBody currentBody = GetPhysicsBody(i);
  55. if (currentBody != NULL) PhysicsShatter(currentBody, GetMousePosition(), 10/currentBody->inverseMass);
  56. }
  57. }
  58. //----------------------------------------------------------------------------------
  59. // Draw
  60. //----------------------------------------------------------------------------------
  61. BeginDrawing();
  62. ClearBackground(BLACK);
  63. // Draw created physics bodies
  64. int bodiesCount = GetPhysicsBodiesCount();
  65. for (int i = 0; i < bodiesCount; i++)
  66. {
  67. PhysicsBody currentBody = GetPhysicsBody(i);
  68. int vertexCount = GetPhysicsShapeVerticesCount(i);
  69. for (int j = 0; j < vertexCount; j++)
  70. {
  71. // Get physics bodies shape vertices to draw lines
  72. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  73. Vector2 vertexA = GetPhysicsShapeVertex(currentBody, j);
  74. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  75. Vector2 vertexB = GetPhysicsShapeVertex(currentBody, jj);
  76. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  77. }
  78. }
  79. DrawText("Left mouse button in polygon area to shatter body\nPress 'R' to reset example", 10, 10, 10, WHITE);
  80. DrawText("Physac", logoX, logoY, 30, WHITE);
  81. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  82. EndDrawing();
  83. //----------------------------------------------------------------------------------
  84. }
  85. // De-Initialization
  86. //--------------------------------------------------------------------------------------
  87. ClosePhysics(); // Unitialize physics
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }