physics_movement.c 6.3 KB

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