physics_friction.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*******************************************************************************************
  2. *
  3. * Physac - Physics friction
  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 bodyA;
  31. PhysicsBody bodyB;
  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 friction");
  45. // Physac logo drawing position
  46. logoX = screenWidth - MeasureText("Physac", 30) - 10;
  47. // Initialize physics and default physics bodies
  48. InitPhysics();
  49. // Create floor rectangle physics body
  50. PhysicsBody ground = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
  51. ground->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  52. PhysicsBody wall = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight*0.8f }, 10, 80, 10);
  53. wall->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  54. // Create left ramp physics body
  55. PhysicsBody rectLeft = CreatePhysicsBodyRectangle((Vector2){ 25, screenHeight - 5 }, 250, 250, 10);
  56. rectLeft->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  57. SetPhysicsBodyRotation(rectLeft, 30*DEG2RAD);
  58. // Create right ramp physics body
  59. PhysicsBody rectRight = CreatePhysicsBodyRectangle((Vector2){ screenWidth - 25, screenHeight - 5 }, 250, 250, 10);
  60. rectRight->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  61. SetPhysicsBodyRotation(rectRight, 330*DEG2RAD);
  62. // Create dynamic physics bodies
  63. bodyA = CreatePhysicsBodyRectangle((Vector2){ 35, screenHeight*0.6f }, 40, 40, 10);
  64. bodyA->staticFriction = 0.1f;
  65. bodyA->dynamicFriction = 0.1f;
  66. SetPhysicsBodyRotation(bodyA, 30*DEG2RAD);
  67. bodyB = CreatePhysicsBodyRectangle((Vector2){ screenWidth - 35, screenHeight*0.6f }, 40, 40, 10);
  68. bodyB->staticFriction = 1;
  69. bodyB->dynamicFriction = 1;
  70. SetPhysicsBodyRotation(bodyB, 330*DEG2RAD);
  71. #if defined(PLATFORM_WEB)
  72. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  73. #else
  74. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  75. //--------------------------------------------------------------------------------------
  76. // Main game loop
  77. while (!WindowShouldClose()) // Detect window close button or ESC key
  78. {
  79. UpdateDrawFrame();
  80. }
  81. #endif
  82. // De-Initialization
  83. //--------------------------------------------------------------------------------------
  84. ClosePhysics(); // Uninitialize physics
  85. CloseWindow(); // Close window and OpenGL context
  86. //--------------------------------------------------------------------------------------
  87. return 0;
  88. }
  89. //----------------------------------------------------------------------------------
  90. // Module Functions Definition
  91. //----------------------------------------------------------------------------------
  92. void UpdateDrawFrame(void)
  93. {
  94. // Update
  95. //----------------------------------------------------------------------------------
  96. if (IsKeyPressed('R')) // Reset physics input
  97. {
  98. // Reset dynamic physics bodies position, velocity and rotation
  99. bodyA->position = (Vector2){ 35, screenHeight*0.6f };
  100. bodyA->velocity = (Vector2){ 0, 0 };
  101. bodyA->angularVelocity = 0;
  102. SetPhysicsBodyRotation(bodyA, 30*DEG2RAD);
  103. bodyB->position = (Vector2){ screenWidth - 35, screenHeight*0.6f };
  104. bodyB->velocity = (Vector2){ 0, 0 };
  105. bodyB->angularVelocity = 0;
  106. SetPhysicsBodyRotation(bodyB, 330*DEG2RAD);
  107. }
  108. //----------------------------------------------------------------------------------
  109. // Draw
  110. //----------------------------------------------------------------------------------
  111. BeginDrawing();
  112. ClearBackground(BLACK);
  113. DrawFPS(screenWidth - 90, screenHeight - 30);
  114. // Draw created physics bodies
  115. int bodiesCount = GetPhysicsBodiesCount();
  116. for (int i = 0; i < bodiesCount; i++)
  117. {
  118. PhysicsBody body = GetPhysicsBody(i);
  119. if (body != NULL)
  120. {
  121. int vertexCount = GetPhysicsShapeVerticesCount(i);
  122. for (int j = 0; j < vertexCount; j++)
  123. {
  124. // Get physics bodies shape vertices to draw lines
  125. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  126. Vector2 vertexA = GetPhysicsShapeVertex(body, j);
  127. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  128. Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
  129. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  130. }
  131. }
  132. }
  133. DrawRectangle(0, screenHeight - 49, screenWidth, 49, BLACK);
  134. DrawText("Friction amount", (screenWidth - MeasureText("Friction amount", 30))/2, 75, 30, WHITE);
  135. DrawText("0.1", bodyA->position.x - MeasureText("0.1", 20)/2, bodyA->position.y - 7, 20, WHITE);
  136. DrawText("1", bodyB->position.x - MeasureText("1", 20)/2, bodyB->position.y - 7, 20, WHITE);
  137. DrawText("Press 'R' to reset example", 10, 10, 10, WHITE);
  138. DrawText("Physac", logoX, logoY, 30, WHITE);
  139. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  140. EndDrawing();
  141. //----------------------------------------------------------------------------------
  142. }