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.

128 lines
5.3 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [physac] example - physics movement
  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. #define VELOCITY 0.5f
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. SetConfigFlags(FLAG_MSAA_4X_HINT);
  24. InitWindow(screenWidth, screenHeight, "raylib [physac] example - physics movement");
  25. // Physac logo drawing position
  26. int logoX = screenWidth - MeasureText("Physac", 30) - 10;
  27. int logoY = 15;
  28. // Initialize physics and default physics bodies
  29. InitPhysics();
  30. // Create floor and walls rectangle physics body
  31. PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
  32. PhysicsBody platformLeft = CreatePhysicsBodyRectangle((Vector2){ screenWidth*0.25f, screenHeight*0.6f }, screenWidth*0.25f, 10, 10);
  33. PhysicsBody platformRight = CreatePhysicsBodyRectangle((Vector2){ screenWidth*0.75f, screenHeight*0.6f }, screenWidth*0.25f, 10, 10);
  34. PhysicsBody wallLeft = CreatePhysicsBodyRectangle((Vector2){ -5, screenHeight/2 }, 10, screenHeight, 10);
  35. PhysicsBody wallRight = CreatePhysicsBodyRectangle((Vector2){ screenWidth + 5, screenHeight/2 }, 10, screenHeight, 10);
  36. // Disable dynamics to floor and walls physics bodies
  37. floor->enabled = false;
  38. platformLeft->enabled = false;
  39. platformRight->enabled = false;
  40. wallLeft->enabled = false;
  41. wallRight->enabled = false;
  42. // Create movement physics body
  43. PhysicsBody body = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight/2 }, 50, 50, 1);
  44. body->freezeOrient = true; // Constrain body rotation to avoid little collision torque amounts
  45. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  46. //--------------------------------------------------------------------------------------
  47. // Main game loop
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. // Update
  51. //----------------------------------------------------------------------------------
  52. UpdatePhysics(); // Update physics system
  53. if (IsKeyPressed(KEY_R)) // Reset physics input
  54. {
  55. // Reset movement physics body position, velocity and rotation
  56. body->position = (Vector2){ screenWidth/2, screenHeight/2 };
  57. body->velocity = (Vector2){ 0, 0 };
  58. SetPhysicsBodyRotation(body, 0);
  59. }
  60. // Horizontal movement input
  61. if (IsKeyDown(KEY_RIGHT)) body->velocity.x = VELOCITY;
  62. else if (IsKeyDown(KEY_LEFT)) body->velocity.x = -VELOCITY;
  63. // Vertical movement input checking if player physics body is grounded
  64. if (IsKeyDown(KEY_UP) && body->isGrounded) body->velocity.y = -VELOCITY*4;
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(BLACK);
  70. DrawFPS(screenWidth - 90, screenHeight - 30);
  71. // Draw created physics bodies
  72. int bodiesCount = GetPhysicsBodiesCount();
  73. for (int i = 0; i < bodiesCount; i++)
  74. {
  75. PhysicsBody body = GetPhysicsBody(i);
  76. int vertexCount = GetPhysicsShapeVerticesCount(i);
  77. for (int j = 0; j < vertexCount; j++)
  78. {
  79. // Get physics bodies shape vertices to draw lines
  80. // Note: GetPhysicsShapeVertex() already calculates rotation transformations
  81. Vector2 vertexA = GetPhysicsShapeVertex(body, j);
  82. int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
  83. Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
  84. DrawLineV(vertexA, vertexB, GREEN); // Draw a line between two vertex positions
  85. }
  86. }
  87. DrawText("Use 'ARROWS' to move player", 10, 10, 10, WHITE);
  88. DrawText("Press 'R' to reset example", 10, 30, 10, WHITE);
  89. DrawText("Physac", logoX, logoY, 30, WHITE);
  90. DrawText("Powered by", logoX + 50, logoY - 7, 10, WHITE);
  91. EndDrawing();
  92. //----------------------------------------------------------------------------------
  93. }
  94. // De-Initialization
  95. //--------------------------------------------------------------------------------------
  96. ClosePhysics(); // Unitialize physics
  97. CloseWindow(); // Close window and OpenGL context
  98. //--------------------------------------------------------------------------------------
  99. return 0;
  100. }