Good news, I figured out why things broke when I fixed the function.
The problem was two fold:
Code: Select all
if (Reject1 && Reject2)
{
Log("Both rejected");
Return 0;
}
else if (Reject1 && !Reject2)
{
Log("Rejected #1");
Intersection1 = Intersection2;
Return 1;
}
else if (!Reject1 && Reject2)
{
Log("Rejected #2");
Return 1;
}
else
{
Log("No rejection");
Return 2;
}
Which wouldn't have been a bad thing except for the way I was using the function:
Code: Select all
for (I=1; I<50; I++)
{
Log("***********************************");
Log("Processing TestBody["$I$"].");
SphereOrigin = TestBody[I-1].Location;
While (FindSphereIntersection(UnCompassPoints[LastSegment+1], UnCompassPoints[LastSegment], SphereOrigin, SDist, Intersection1, Intersection2) < 1)
{
LastSegment++;
Assert(LastSegment < 64); //Testing.
Log("While loop forced increment. LastSegment = "$LastSegment$".");
}
// DrawDebugLine(Vect(0,0,0),Intersection1, Green);
// DrawDebugSphere(Intersection1, 2, 4, Green);
TestBody[I].SetLocation(Intersection1);
}
What this dose is feed in line segments until >=1 is returned, a 1 is returned when one intersection point is valid, a two is returned when both points are valid. Then we place a body segment and move onto the next.
So what dose this mean?
Using the broken function always made Intersection2 be rejected, so a 1 was returned (when Intersection1 was valid) or a 0 was returned (when Intersection1 was invalid).
After fixing the function, with Intersection2 no longer being constantly rejected this happened:
Code: Select all
else if (Reject1 && !Reject2)
{
Log("Rejected #1");
Intersection1 = Intersection2;
Return 1;
}
And everything broke.
Essentially, what happens in the code is if Intersection2 is positive it has hit the line in the wrong direction. I didn't take this into account (bad ExpEM) so the next body segment was placed the wrong way down the line on top of another segment, then the next one would be placed correctly from the wrong location, so also on top of another segment. And then we have a loop of "place backwards, place forwards, place backwards, place forwards".
And so the fix:
Code: Select all
else if (Reject1 && !Reject2)
{
Log("Rejected #1");
Return 0;
}
Return 0 not 1.
This fix is specific to how I am using the function where I now know that Intersection2 should always be discarded.
For general purpose this would be more appropriate:
Code: Select all
if (Reject1 && Reject2)
Return 0; //No hit.
else if (Reject1 && !Reject2)
Return 1; //Intersection2 hit.
else if (!Reject1 && Reject2)
Return 2; //Intersection1 hit.
else
Return 3; //Both hit.
Signature goes here.