I am writing some ray-tracing code in C# for a game. The trace is supposed to beam from the camera view down towards wherever the mouse is pointing. While the ray-trace itself does happen correctly (I can tell from the output log), I am having a few issues beyond that and I have been looking for solutions for hours but could not find any. I have encountered a few plugin libraries in my search but I would like to avoid those as much as possible, and oddly enough looking through the code of one of them didn't provide me with a solution to these issues.
1) Finding the right functionality for detecting collision between the ray-trace and a physics object (static or otherwise) proves to be janky. For whatever reason the trace detects collision with 6 Variant colliders (key value paired) which I'm not sure how it does and does not log collision detection with what it's supposed to. I made sure of the layers, and they correspond, apparently the masks have no effect, but the layers do, and this janky collision detection only happens when layers correspond between the one I set in code and the one set on the collider of a given node object. When they don't correspond, no collision detection happens. The area it detects collision at all appears to be the point where the trace leaves the collider instead of the point where it's entering it.
2) Getting the point coordinates at which the collision happens when it does isn't something that I have been able to do.
3) Drawing a line to display where the trace happens also doesn't work. This is something I wanted to do just to make sure visually that the ray-trace is done correctly.
For reference, this is what the code looks like so far for the ray trace itself (works as intended).
float RayLength = 100f;
var camera3DNodes = GetTree().Root.GetNode<Node3D>("Node3D").FindChildren("Camera3D");
ForEachNode(camera3DNodes);
var camera3D = cameraNodes[0] as Camera3D;
GD.Print(camera3D.Name);
var from = camera3D.ProjectRayOrigin(eventMouseButton.Position);
GD.Print("Vector Start Position: " + from.X + ", " + from.Y + ", " + from.Z);
var to = from + camera3D.ProjectLocalRayNormal(eventMouseButton.Position) * RayLength;
GD.Print("Vector End Position: " + to.X + ", " + to.Y + ", " + to.Z);
Then this is the section for the former 2 issues. The second one I couldn't really get to until the first is working as intended.
Godot.Collections.Array<Rid> intersectionArray = new Godot.Collections.Array<Rid>();
var query = PhysicsRayQueryParameters3D.Create(from, to, 1, intersectionArray);
var collision = GetViewport().World3D.DirectSpaceState.IntersectRay(query);
And this for the last one. I will be honest, I could not figure out what the purpose of SetUV actually is or what it's meant to represent, so that might be contributing to the issue. For context, the "to" and "from" variables are set in code as shown previously.
var surfacetool = new SurfaceTool();
surfacetool.Begin(Mesh.PrimitiveType.Lines);
surfacetool.SetColor(new Color(255, 0, 0));
surfacetool.SetUV(new Vector2(0, 0));
surfacetool.AddVertex(from);
surfacetool.AddVertex(to);
Also excuse the bad architecturing of this, I know how to structure some of this better, but I've done this for the moment while I'm getting used to syntax as I'm new to Godot.