physics_demo.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*******************************************************************************************
  2. *
  3. * Physac - Physics demo
  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 ground;
  31. PhysicsBody circle;
  32. //----------------------------------------------------------------------------------
  33. // Module Functions Declaration
  34. //----------------------------------------------------------------------------------
  35. void UpdateDrawFrame(void); // Update and Draw one frame
  36. //----------------------------------------------------------------------------------
  37. // Main Enry Point
  38. //----------------------------------------------------------------------------------
  39. int main()
  40. {
  41. // Initialization
  42. //--------------------------------------------------------------------------------------
  43. SetConfigFlags(FLAG_MSAA_4X_HINT);
  44. InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics demo");
  45. SetTargetFPS(60);
  46. logoX = screenWidth - MeasureText("Physac", 30) - 10;
  47. // Initialize physics and default physics bodies
  48. InitPhysics();
  49. // Create floor rectangle physics body
  50. ground = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
  51. ground->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  52. // Create obstacle circle physics body
  53. circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
  54. circle->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  55. #if defined(PLATFORM_WEB)
  56. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  57. #else
  58. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  59. //--------------------------------------------------------------------------------------
  60. // Main game loop
  61. while (!WindowShouldClose()) // Detect window close button or ESC key
  62. {
  63. UpdateDrawFrame();
  64. }
  65. #endif
  66. // De-Initialization
  67. //--------------------------------------------------------------------------------------
  68. ClosePhysics(); // Uninitialize physics
  69. CloseWindow(); // Close window and OpenGL context
  70. //--------------------------------------------------------------------------------------
  71. return 0;
  72. }
  73. //----------------------------------------------------------------------------------
  74. // Module Functions Definition
  75. //----------------------------------------------------------------------------------
  76. void UpdateDrawFrame(void)
  77. {
  78. // Update
  79. //----------------------------------------------------------------------------------
  80. if (IsKeyPressed('R')) // Reset physics input
  81. {
  82. ResetPhysics();
  83. ground = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, 500, 100, 10);
  84. ground->enabled = false;
  85. circle = CreatePhysicsBodyCircle((Vector2){ screenWidth/2, screenHeight/2 }, 45, 10);
  86. circle->enabled = false;
  87. }
  88. // Physics body creation inputs
  89. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) CreatePhysicsBodyPolygon(GetMousePosition(), GetRandomValue(20, 80), GetRandomValue(3, 8), 10);
  90. else if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)) CreatePhysicsBodyCircle(GetMousePosition(), GetRandomValue(10, 45), 10);
  91. // Destroy falling physics bodies
  92. int bodiesCount = GetPhysicsBodiesCount();
  93. for (int i = bodiesCount - 1; i >= 0; i--)
  94. {
  95. PhysicsBody body = GetPhysicsBody(i);
  96. if (body != NULL && (body->position.y > screenHeight*2)) DestroyPhysicsBody(body);
  97. }
  98. //----------------------------------------------------------------------------------
  99. // Draw
  100. //----------------------------------------------------------------------------------
  101. BeginDrawing();
  102. ClearBackground(BLACK);
  103. DrawFPS(screenWidth - 90, screenHeight - 30);
  104. // Draw created physics bodies
  105. bodiesCount = GetPhysicsBodiesCount();
  106. for (int i = 0; i < bodiesCount; i++)
  107. {
  108. PhysicsBody body = GetPhysicsBody(i);
  109. if (body != NULL)
  110. {
  111. int vertexCount = GetPhysicsShapeVerticesCount(i);
  112. for (int j = 0; j < vertexCount; j++)
  113. {
  114. // Get physics bodies shape vertices to draw lines
  115. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  116. Vector2 vertexA = GetPhysicsShapeVertex(body, j);
  117. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  118. Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
  119. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  120. }
  121. }
  122. }
  123. DrawText("Left mouse button to create a polygon", 10, 10, 10, WHITE);
  124. DrawText("Right mouse button to create a circle", 10, 25, 10, WHITE);
  125. DrawText("Press 'R' to reset example", 10, 40, 10, WHITE);
  126. DrawText("Physac", logoX, logoY, 30, WHITE);
  127. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  128. EndDrawing();
  129. //----------------------------------------------------------------------------------
  130. }