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.

118 lines
5.1 KiB

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