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.

123 lines
5.2 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 $(RAYLIB_DIR)\raylib\raylib_icon -static -lraylib -lpthread
  11. * -lglfw3 -lopengl32 -lgdi32 -lopenal32 -lwinmm -std=c99 -Wl,--subsystem,windows -Wl,-allow-multiple-definition
  12. *
  13. * Copyright (c) 2016-2018 Victor Fisac
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #define PHYSAC_IMPLEMENTATION
  18. #include "physac.h"
  19. int main()
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. int screenWidth = 800;
  24. int screenHeight = 450;
  25. SetConfigFlags(FLAG_MSAA_4X_HINT);
  26. InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics restitution");
  27. // Physac logo drawing position
  28. int logoX = screenWidth - MeasureText("Physac", 30) - 10;
  29. int logoY = 15;
  30. // Initialize physics and default physics bodies
  31. InitPhysics();
  32. // Create floor rectangle physics body
  33. PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
  34. floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  35. floor->restitution = 1;
  36. // Create circles physics body
  37. PhysicsBody circleA = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.25f, screenHeight/2 }, 30, 10);
  38. circleA->restitution = 0;
  39. PhysicsBody circleB = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.5f, screenHeight/2 }, 30, 10);
  40. circleB->restitution = 0.5f;
  41. PhysicsBody circleC = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.75f, screenHeight/2 }, 30, 10);
  42. circleC->restitution = 1;
  43. SetTargetFPS(60);
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. if (IsKeyPressed('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. ClosePhysics(); // Unitialize physics
  95. CloseWindow(); // Close window and OpenGL context
  96. //--------------------------------------------------------------------------------------
  97. return 0;
  98. }