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.

120 lines
4.9 KiB

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