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.

144 lines
6.1 KiB

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