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.

127 lines
6.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [physac] example - Basic rigidbody
  4. *
  5. * This example has been created using raylib 1.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. *
  9. * Compile example using:
  10. * cmd /c IF NOT EXIST pthreadGC2.dll copy C:\raylib\raylib\src\external\pthread\pthreadGC2.dll $(CURRENT_DIRECTORY) /Y
  11. *
  12. * Copyright (c) 2016 Victor Fisac and Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #define PHYSAC_IMPLEMENTATION
  17. #include "physac.h"
  18. #define MOVE_VELOCITY 5
  19. #define JUMP_VELOCITY 30
  20. int main()
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. int screenWidth = 800;
  25. int screenHeight = 450;
  26. InitWindow(screenWidth, screenHeight, "raylib [physac] example - basic rigidbody");
  27. InitPhysics((Vector2){ 0.0f, -9.81f/2 }); // Initialize physics module
  28. // Debug variables
  29. bool isDebug = false;
  30. // Create rectangle physic object
  31. PhysicBody rectangle = CreatePhysicBody((Vector2){ screenWidth*0.25f, screenHeight/2 }, 0.0f, (Vector2){ 75, 50 });
  32. rectangle->rigidbody.enabled = true; // Enable physic object rigidbody behaviour
  33. rectangle->rigidbody.applyGravity = true;
  34. rectangle->rigidbody.friction = 0.1f;
  35. rectangle->rigidbody.bounciness = 6.0f;
  36. // Create square physic object
  37. PhysicBody square = CreatePhysicBody((Vector2){ screenWidth*0.75f, screenHeight/2 }, 0.0f, (Vector2){ 50, 50 });
  38. square->rigidbody.enabled = true; // Enable physic object rigidbody behaviour
  39. square->rigidbody.applyGravity = true;
  40. square->rigidbody.friction = 0.1f;
  41. // Create walls physic objects
  42. PhysicBody floor = CreatePhysicBody((Vector2){ screenWidth/2, screenHeight*0.95f }, 0.0f, (Vector2){ screenWidth*0.9f, 100 });
  43. PhysicBody leftWall = CreatePhysicBody((Vector2){ 0.0f, screenHeight/2 }, 0.0f, (Vector2){ screenWidth*0.1f, screenHeight });
  44. PhysicBody rightWall = CreatePhysicBody((Vector2){ screenWidth, screenHeight/2 }, 0.0f, (Vector2){ screenWidth*0.1f, screenHeight });
  45. PhysicBody roof = CreatePhysicBody((Vector2){ screenWidth/2, screenHeight*0.05f }, 0.0f, (Vector2){ screenWidth*0.9f, 100 });
  46. // Create pplatform physic object
  47. PhysicBody platform = CreatePhysicBody((Vector2){ screenWidth/2, screenHeight*0.7f }, 0.0f, (Vector2){ screenWidth*0.25f, 20 });
  48. SetTargetFPS(60);
  49. //--------------------------------------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //----------------------------------------------------------------------------------
  55. // Check rectangle movement inputs
  56. if (IsKeyPressed('W')) rectangle->rigidbody.velocity.y = JUMP_VELOCITY;
  57. if (IsKeyDown('A')) rectangle->rigidbody.velocity.x = -MOVE_VELOCITY;
  58. else if (IsKeyDown('D')) rectangle->rigidbody.velocity.x = MOVE_VELOCITY;
  59. // Check square movement inputs
  60. if (IsKeyDown(KEY_UP) && square->rigidbody.isGrounded) square->rigidbody.velocity.y = JUMP_VELOCITY;
  61. if (IsKeyDown(KEY_LEFT)) square->rigidbody.velocity.x = -MOVE_VELOCITY;
  62. else if (IsKeyDown(KEY_RIGHT)) square->rigidbody.velocity.x = MOVE_VELOCITY;
  63. // Check debug switch input
  64. if (IsKeyPressed('P')) isDebug = !isDebug;
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(RAYWHITE);
  70. // Draw floor, roof and walls rectangles
  71. DrawRectangleRec(TransformToRectangle(floor->transform), DARKGRAY); // Convert transform values to rectangle data type variable
  72. DrawRectangleRec(TransformToRectangle(leftWall->transform), DARKGRAY);
  73. DrawRectangleRec(TransformToRectangle(rightWall->transform), DARKGRAY);
  74. DrawRectangleRec(TransformToRectangle(roof->transform), DARKGRAY);
  75. // Draw middle platform rectangle
  76. DrawRectangleRec(TransformToRectangle(platform->transform), DARKGRAY);
  77. // Draw physic objects
  78. DrawRectangleRec(TransformToRectangle(rectangle->transform), RED);
  79. DrawRectangleRec(TransformToRectangle(square->transform), BLUE);
  80. // Draw collider lines if debug is enabled
  81. if (isDebug)
  82. {
  83. DrawRectangleLines(floor->collider.bounds.x, floor->collider.bounds.y, floor->collider.bounds.width, floor->collider.bounds.height, GREEN);
  84. DrawRectangleLines(leftWall->collider.bounds.x, leftWall->collider.bounds.y, leftWall->collider.bounds.width, leftWall->collider.bounds.height, GREEN);
  85. DrawRectangleLines(rightWall->collider.bounds.x, rightWall->collider.bounds.y, rightWall->collider.bounds.width, rightWall->collider.bounds.height, GREEN);
  86. DrawRectangleLines(roof->collider.bounds.x, roof->collider.bounds.y, roof->collider.bounds.width, roof->collider.bounds.height, GREEN);
  87. DrawRectangleLines(platform->collider.bounds.x, platform->collider.bounds.y, platform->collider.bounds.width, platform->collider.bounds.height, GREEN);
  88. DrawRectangleLines(rectangle->collider.bounds.x, rectangle->collider.bounds.y, rectangle->collider.bounds.width, rectangle->collider.bounds.height, GREEN);
  89. DrawRectangleLines(square->collider.bounds.x, square->collider.bounds.y, square->collider.bounds.width, square->collider.bounds.height, GREEN);
  90. }
  91. // Draw help message
  92. DrawText("Use WASD to move rectangle and ARROWS to move square", screenWidth/2 - MeasureText("Use WASD to move rectangle and ARROWS to move square", 20)/2, screenHeight*0.075f, 20, LIGHTGRAY);
  93. DrawFPS(10, 10);
  94. EndDrawing();
  95. //----------------------------------------------------------------------------------
  96. }
  97. // De-Initialization
  98. //--------------------------------------------------------------------------------------
  99. ClosePhysics(); // Unitialize physics (including all loaded objects)
  100. CloseWindow(); // Close window and OpenGL context
  101. //--------------------------------------------------------------------------------------
  102. return 0;
  103. }