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.

139 lines
6.0 KiB

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