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.

111 lines
4.3 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [physac] example - physics shatter
  4. *
  5. * This example has been created using raylib 1.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * This example uses physac 1.1 (https://github.com/raysan5/raylib/blob/master/src/physac.h)
  9. *
  10. * Copyright (c) 2016-2021 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define PHYSAC_IMPLEMENTATION
  15. #include "physac.h"
  16. int main(void)
  17. {
  18. // Initialization
  19. //--------------------------------------------------------------------------------------
  20. const int screenWidth = 800;
  21. const int screenHeight = 450;
  22. SetConfigFlags(FLAG_MSAA_4X_HINT);
  23. InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics shatter");
  24. // Physac logo drawing position
  25. int logoX = screenWidth - MeasureText("Physac", 30) - 10;
  26. int logoY = 15;
  27. // Initialize physics and default physics bodies
  28. InitPhysics();
  29. SetPhysicsGravity(0, 0);
  30. // Create random polygon physics body to shatter
  31. CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
  32. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  33. //--------------------------------------------------------------------------------------
  34. // Main game loop
  35. while (!WindowShouldClose()) // Detect window close button or ESC key
  36. {
  37. //----------------------------------------------------------------------------------
  38. UpdatePhysics(); // Update physics system
  39. if (IsKeyPressed(KEY_R)) // Reset physics input
  40. {
  41. ResetPhysics();
  42. CreatePhysicsBodyPolygon((Vector2){ screenWidth/2, screenHeight/2 }, GetRandomValue(80, 200), GetRandomValue(3, 8), 10);
  43. }
  44. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) // Physics shatter input
  45. {
  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. }