2024-08-20 00:50:55 +02:00
|
|
|
|
|
|
|
using Godot;
|
|
|
|
|
|
|
|
namespace Gmtk24 {
|
|
|
|
public static class Util {
|
2024-08-20 02:33:29 +02:00
|
|
|
public static RaycastResult? RaycastFromMouse(Camera3D camera, uint mask = 0b11000) {
|
2024-08-20 00:50:55 +02:00
|
|
|
var viewport = camera.GetViewport();
|
|
|
|
var origin = camera.ProjectRayOrigin(viewport.GetMousePosition());
|
|
|
|
var normal = camera.ProjectRayNormal(viewport.GetMousePosition()) * 10000;
|
|
|
|
var ray = new PhysicsRayQueryParameters3D {
|
|
|
|
From = origin,
|
|
|
|
To = normal,
|
|
|
|
CollideWithBodies = true,
|
|
|
|
CollisionMask = mask,
|
|
|
|
HitBackFaces = false,
|
|
|
|
HitFromInside = false,
|
|
|
|
};
|
|
|
|
var results = camera.GetWorld3D().DirectSpaceState.IntersectRay(ray);
|
|
|
|
if (results.ContainsKey("position")) {
|
|
|
|
return new RaycastResult() {
|
|
|
|
Collider = (GodotObject)results["collider"],
|
|
|
|
Position = (Vector3)results["position"],
|
|
|
|
Normal = (Vector3)results["normal"],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public struct RaycastResult {
|
|
|
|
public Vector3 Position;
|
|
|
|
public Vector3 Normal;
|
|
|
|
public GodotObject Collider;
|
|
|
|
}
|
|
|
|
}
|