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.

125 lines
5.3 KiB

  1. /*******************************************************************************************
  2. *
  3. * Physac - Physics movement
  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. #define VELOCITY 0.5f
  18. int main()
  19. {
  20. // Initialization
  21. //--------------------------------------------------------------------------------------
  22. int screenWidth = 800;
  23. int screenHeight = 450;
  24. SetConfigFlags(FLAG_MSAA_4X_HINT);
  25. InitWindow(screenWidth, screenHeight, "Physac [raylib] - Physics movement");
  26. SetTargetFPS(60);
  27. // Physac logo drawing position
  28. int logoX = screenWidth - MeasureText("Physac", 30) - 10;
  29. int logoY = 15;
  30. // Initialize physics and default physics bodies
  31. InitPhysics();
  32. // Create floor and walls rectangle physics body
  33. PhysicsBody floor = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight }, screenWidth, 100, 10);
  34. PhysicsBody platformLeft = CreatePhysicsBodyRectangle((Vector2){ screenWidth*0.25f, screenHeight*0.6f }, screenWidth*0.25f, 10, 10);
  35. PhysicsBody platformRight = CreatePhysicsBodyRectangle((Vector2){ screenWidth*0.75f, screenHeight*0.6f }, screenWidth*0.25f, 10, 10);
  36. PhysicsBody wallLeft = CreatePhysicsBodyRectangle((Vector2){ -5, screenHeight/2 }, 10, screenHeight, 10);
  37. PhysicsBody wallRight = CreatePhysicsBodyRectangle((Vector2){ screenWidth + 5, screenHeight/2 }, 10, screenHeight, 10);
  38. // Disable dynamics to floor and walls physics bodies
  39. floor->enabled = false;
  40. platformLeft->enabled = false;
  41. platformRight->enabled = false;
  42. wallLeft->enabled = false;
  43. wallRight->enabled = false;
  44. // Create movement physics body
  45. PhysicsBody body = CreatePhysicsBodyRectangle((Vector2){ screenWidth/2, screenHeight/2 }, 50, 50, 1);
  46. body->freezeOrient = true; // Constrain body rotation to avoid little collision torque amounts
  47. //--------------------------------------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. if (IsKeyPressed('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. }