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.

142 lines
6.1 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [physac] example - physics friction
  4. *
  5. * This example has been created using raylib 1.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * This example uses physac 1.1 (https://github.com/raysan5/raylib/blob/master/src/physac.h)
  9. *
  10. * Copyright (c) 2016-2021 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define PHYSAC_IMPLEMENTATION
  15. #include "physac.h"
  16. int main(void)
  17. {
  18. // Initialization
  19. //--------------------------------------------------------------------------------------
  20. const int screenWidth = 800;
  21. const int screenHeight = 450;
  22. SetConfigFlags(FLAG_MSAA_4X_HINT);
  23. InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics friction");
  24. // Physac logo drawing position
  25. int logoX = screenWidth - MeasureText("Physac", 30) - 10;
  26. int logoY = 15;
  27. // Initialize physics and default physics bodies
  28. InitPhysics();
  29. // Create floor rectangle physics body
  30. PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
  31. floor->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  32. PhysicsBody wall = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight*0.8f }, 10, 80, 10);
  33. wall->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  34. // Create left ramp physics body
  35. PhysicsBody rectLeft = CreatePhysicsBodyRectangle((Vector2){ 25, screenHeight - 5 }, 250, 250, 10);
  36. rectLeft->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  37. SetPhysicsBodyRotation(rectLeft, 30*DEG2RAD);
  38. // Create right ramp physics body
  39. PhysicsBody rectRight = CreatePhysicsBodyRectangle((Vector2){ screenWidth - 25, screenHeight - 5 }, 250, 250, 10);
  40. rectRight->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
  41. SetPhysicsBodyRotation(rectRight, 330*DEG2RAD);
  42. // Create dynamic physics bodies
  43. PhysicsBody bodyA = CreatePhysicsBodyRectangle((Vector2){ 35, screenHeight*0.6f }, 40, 40, 10);
  44. bodyA->staticFriction = 0.1f;
  45. bodyA->dynamicFriction = 0.1f;
  46. SetPhysicsBodyRotation(bodyA, 30*DEG2RAD);
  47. PhysicsBody bodyB = CreatePhysicsBodyRectangle((Vector2){ screenWidth - 35, screenHeight*0.6f }, 40, 40, 10);
  48. bodyB->staticFriction = 1.0f;
  49. bodyB->dynamicFriction = 1.0f;
  50. SetPhysicsBodyRotation(bodyB, 330*DEG2RAD);
  51. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  52. //--------------------------------------------------------------------------------------
  53. // Main game loop
  54. while (!WindowShouldClose()) // Detect window close button or ESC key
  55. {
  56. // Update
  57. //----------------------------------------------------------------------------------
  58. UpdatePhysics(); // Update physics system
  59. if (IsKeyPressed(KEY_R)) // Reset physics system
  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. }