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.

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