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.

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