physics_basic_rigidbody.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*******************************************************************************************
  2. *
  3. * raylib [physac] physics example - Basic rigidbody
  4. *
  5. * This example has been created using raylib 1.4 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2016 Victor Fisac and Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define OBJECT_SIZE 50
  13. #define PLAYER_INDEX 0
  14. int main()
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [physics] example - basic rigidbody");
  21. InitPhysics(3); // Initialize physics system with maximum physic objects
  22. // Object initialization
  23. Transform player = (Transform){(Vector2){(screenWidth - OBJECT_SIZE) / 2, (screenHeight - OBJECT_SIZE) / 2}, 0.0f, (Vector2){OBJECT_SIZE, OBJECT_SIZE}};
  24. AddCollider(PLAYER_INDEX, (Collider){true, COLLIDER_RECTANGLE, (Rectangle){player.position.x, player.position.y, player.scale.x, player.scale.y}, 0});
  25. AddRigidbody(PLAYER_INDEX, (Rigidbody){true, 1.0f, (Vector2){0, 0}, (Vector2){0, 0}, false, false, true, 0.5f, 1.0f});
  26. // Floor initialization
  27. // NOTE: floor doesn't need a rigidbody because it's a static physic object, just a collider to collide with other dynamic colliders (with rigidbody)
  28. Transform floor = (Transform){(Vector2){0, screenHeight * 0.8f}, 0.0f, (Vector2){screenWidth, screenHeight * 0.2f}};
  29. AddCollider(PLAYER_INDEX + 1, (Collider){true, COLLIDER_RECTANGLE, (Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, 0});
  30. // Object properties initialization
  31. float moveSpeed = 6.0f;
  32. float jumpForce = 5.0f;
  33. bool physicsDebug = false;
  34. SetTargetFPS(60);
  35. //--------------------------------------------------------------------------------------
  36. // Main game loop
  37. while (!WindowShouldClose()) // Detect window close button or ESC key
  38. {
  39. // Update
  40. //----------------------------------------------------------------------------------
  41. // Update object physics
  42. // NOTE: all physics detections and reactions are calculated in ApplyPhysics() function (You will live happier :D)
  43. ApplyPhysics(PLAYER_INDEX, &player.position);
  44. // Check jump button input
  45. if (IsKeyDown(KEY_SPACE) && GetRigidbody(PLAYER_INDEX).isGrounded)
  46. {
  47. // Reset object Y velocity to avoid double jumping cases but keep the same X velocity that it already has
  48. SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){GetRigidbody(PLAYER_INDEX).velocity.x, 0});
  49. // Add jumping force in Y axis
  50. AddRigidbodyForce(PLAYER_INDEX, (Vector2){0, jumpForce});
  51. }
  52. // Check movement buttons input
  53. if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D))
  54. {
  55. // Set rigidbody velocity in X based on moveSpeed value and apply the same Y velocity that it already has
  56. SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){moveSpeed, GetRigidbody(PLAYER_INDEX).velocity.y});
  57. }
  58. else if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A))
  59. {
  60. // Set rigidbody velocity in X based on moveSpeed negative value and apply the same Y velocity that it already has
  61. SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){-moveSpeed, GetRigidbody(PLAYER_INDEX).velocity.y});
  62. }
  63. // Check debug mode toggle button input
  64. if (IsKeyPressed(KEY_P)) physicsDebug = !physicsDebug;
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(RAYWHITE);
  70. // Draw information
  71. DrawText("Use LEFT / RIGHT to MOVE and SPACE to JUMP", (screenWidth - MeasureText("Use LEFT / RIGHT to MOVE and SPACE to JUMP", 20)) / 2, screenHeight * 0.20f, 20, LIGHTGRAY);
  72. DrawText("Use P to switch DEBUG MODE", (screenWidth - MeasureText("Use P to switch DEBUG MODE", 20)) / 2, screenHeight * 0.3f, 20, LIGHTGRAY);
  73. // Check if debug mode is enabled
  74. if (physicsDebug)
  75. {
  76. // Draw every internal physics stored collider if it is active
  77. for (int i = 0; i < 2; i++)
  78. {
  79. if (GetCollider(i).enabled)
  80. {
  81. DrawRectangleLines(GetCollider(i).bounds.x, GetCollider(i).bounds.y, GetCollider(i).bounds.width, GetCollider(i).bounds.height, GREEN);
  82. }
  83. }
  84. }
  85. else
  86. {
  87. // Draw player and floor
  88. DrawRectangleRec((Rectangle){player.position.x, player.position.y, player.scale.x, player.scale.y}, GRAY);
  89. DrawRectangleRec((Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, BLACK);
  90. }
  91. EndDrawing();
  92. //----------------------------------------------------------------------------------
  93. }
  94. // De-Initialization
  95. //--------------------------------------------------------------------------------------
  96. UnloadPhysics(); // Unload physic objects
  97. CloseWindow(); // Close window and OpenGL context
  98. //--------------------------------------------------------------------------------------
  99. return 0;
  100. }