physics_shatter.c 4.2 KB

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