I am using odin and raylib. I would like to know where the mouse position hits something in my 3d world. I can see, that raylib 4.2 itself should have a GetMouseRay function, but does not exist when trying from odin. Ref: https://www.raylib.com/cheatsheet/raylib_cheatsheet_v4.2.pdf
How do others in here find the mouse position in a 3d world?
#Find out where mouse cursor hits something in my 3d world (raylib)
1 messages · Page 1 of 1 (latest)
Odin's Raylib bindings are for 5. If you check the cheatsheet, you'll see:
#define GetMouseRay GetScreenToWorldRay // Compatibility hack for previous raylib versions
In short: The function is called GetScreenToWorldRay now
ohhhh, thank you ! 🙂
In case others stumble upon this thread. The solution for a 3d scenario:
ray := rl.GetScreenToWorldRay(rl.GetMousePosition(), camera); // camera is a 3d camera
and an example of collision detection: https://www.raylib.com/examples/core/loader.html?name=core_3d_picking
Fully working example for the next one to grab:
package game
import rl "vendor:raylib"
main :: proc() {
rl.InitWindow(1280, 720, "My First Game")
camera := rl.Camera3D{
position = {20, 20, 20},
target = {0, 0, 0},
up = {0, 1, 0},
fovy = 45,
projection = rl.CameraProjection.PERSPECTIVE,
}
cubePosition := rl.Vector3{ 0.0, 1.0, 0.0 };
cubeSize := rl.Vector3{ 2.0, 2.0, 2.0 };
collision :=rl.RayCollision {};
for !rl.WindowShouldClose() {
ray := rl.GetScreenToWorldRay(rl.GetMousePosition(), camera);
// Check collision between ray and box
collision := rl.GetRayCollisionBox(ray,
(rl.BoundingBox){(rl.Vector3){ cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2 },
(rl.Vector3){ cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 }});
time := rl.GetFrameTime();
rl.BeginDrawing()
rl.ClearBackground(rl.BLUE)
color := collision.hit ? rl.RED : rl.GRAY;
rl.BeginMode3D(camera)
rl.DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, color);
rl.DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, rl.MAROON);
rl.DrawCubeWires(cubePosition, cubeSize.x + 0.2, cubeSize.y + 0.2, cubeSize.z + 0.2, rl.BLACK)
rl.EndMode3D();
rl.DrawText("Nice game", 10, 10, 20, rl.DARKGRAY)
rl.EndDrawing()
}
rl.CloseWindow()
}