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.

102 lines
4.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Picking in 3d mode
  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()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking");
  19. // Define the camera to look into our 3d world
  20. Camera camera;
  21. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  22. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  23. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  24. camera.fovy = 45.0f; // Camera field-of-view Y
  25. Vector3 cubePosition = { 0.0f, 1.0f, 0.0f };
  26. Vector3 cubeSize = { 2.0f, 2.0f, 2.0f };
  27. Ray ray = {0.0f, 0.0f, 0.0f}; // Picking line ray
  28. bool collision = false;
  29. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  30. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  31. //--------------------------------------------------------------------------------------
  32. // Main game loop
  33. while (!WindowShouldClose()) // Detect window close button or ESC key
  34. {
  35. // Update
  36. //----------------------------------------------------------------------------------
  37. UpdateCamera(&camera); // Update camera
  38. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  39. {
  40. ray = GetMouseRay(GetMousePosition(), camera);
  41. // Check collision between ray and box
  42. collision = CheckCollisionRayBox(ray,
  43. (BoundingBox){(Vector3){ cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2 },
  44. (Vector3){ cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 }});
  45. }
  46. //----------------------------------------------------------------------------------
  47. // Draw
  48. //----------------------------------------------------------------------------------
  49. BeginDrawing();
  50. ClearBackground(RAYWHITE);
  51. Begin3dMode(camera);
  52. if (collision)
  53. {
  54. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, RED);
  55. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, MAROON);
  56. DrawCubeWires(cubePosition, cubeSize.x + 0.2f, cubeSize.y + 0.2f, cubeSize.z + 0.2f, GREEN);
  57. }
  58. else
  59. {
  60. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY);
  61. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY);
  62. }
  63. DrawRay(ray, MAROON);
  64. DrawGrid(10, 1.0f);
  65. End3dMode();
  66. DrawText("Try selecting the box with mouse!", 240, 10, 20, DARKGRAY);
  67. if(collision) DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, screenHeight * 0.1f, 30, GREEN);
  68. DrawFPS(10, 10);
  69. EndDrawing();
  70. //----------------------------------------------------------------------------------
  71. }
  72. // De-Initialization
  73. //--------------------------------------------------------------------------------------
  74. CloseWindow(); // Close window and OpenGL context
  75. //--------------------------------------------------------------------------------------
  76. return 0;
  77. }