I’ve made a method to find the intersection of two rays if it exists, however it isn’t working for some point-angle sets I give it. The method itself is this:
public static Godot.Vector2 Rays_FindIntersection(Godot.Vector2[] points, float[] angles)
{
Godot.Vector2[] angle_vectors =
[
Godot.Vector2.FromAngle(angles[0]),
Godot.Vector2.FromAngle(angles[1])
];
//rationale for using 2,048
//https://docs.godotengine.org/en/stable/tutorials/physics/large_world_coordinates.html#why-use-large-world-coordinates
Variant intersection = Geometry2D.SegmentIntersectsSegment
(
points[0],
points[0] + new Godot.Vector2(angle_vectors[0].X, - angle_vectors[0].Y) * 2048,
points[1],
points[1] + new Godot.Vector2(angle_vectors[1].X, - angle_vectors[1].Y) * 2048
);
if (intersection.VariantType == Variant.Type.Nil)
{
//error message and quit
}
return (Godot.Vector2) intersection;
}
It correctly finds the intersections of these sets of inputs:
points[0]
points[1]
angles[0]
angles[1]
(0.61, 0)
(0, -14.28)
1.57
6.28
(0, -14.28)
(-0.61, 0)
3.14
1.57
(-0.61, 0)
(0, 31.53)
-1.57
3.14
(0, 31.53)
(0.61, 0)
0
4.71
But errors as soon as it runs the first of these sets of inputs:
points[0]
points[1]
angles[0]
angles[1]
(0, -0.61)
(-14.28, 0)
3.14
7.85
(-14.28, 0)
(0, 0.61)
4.71
3.14
(0, 0.61)
(31.53, 0)
0
4.71
(31.53, 0)
(0, -0.61)
1.57
6.28
I’ve plugged everything into Desmos to visually check, and both tables should work properly, but only the first one does. I must be missing something, but I’m not sure what.
It is indeed the same method. You should draw those line segments in Godot to see how they are actually positioned in Godot’s coordinate space. There may be discrepancy between how Godot interprets angle values vs how Desmos does it. Can you post what you typed into Desmos?
I only put the points themselves into Desmos and traced the angles in my head, since they’re all on 90° angles. These are the tables, with the one at the working angle first and the not working angle second:
X
Y
0.61
0.00
0.00
14.28
-0.61
0.00
0.00
-31.53
X
Y
0.00
0.61
-14.28
0.00
0.00
-0.61
31.53
0.00
I only put in points[0] since points[1] is just the same array rotated down once. I inverted the Y-axes of the GD.Print() output when transferring to Desmos since Godot does negative Z toward the back and you can’t make negative Y up on Desmos, but that shouldn’t matter, since all the calculations for the first table work, right?
I’ll have to do more reconfiguring than I can tonight to draw the line segments in-engine. I should say, the point of this is to get the corners of a square whose edges are each centered on a known point and rotated at a known angle. Ray tracing seemed like the easiest way to do that, but if there’s an easier one I’m missing, let me know.
Could you show us the equations you’re using to plot these functions? Remember that vectors and functions are not the same. If the vectors point away from each other, they are not bound to intersect like functions are. It’s possible that the result you’re getting is actually correct.
Given how much trouble I’m having explaining myself, it might be easier to just upload an MRE.
I’m aware the camera script is (probably a bit excessively) complicated. I’ve left it untouched even though some functions could be trimmed for an MRE, just to be extra safe. Here is a basic explanation of how to reproduce the error in question:
Open the game. Using WASD to move the camera, observe that its bounds are calculated such that the it cannot show anywhere outside the room[1] using Update_Bounds(), and that the corners that define this area are printed in the Output window (line 477).[2]
Press the Tab key to zoom out. Note that the corners in the output window have changed, as the bounds have been recalculated to achieve the same result as in Step 1.[3]
Press either the Q or E key[4] to rotate the camera. The error in question will occur as the bounds are recalculated again, with Rays_FindIntersection() being called as per usual from line 479, in Update_CoordsOOB_Camera().
Also, It took me more than 10 minutes getting the skybox to work and I couldn’t, so I just gave up. I assume I’m missing something obvious, but that needs to be fixed to make sense of what’s on screen when the project runs. Sorry about that.
If you try setting Fixed_Rotate_Power to a greater number, you’ll notice it is possible to see outside the room at non-cardinal angles. This is intended behavior having to do with the way the area is calculated as a bounding box. ↩︎
You will notice a variable called “was_index_3” in this part. The bounds are programmed to be more strict moving the camera back into bounds than they are getting OOB, as a safety measure. Essentially, this is irrelevant to the problem at hand. ↩︎
Technically there’s some sleight of hand going on with regard to horizontal movement; see line 31. ↩︎
I’ve been testing this mainly by rotating with the Q key, but the crash should occur either way. ↩︎
That doesn’t sound minimal. Make a super simple test scene for that function only. Call it with bunch of values and plot the results (lines and intersection points) in 2D using _Draw(). Standard unit testing.
I doubt that SegmentIntersectsSegment() and consequently your function doesn’t work as it should though. The bug is likely somewhere else in your code.