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.

134 lines
7.0 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. * NOTE:
  9. * Physac requires multi-threading, when InitPhysics() a second thread is created to manage
  10. * physics calculations. To accomplish that, physac uses pthread Win32 library that can be
  11. * found inside raylib/src/external/pthread directory.
  12. *
  13. * Add pthread library when compiling physac example:
  14. * gcc -o $(NAME_PART).exe $(FILE_NAME) $(RAYLIB_DIR)\raylib_icon -L../src/external/pthread/lib \
  15. * -I../src -I../src/external/pthread/include -lraylib -lglfw3 -lopengl32 -lgdi32 -lpthreadGC2 -std=c99 -Wall
  16. *
  17. * Note that pthreadGC2.dll must be also copied to project directory!
  18. *
  19. * Copyright (c) 2016 Victor Fisac and Ramon Santamaria (@raysan5)
  20. *
  21. ********************************************************************************************/
  22. #include "raylib.h"
  23. #define PHYSAC_IMPLEMENTATION
  24. #include "physac.h"
  25. #define MOVE_VELOCITY 5
  26. #define JUMP_VELOCITY 30
  27. int main()
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. int screenWidth = 800;
  32. int screenHeight = 450;
  33. InitWindow(screenWidth, screenHeight, "raylib [physac] example - basic rigidbody");
  34. InitPhysics((Vector2){ 0.0f, -9.81f/2 }); // Initialize physics module
  35. // Debug variables
  36. bool isDebug = false;
  37. // Create rectangle physic object
  38. PhysicBody rectangle = CreatePhysicBody((Vector2){ screenWidth*0.25f, screenHeight/2 }, 0.0f, (Vector2){ 75, 50 });
  39. rectangle->rigidbody.enabled = true; // Enable physic object rigidbody behaviour
  40. rectangle->rigidbody.applyGravity = true;
  41. rectangle->rigidbody.friction = 0.1f;
  42. rectangle->rigidbody.bounciness = 6.0f;
  43. // Create square physic object
  44. PhysicBody square = CreatePhysicBody((Vector2){ screenWidth*0.75f, screenHeight/2 }, 0.0f, (Vector2){ 50, 50 });
  45. square->rigidbody.enabled = true; // Enable physic object rigidbody behaviour
  46. square->rigidbody.applyGravity = true;
  47. square->rigidbody.friction = 0.1f;
  48. // Create walls physic objects
  49. PhysicBody floor = CreatePhysicBody((Vector2){ screenWidth/2, screenHeight*0.95f }, 0.0f, (Vector2){ screenWidth*0.9f, 100 });
  50. PhysicBody leftWall = CreatePhysicBody((Vector2){ 0.0f, screenHeight/2 }, 0.0f, (Vector2){ screenWidth*0.1f, screenHeight });
  51. PhysicBody rightWall = CreatePhysicBody((Vector2){ screenWidth, screenHeight/2 }, 0.0f, (Vector2){ screenWidth*0.1f, screenHeight });
  52. PhysicBody roof = CreatePhysicBody((Vector2){ screenWidth/2, screenHeight*0.05f }, 0.0f, (Vector2){ screenWidth*0.9f, 100 });
  53. // Create pplatform physic object
  54. PhysicBody platform = CreatePhysicBody((Vector2){ screenWidth/2, screenHeight*0.7f }, 0.0f, (Vector2){ screenWidth*0.25f, 20 });
  55. SetTargetFPS(60);
  56. //--------------------------------------------------------------------------------------
  57. // Main game loop
  58. while (!WindowShouldClose()) // Detect window close button or ESC key
  59. {
  60. // Update
  61. //----------------------------------------------------------------------------------
  62. // Check rectangle movement inputs
  63. if (IsKeyPressed('W')) rectangle->rigidbody.velocity.y = JUMP_VELOCITY;
  64. if (IsKeyDown('A')) rectangle->rigidbody.velocity.x = -MOVE_VELOCITY;
  65. else if (IsKeyDown('D')) rectangle->rigidbody.velocity.x = MOVE_VELOCITY;
  66. // Check square movement inputs
  67. if (IsKeyDown(KEY_UP) && square->rigidbody.isGrounded) square->rigidbody.velocity.y = JUMP_VELOCITY;
  68. if (IsKeyDown(KEY_LEFT)) square->rigidbody.velocity.x = -MOVE_VELOCITY;
  69. else if (IsKeyDown(KEY_RIGHT)) square->rigidbody.velocity.x = MOVE_VELOCITY;
  70. // Check debug switch input
  71. if (IsKeyPressed('P')) isDebug = !isDebug;
  72. //----------------------------------------------------------------------------------
  73. // Draw
  74. //----------------------------------------------------------------------------------
  75. BeginDrawing();
  76. ClearBackground(RAYWHITE);
  77. // Draw floor, roof and walls rectangles
  78. DrawRectangleRec(TransformToRectangle(floor->transform), DARKGRAY); // Convert transform values to rectangle data type variable
  79. DrawRectangleRec(TransformToRectangle(leftWall->transform), DARKGRAY);
  80. DrawRectangleRec(TransformToRectangle(rightWall->transform), DARKGRAY);
  81. DrawRectangleRec(TransformToRectangle(roof->transform), DARKGRAY);
  82. // Draw middle platform rectangle
  83. DrawRectangleRec(TransformToRectangle(platform->transform), DARKGRAY);
  84. // Draw physic objects
  85. DrawRectangleRec(TransformToRectangle(rectangle->transform), RED);
  86. DrawRectangleRec(TransformToRectangle(square->transform), BLUE);
  87. // Draw collider lines if debug is enabled
  88. if (isDebug)
  89. {
  90. DrawRectangleLines(floor->collider.bounds.x, floor->collider.bounds.y, floor->collider.bounds.width, floor->collider.bounds.height, GREEN);
  91. DrawRectangleLines(leftWall->collider.bounds.x, leftWall->collider.bounds.y, leftWall->collider.bounds.width, leftWall->collider.bounds.height, GREEN);
  92. DrawRectangleLines(rightWall->collider.bounds.x, rightWall->collider.bounds.y, rightWall->collider.bounds.width, rightWall->collider.bounds.height, GREEN);
  93. DrawRectangleLines(roof->collider.bounds.x, roof->collider.bounds.y, roof->collider.bounds.width, roof->collider.bounds.height, GREEN);
  94. DrawRectangleLines(platform->collider.bounds.x, platform->collider.bounds.y, platform->collider.bounds.width, platform->collider.bounds.height, GREEN);
  95. DrawRectangleLines(rectangle->collider.bounds.x, rectangle->collider.bounds.y, rectangle->collider.bounds.width, rectangle->collider.bounds.height, GREEN);
  96. DrawRectangleLines(square->collider.bounds.x, square->collider.bounds.y, square->collider.bounds.width, square->collider.bounds.height, GREEN);
  97. }
  98. // Draw help message
  99. 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);
  100. DrawFPS(10, 10);
  101. EndDrawing();
  102. //----------------------------------------------------------------------------------
  103. }
  104. // De-Initialization
  105. //--------------------------------------------------------------------------------------
  106. ClosePhysics(); // Unitialize physics (including all loaded objects)
  107. CloseWindow(); // Close window and OpenGL context
  108. //--------------------------------------------------------------------------------------
  109. return 0;
  110. }