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.

129 lines
5.4 KiB

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