core_3d_picking.bmx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. SuperStrict
  2. Framework Ray.Lib
  3. ' Initialization
  4. '--------------------------------------------------------------------------------------
  5. Const screenWidth:Int = 800
  6. Const screenHeight:Int = 450
  7. InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking")
  8. ' Define the camera to look into our 3d world
  9. Local camera:RCamera
  10. camera.position = New RVector3(10.0, 10.0, 10.0) ' Camera position
  11. camera.target = New RVector3(0.0, 0.0, 0.0) ' Camera looking at point
  12. camera.up = New RVector3(0.0, 1.0, 0.0) ' Camera up vector (rotation towards target)
  13. camera.fovy = 45.0 ' Camera field-of-view Y
  14. camera.projection = CAMERA_PERSPECTIVE ' Camera mode type
  15. Local cubePosition:RVector3 = New RVector3(0.0, 1.0, 0.0)
  16. Local cubeSize:RVector3 = New RVector3(2.0, 2.0, 2.0)
  17. Local ray:RRay ' Picking line ray
  18. Local collision:RRayCollision ' Ray collision hit info
  19. SetTargetFPS(60) ' Set our game to run at 60 frames-per-second
  20. '--------------------------------------------------------------------------------------
  21. ' Main game loop
  22. While Not WindowShouldClose() ' Detect window close button or ESC key
  23. ' Update
  24. '----------------------------------------------------------------------------------
  25. If IsCursorHidden() Then
  26. UpdateCamera(camera, CAMERA_FIRST_PERSON)
  27. End If
  28. ' Toggle camera controls
  29. If IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) Then
  30. If IsCursorHidden() Then
  31. EnableCursor()
  32. Else
  33. DisableCursor()
  34. End If
  35. End If
  36. If IsMouseButtonPressed(MOUSE_LEFT_BUTTON) Then
  37. If Not collision.hit Then
  38. ray = GetScreenToWorldRay(GetMousePosition(), camera)
  39. ' Check collision between ray and box
  40. collision = GetRayCollisionBox(ray, ..
  41. New RBoundingBox(New RVector3(cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2), ..
  42. New RVector3(cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2)))
  43. Else
  44. collision.hit = False
  45. End If
  46. End If
  47. '----------------------------------------------------------------------------------
  48. ' Draw
  49. '----------------------------------------------------------------------------------
  50. BeginDrawing()
  51. ClearBackground(RAYWHITE)
  52. BeginMode3D(camera)
  53. If collision.hit Then
  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.2, cubeSize.y + 0.2, cubeSize.z + 0.2, GREEN)
  57. Else
  58. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY)
  59. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY)
  60. End If
  61. DrawRay(ray, MAROON)
  62. DrawGrid(10, 1.0)
  63. EndMode3D()
  64. DrawText("Try selecting the box with mouse!", 240, 10, 20, DARKGRAY)
  65. If collision.hit Then
  66. DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, Int(screenHeight * 0.1), 30, GREEN)
  67. End If
  68. DrawText("Right click mouse to toggle camera controls", 10, 430, 10, GRAY)
  69. DrawFPS(10, 10)
  70. EndDrawing()
  71. '----------------------------------------------------------------------------------
  72. Wend
  73. ' De-Initialization
  74. '--------------------------------------------------------------------------------------
  75. CloseWindow() ' Close window and OpenGL context
  76. '--------------------------------------------------------------------------------------