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.

130 lines
5.3 KiB

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