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.

128 lines
5.2 KiB

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