physics_restitution.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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) 2017 Victor Fisac
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #define PHYSAC_IMPLEMENTATION
  18. #include "physac.h"
  19. #if defined(PLATFORM_WEB)
  20. #include <emscripten/emscripten.h>
  21. #endif
  22. //----------------------------------------------------------------------------------
  23. // Global Variables Definition
  24. //----------------------------------------------------------------------------------
  25. int screenWidth = 800;
  26. int screenHeight = 450;
  27. // Physac logo drawing position
  28. int logoX = 0;
  29. int logoY = 15;
  30. PhysicsBody circleA;
  31. PhysicsBody circleB;
  32. PhysicsBody circleC;
  33. //----------------------------------------------------------------------------------
  34. // Module Functions Declaration
  35. //----------------------------------------------------------------------------------
  36. void UpdateDrawFrame(void); // Update and Draw one frame
  37. //----------------------------------------------------------------------------------
  38. // Main Enry Point
  39. //----------------------------------------------------------------------------------
  40. int main()
  41. {
  42. // Initialization
  43. //--------------------------------------------------------------------------------------
  44. SetConfigFlags(FLAG_MSAA_4X_HINT);
  45. InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics restitution");
  46. // Physac logo drawing position
  47. logoX = screenWidth - MeasureText("Physac", 30) - 10;
  48. // Initialize physics and default physics bodies
  49. InitPhysics();
  50. // Create ground rectangle physics body
  51. PhysicsBody ground = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
  52. ground->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  53. ground->restitution = 1;
  54. // Create circles physics body
  55. circleA = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.25f, screenHeight/2 }, 30, 10);
  56. circleA->restitution = 0;
  57. circleB = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.5f, screenHeight/2 }, 30, 10);
  58. circleB->restitution = 0.5f;
  59. circleC = CreatePhysicsBodyCircle((Vector2){ screenWidth*0.75f, screenHeight/2 }, 30, 10);
  60. circleC->restitution = 1;
  61. #if defined(PLATFORM_WEB)
  62. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  63. #else
  64. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  65. //--------------------------------------------------------------------------------------
  66. // Main game loop
  67. while (!WindowShouldClose()) // Detect window close button or ESC key
  68. {
  69. UpdateDrawFrame();
  70. }
  71. #endif
  72. // De-Initialization
  73. //--------------------------------------------------------------------------------------
  74. ClosePhysics(); // Uninitialize physics
  75. CloseWindow(); // Close window and OpenGL context
  76. //--------------------------------------------------------------------------------------
  77. return 0;
  78. }
  79. //----------------------------------------------------------------------------------
  80. // Module Functions Definition
  81. //----------------------------------------------------------------------------------
  82. void UpdateDrawFrame(void)
  83. {
  84. // Update
  85. //----------------------------------------------------------------------------------
  86. if (IsKeyPressed('R')) // Reset physics input
  87. {
  88. // Reset circles physics bodies position and velocity
  89. circleA->position = (Vector2){ screenWidth*0.25f, screenHeight/2 };
  90. circleA->velocity = (Vector2){ 0, 0 };
  91. circleB->position = (Vector2){ screenWidth*0.5f, screenHeight/2 };
  92. circleB->velocity = (Vector2){ 0, 0 };
  93. circleC->position = (Vector2){ screenWidth*0.75f, screenHeight/2 };
  94. circleC->velocity = (Vector2){ 0, 0 };
  95. }
  96. //----------------------------------------------------------------------------------
  97. // Draw
  98. //----------------------------------------------------------------------------------
  99. BeginDrawing();
  100. ClearBackground(BLACK);
  101. DrawFPS(screenWidth - 90, screenHeight - 30);
  102. // Draw created physics bodies
  103. int bodiesCount = GetPhysicsBodiesCount();
  104. for (int i = 0; i < bodiesCount; i++)
  105. {
  106. PhysicsBody body = GetPhysicsBody(i);
  107. int vertexCount = GetPhysicsShapeVerticesCount(i);
  108. for (int j = 0; j < vertexCount; j++)
  109. {
  110. // Get physics bodies shape vertices to draw lines
  111. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  112. Vector2 vertexA = GetPhysicsShapeVertex(body, j);
  113. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  114. Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
  115. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  116. }
  117. }
  118. DrawText("Restitution amount", (screenWidth - MeasureText("Restitution amount", 30))/2, 75, 30, WHITE);
  119. DrawText("0", circleA->position.x - MeasureText("0", 20)/2, circleA->position.y - 7, 20, WHITE);
  120. DrawText("0.5", circleB->position.x - MeasureText("0.5", 20)/2, circleB->position.y - 7, 20, WHITE);
  121. DrawText("1", circleC->position.x - MeasureText("1", 20)/2, circleC->position.y - 7, 20, WHITE);
  122. DrawText("Press 'R' to reset example", 10, 10, 10, WHITE);
  123. DrawText("Physac", logoX, logoY, 30, WHITE);
  124. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  125. EndDrawing();
  126. //----------------------------------------------------------------------------------
  127. }