Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

125 wiersze
5.2 KiB

11 miesięcy temu
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Detect basic 3d collisions (box vs sphere vs box)
  4. *
  5. * Example originally created with raylib 1.3, last time updated with raylib 3.5
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2015-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. //------------------------------------------------------------------------------------
  15. // Program main entry point
  16. //------------------------------------------------------------------------------------
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. InitWindow(screenWidth, screenHeight, "raylib [models] example - box collisions");
  24. // Define the camera to look into our 3d world
  25. Camera camera = { { 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  26. Vector3 playerPosition = { 0.0f, 1.0f, 2.0f };
  27. Vector3 playerSize = { 1.0f, 2.0f, 1.0f };
  28. Color playerColor = GREEN;
  29. Vector3 enemyBoxPos = { -4.0f, 1.0f, 0.0f };
  30. Vector3 enemyBoxSize = { 2.0f, 2.0f, 2.0f };
  31. Vector3 enemySpherePos = { 4.0f, 0.0f, 0.0f };
  32. float enemySphereSize = 1.5f;
  33. bool collision = false;
  34. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  35. //--------------------------------------------------------------------------------------
  36. // Main game loop
  37. while (!WindowShouldClose()) // Detect window close button or ESC key
  38. {
  39. // Update
  40. //----------------------------------------------------------------------------------
  41. // Move player
  42. if (IsKeyDown(KEY_RIGHT)) playerPosition.x += 0.2f;
  43. else if (IsKeyDown(KEY_LEFT)) playerPosition.x -= 0.2f;
  44. else if (IsKeyDown(KEY_DOWN)) playerPosition.z += 0.2f;
  45. else if (IsKeyDown(KEY_UP)) playerPosition.z -= 0.2f;
  46. collision = false;
  47. // Check collisions player vs enemy-box
  48. if (CheckCollisionBoxes(
  49. (BoundingBox){(Vector3){ playerPosition.x - playerSize.x/2,
  50. playerPosition.y - playerSize.y/2,
  51. playerPosition.z - playerSize.z/2 },
  52. (Vector3){ playerPosition.x + playerSize.x/2,
  53. playerPosition.y + playerSize.y/2,
  54. playerPosition.z + playerSize.z/2 }},
  55. (BoundingBox){(Vector3){ enemyBoxPos.x - enemyBoxSize.x/2,
  56. enemyBoxPos.y - enemyBoxSize.y/2,
  57. enemyBoxPos.z - enemyBoxSize.z/2 },
  58. (Vector3){ enemyBoxPos.x + enemyBoxSize.x/2,
  59. enemyBoxPos.y + enemyBoxSize.y/2,
  60. enemyBoxPos.z + enemyBoxSize.z/2 }})) collision = true;
  61. // Check collisions player vs enemy-sphere
  62. if (CheckCollisionBoxSphere(
  63. (BoundingBox){(Vector3){ playerPosition.x - playerSize.x/2,
  64. playerPosition.y - playerSize.y/2,
  65. playerPosition.z - playerSize.z/2 },
  66. (Vector3){ playerPosition.x + playerSize.x/2,
  67. playerPosition.y + playerSize.y/2,
  68. playerPosition.z + playerSize.z/2 }},
  69. enemySpherePos, enemySphereSize)) collision = true;
  70. if (collision) playerColor = RED;
  71. else playerColor = GREEN;
  72. //----------------------------------------------------------------------------------
  73. // Draw
  74. //----------------------------------------------------------------------------------
  75. BeginDrawing();
  76. ClearBackground(RAYWHITE);
  77. BeginMode3D(camera);
  78. // Draw enemy-box
  79. DrawCube(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, GRAY);
  80. DrawCubeWires(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, DARKGRAY);
  81. // Draw enemy-sphere
  82. DrawSphere(enemySpherePos, enemySphereSize, GRAY);
  83. DrawSphereWires(enemySpherePos, enemySphereSize, 16, 16, DARKGRAY);
  84. // Draw player
  85. DrawCubeV(playerPosition, playerSize, playerColor);
  86. DrawGrid(10, 1.0f); // Draw a grid
  87. EndMode3D();
  88. DrawText("Move player with arrow keys to collide", 220, 40, 20, GRAY);
  89. DrawFPS(10, 10);
  90. EndDrawing();
  91. //----------------------------------------------------------------------------------
  92. }
  93. // De-Initialization
  94. //--------------------------------------------------------------------------------------
  95. CloseWindow(); // Close window and OpenGL context
  96. //--------------------------------------------------------------------------------------
  97. return 0;
  98. }