core_3d_picking.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [core] example - Picking in 3d mode
  4. --
  5. -- This example has been created using raylib 1.6 (www.raylib.com)
  6. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. --
  8. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  9. --
  10. -------------------------------------------------------------------------------------------
  11. -- Initialization
  12. -------------------------------------------------------------------------------------------
  13. local screenWidth = 800
  14. local screenHeight = 450
  15. InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking")
  16. -- Define the camera to look into our 3d world
  17. local camera = {}
  18. camera.position = Vector3(0.0, 10.0, 10.0) -- Camera position
  19. camera.target = Vector3(0.0, 0.0, 0.0) -- Camera looking at point
  20. camera.up = Vector3(0.0, 1.0, 0.0) -- Camera up vector (rotation towards target)
  21. camera.fovy = 45.0 -- Camera field-of-view Y
  22. local cubePosition = Vector3(0.0, 1.0, 0.0)
  23. local cubeSize = Vector3(2.0, 2.0, 2.0)
  24. local ray = Ray(Vector3(0, 0, 0), Vector3(0, 0, 0)) -- Picking line ray
  25. local collision = false
  26. SetCameraMode(camera, CameraMode.FREE) -- Set a free camera mode
  27. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  28. -------------------------------------------------------------------------------------------
  29. -- Main game loop
  30. while not WindowShouldClose() do -- Detect window close button or ESC key
  31. -- Update
  32. ---------------------------------------------------------------------------------------
  33. camera = UpdateCamera(camera) -- Update camera
  34. if (IsMouseButtonPressed(MOUSE.LEFT_BUTTON)) then
  35. -- NOTE: This function is NOT WORKING properly!
  36. ray = GetMouseRay(GetMousePosition(), camera)
  37. -- Check collision between ray and box
  38. collision = CheckCollisionRayBox(ray,
  39. BoundingBox(Vector3(cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2),
  40. Vector3(cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2)))
  41. --print("collision check:", collision)
  42. end
  43. ---------------------------------------------------------------------------------------
  44. -- Draw
  45. ---------------------------------------------------------------------------------------
  46. BeginDrawing()
  47. ClearBackground(RAYWHITE)
  48. Begin3dMode(camera)
  49. if (collision) then
  50. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, RED)
  51. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, MAROON)
  52. DrawCubeWires(cubePosition, cubeSize.x + 0.2, cubeSize.y + 0.2, cubeSize.z + 0.2, GREEN)
  53. else
  54. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY)
  55. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY)
  56. end
  57. DrawRay(ray, MAROON)
  58. DrawGrid(10, 1.0)
  59. End3dMode()
  60. DrawText("Try selecting the box with mouse!", 240, 10, 20, DARKGRAY)
  61. if (collision) then
  62. DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30))/2, screenHeight*0.1, 30, GREEN)
  63. end
  64. DrawFPS(10, 10)
  65. EndDrawing()
  66. ---------------------------------------------------------------------------------------
  67. end
  68. -- De-Initialization
  69. -------------------------------------------------------------------------------------------
  70. CloseWindow() -- Close window and OpenGL context
  71. -------------------------------------------------------------------------------------------