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. * Physac - Physics restitution
  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 -static /
  11. * -lraylib -lpthread -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm /
  12. * -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition
  13. *
  14. * Copyright (c) 2016-2018 Victor Fisac
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #define PHYSAC_IMPLEMENTATION
  19. #define PHYSAC_NO_THREADS
  20. #include "physac.h"
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. SetConfigFlags(FLAG_MSAA_4X_HINT);
  28. InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics restitution");
  29. // Physac logo drawing position
  30. int logoX = screenWidth - MeasureText("Physac", 30) - 10;
  31. int logoY = 15;
  32. // Initialize physics and default physics bodies
  33. InitPhysics();
  34. // Create floor rectangle physics body
  35. PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
  36. floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  37. floor->restitution = 1;
  38. // Create circles physics body
  39. PhysicsBody circleA = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.25f, screenHeight/2 }, 30, 10);
  40. circleA->restitution = 0;
  41. PhysicsBody circleB = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.5f, screenHeight/2 }, 30, 10);
  42. circleB->restitution = 0.5f;
  43. PhysicsBody circleC = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.75f, screenHeight/2 }, 30, 10);
  44. circleC->restitution = 1;
  45. // Restitution demo needs a very tiny physics time step for a proper simulation
  46. SetPhysicsTimeStep(1.0/60.0/100*1000);
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. RunPhysicsStep();
  55. if (IsKeyPressed('R')) // Reset physics input
  56. {
  57. // Reset circles physics bodies position and velocity
  58. circleA->position = (Vector2){ screenWidth*0.25f, screenHeight/2 };
  59. circleA->velocity = (Vector2){ 0, 0 };
  60. circleB->position = (Vector2){ screenWidth*0.5f, screenHeight/2 };
  61. circleB->velocity = (Vector2){ 0, 0 };
  62. circleC->position = (Vector2){ screenWidth*0.75f, screenHeight/2 };
  63. circleC->velocity = (Vector2){ 0, 0 };
  64. }
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(BLACK);
  70. DrawFPS(screenWidth - 90, screenHeight - 30);
  71. // Draw created physics bodies
  72. int bodiesCount = GetPhysicsBodiesCount();
  73. for (int i = 0; i < bodiesCount; i++)
  74. {
  75. PhysicsBody body = GetPhysicsBody(i);
  76. int vertexCount = GetPhysicsShapeVerticesCount(i);
  77. for (int j = 0; j < vertexCount; j++)
  78. {
  79. // Get physics bodies shape vertices to draw lines
  80. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  81. Vector2 vertexA = GetPhysicsShapeVertex(body, j);
  82. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  83. Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
  84. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  85. }
  86. }
  87. DrawText("Restitution amount", (screenWidth - MeasureText("Restitution amount", 30))/2, 75, 30, WHITE);
  88. DrawText("0", circleA->position.x - MeasureText("0", 20)/2, circleA->position.y - 7, 20, WHITE);
  89. DrawText("0.5", circleB->position.x - MeasureText("0.5", 20)/2, circleB->position.y - 7, 20, WHITE);
  90. DrawText("1", circleC->position.x - MeasureText("1", 20)/2, circleC->position.y - 7, 20, WHITE);
  91. DrawText("Press 'R' to reset example", 10, 10, 10, WHITE);
  92. DrawText("Physac", logoX, logoY, 30, WHITE);
  93. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  94. EndDrawing();
  95. //----------------------------------------------------------------------------------
  96. }
  97. // De-Initialization
  98. //--------------------------------------------------------------------------------------
  99. ClosePhysics(); // Unitialize physics
  100. CloseWindow(); // Close window and OpenGL context
  101. //--------------------------------------------------------------------------------------
  102. return 0;
  103. }