physics_basic_rigidbody.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*******************************************************************************************
  2. *
  3. * raylib [physics] example - Basic rigidbody
  4. *
  5. * Welcome to raylib!
  6. *
  7. * To test examples, just press F6 and execute raylib_compile_execute script
  8. * Note that compiled executable is placed in the same folder as .c file
  9. *
  10. * You can find all basic examples on C:\raylib\raylib\examples folder or
  11. * raylib official webpage: www.raylib.com
  12. *
  13. * Enjoy using raylib. :)
  14. *
  15. * This example has been created using raylib 1.3 (www.raylib.com)
  16. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  17. *
  18. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  19. *
  20. ********************************************************************************************/
  21. #include "raylib.h"
  22. #define OBJECT_SIZE 50
  23. #define PLAYER_INDEX 0
  24. int main()
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. int screenWidth = 800;
  29. int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [physics] example - basic rigidbody");
  31. SetTargetFPS(60); // Enable v-sync
  32. InitPhysics(); // Initialize internal physics values (max rigidbodies/colliders available: 1024)
  33. // Physics initialization
  34. Physics worldPhysics = {true, false, (Vector2){0, -9.81f}};
  35. // Set internal physics settings
  36. SetPhysics(worldPhysics);
  37. // Object initialization
  38. Transform player = (Transform){(Vector2){(screenWidth - OBJECT_SIZE) / 2, (screenHeight - OBJECT_SIZE) / 2}, 0.0f, (Vector2){OBJECT_SIZE, OBJECT_SIZE}};
  39. AddCollider(PLAYER_INDEX, (Collider){true, RectangleCollider, (Rectangle){player.position.x, player.position.y, player.scale.x, player.scale.y}, 0});
  40. AddRigidbody(PLAYER_INDEX, (Rigidbody){true, 1.0f, (Vector2){0, 0}, (Vector2){0, 0}, false, false, true, 0.5f, 1.0f});
  41. // Floor initialization
  42. // 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)
  43. Transform floor = (Transform){(Vector2){0, screenHeight * 0.8f}, 0.0f, (Vector2){screenWidth, screenHeight * 0.2f}};
  44. AddCollider(PLAYER_INDEX + 1, (Collider){true, RectangleCollider, (Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, 0});
  45. // Object properties initialization
  46. float moveSpeed = 6.0f;
  47. float jumpForce = 4.5f;
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. // Update object physics
  55. // NOTE: all physics detections and reactions are calculated in ApplyPhysics() function (You will live happier :D)
  56. ApplyPhysics(PLAYER_INDEX, &player.position);
  57. // Check jump button input
  58. if(IsKeyDown(KEY_SPACE) && GetRigidbody(PLAYER_INDEX).isGrounded)
  59. {
  60. // Reset object Y velocity to avoid double jumping cases but keep the same X velocity that it already has
  61. SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){GetRigidbody(PLAYER_INDEX).velocity.x, 0});
  62. // Add jumping force in Y axis
  63. AddRigidbodyForce(PLAYER_INDEX, (Vector2){0, jumpForce});
  64. }
  65. // Check movement buttons input
  66. if(IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D))
  67. {
  68. // Set rigidbody velocity in X based on moveSpeed value and apply the same Y velocity that it already has
  69. SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){moveSpeed, GetRigidbody(PLAYER_INDEX).velocity.y});
  70. }
  71. else if(IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A))
  72. {
  73. // Set rigidbody velocity in X based on moveSpeed negative value and apply the same Y velocity that it already has
  74. SetRigidbodyVelocity(PLAYER_INDEX, (Vector2){-moveSpeed, GetRigidbody(PLAYER_INDEX).velocity.y});
  75. }
  76. // Check debug mode toggle button input
  77. if(IsKeyPressed(KEY_P))
  78. {
  79. // Update program physics value
  80. worldPhysics.debug = !worldPhysics.debug;
  81. // Update internal physics value
  82. SetPhysics(worldPhysics);
  83. }
  84. //----------------------------------------------------------------------------------
  85. // Draw
  86. //----------------------------------------------------------------------------------
  87. BeginDrawing();
  88. ClearBackground(RAYWHITE);
  89. // Draw information
  90. 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);
  91. DrawText("Use P to switch DEBUG MODE", (screenWidth - MeasureText("Use P to switch DEBUG MODE", 20)) / 2, screenHeight * 0.3f, 20, LIGHTGRAY);
  92. // Check if debug mode is enabled
  93. if(worldPhysics.debug)
  94. {
  95. // Draw every internal physics stored collider if it is active
  96. for(int i = 0; i < 2; i++)
  97. {
  98. if(GetCollider(i).enabled)
  99. {
  100. DrawRectangleLines(GetCollider(i).bounds.x, GetCollider(i).bounds.y, GetCollider(i).bounds.width, GetCollider(i).bounds.height, GREEN);
  101. }
  102. }
  103. }
  104. else
  105. {
  106. // Draw player
  107. DrawRectangleRec((Rectangle){player.position.x, player.position.y, player.scale.x, player.scale.y}, GRAY);
  108. // Draw floor
  109. DrawRectangleRec((Rectangle){floor.position.x, floor.position.y, floor.scale.x, floor.scale.y}, BLACK);
  110. }
  111. EndDrawing();
  112. //----------------------------------------------------------------------------------
  113. }
  114. // De-Initialization
  115. //--------------------------------------------------------------------------------------
  116. CloseWindow(); // Close window and OpenGL context
  117. //--------------------------------------------------------------------------------------
  118. return 0;
  119. }