physics_friction.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*******************************************************************************************
  2. *
  3. * Physac - Physics friction
  4. *
  5. * NOTE: Physac requires multi-threading, when InitPhysics() a second thread is created to manage physics calculations.
  6. * The file pthreadGC2.dll is required to run the program; you can find it in 'src\external'
  7. *
  8. * Copyright (c) 2016 Victor Fisac
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define PHYSAC_IMPLEMENTATION
  13. #include "..\src\physac.h"
  14. int main()
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. SetConfigFlags(FLAG_MSAA_4X_HINT);
  21. InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics friction");
  22. SetTargetFPS(60);
  23. // Physac logo drawing position
  24. int logoX = screenWidth - MeasureText("Physac", 30) - 10;
  25. int logoY = 15;
  26. // Initialize physics and default physics bodies
  27. InitPhysics();
  28. // Create floor rectangle physics body
  29. PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
  30. floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  31. PhysicsBody wall = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight*0.8f }, 10, 80, 10);
  32. wall->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  33. // Create left ramp physics body
  34. PhysicsBody rectLeft = CreatePhysicsBodyRectangle((Vector2){ 25, screenHeight - 5 }, 250, 250, 10);
  35. rectLeft->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  36. SetPhysicsBodyRotation(rectLeft, 30*DEG2RAD);
  37. // Create right ramp physics body
  38. PhysicsBody rectRight = CreatePhysicsBodyRectangle((Vector2){ screenWidth - 25, screenHeight - 5 }, 250, 250, 10);
  39. rectRight->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  40. SetPhysicsBodyRotation(rectRight, 330*DEG2RAD);
  41. // Create dynamic physics bodies
  42. PhysicsBody bodyA = CreatePhysicsBodyRectangle((Vector2){ 35, screenHeight*0.6f }, 40, 40, 10);
  43. bodyA->staticFriction = 0.1f;
  44. bodyA->dynamicFriction = 0.1f;
  45. SetPhysicsBodyRotation(bodyA, 30*DEG2RAD);
  46. PhysicsBody bodyB = CreatePhysicsBodyRectangle((Vector2){ screenWidth - 35, screenHeight*0.6f }, 40, 40, 10);
  47. bodyB->staticFriction = 1;
  48. bodyB->dynamicFriction = 1;
  49. SetPhysicsBodyRotation(bodyB, 330*DEG2RAD);
  50. //--------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. // Update
  55. //----------------------------------------------------------------------------------
  56. if (IsKeyPressed('R')) // Reset physics input
  57. {
  58. // Reset dynamic physics bodies position, velocity and rotation
  59. bodyA->position = (Vector2){ 35, screenHeight*0.6f };
  60. bodyA->velocity = (Vector2){ 0, 0 };
  61. bodyA->angularVelocity = 0;
  62. SetPhysicsBodyRotation(bodyA, 30*DEG2RAD);
  63. bodyB->position = (Vector2){ screenWidth - 35, screenHeight*0.6f };
  64. bodyB->velocity = (Vector2){ 0, 0 };
  65. bodyB->angularVelocity = 0;
  66. SetPhysicsBodyRotation(bodyB, 330*DEG2RAD);
  67. }
  68. //----------------------------------------------------------------------------------
  69. // Draw
  70. //----------------------------------------------------------------------------------
  71. BeginDrawing();
  72. ClearBackground(BLACK);
  73. DrawFPS(screenWidth - 90, screenHeight - 30);
  74. // Draw created physics bodies
  75. int bodiesCount = GetPhysicsBodiesCount();
  76. for (int i = 0; i < bodiesCount; i++)
  77. {
  78. PhysicsBody body = GetPhysicsBody(i);
  79. if (body != NULL)
  80. {
  81. int vertexCount = GetPhysicsShapeVerticesCount(i);
  82. for (int j = 0; j < vertexCount; j++)
  83. {
  84. // Get physics bodies shape vertices to draw lines
  85. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  86. Vector2 vertexA = GetPhysicsShapeVertex(body, j);
  87. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  88. Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
  89. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  90. }
  91. }
  92. }
  93. DrawRectangle(0, screenHeight - 49, screenWidth, 49, BLACK);
  94. DrawText("Friction amount", (screenWidth - MeasureText("Friction amount", 30))/2, 75, 30, WHITE);
  95. DrawText("0.1", bodyA->position.x - MeasureText("0.1", 20)/2, bodyA->position.y - 7, 20, WHITE);
  96. DrawText("1", bodyB->position.x - MeasureText("1", 20)/2, bodyB->position.y - 7, 20, WHITE);
  97. DrawText("Press 'R' to reset example", 10, 10, 10, WHITE);
  98. DrawText("Physac", logoX, logoY, 30, WHITE);
  99. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  100. EndDrawing();
  101. //----------------------------------------------------------------------------------
  102. }
  103. // De-Initialization
  104. //--------------------------------------------------------------------------------------
  105. ClosePhysics(); // Unitialize physics
  106. CloseWindow(); // Close window and OpenGL context
  107. //--------------------------------------------------------------------------------------
  108. return 0;
  109. }