So I've managed to build my curve to my liking, now i want to insert/remove CP's along the curve, to make it path around collisions.
I have the collisions working, but when i try to add/remove CP's, i screw up the order, and can't find a reliable way of figuring out what CP's to remove, and when to add.
PseudoCode wise:
Raycast 20 times along curve
if hit: add CP1 at hit point
if another CPx is too close, remove that CPx
Check areas around the hit point, move CP1 to an open area
continue down the curve
I Get all this working, but the CP1 ends up at the end of the Segment list, even if i set it to spawn after the closest point.
Part of the code handling adding/removing
Code:
int raycastResolution = 20;
float segmentLength = splineTotalDist/raycastResolution;
for(int i = 0; i<=raycastResolution; i++)
{
if(i!=raycastResolution)
{
thisInterp = spline.InterpolateByDistance(i*splineTotalDist/raycastResolution*1f);
nextInterp = spline.InterpolateByDistance((i+1f)*splineTotalDist/raycastResolution*1f);
//float tFdistance = spline.TFToDistance();
// float mag = (nextInterp-thisInterp).magnitude;
Ray interpolationRay = new Ray(thisInterp, (nextInterp-thisInterp));
RaycastHit hit;
// Debug.Log (interpolationRay.origin + " " + interpolationRay.direction*10+" Satan is real");
//If Sphere hits something
Debug.DrawRay(interpolationRay.origin, interpolationRay.direction*segmentLength, Color.magenta, 30);
if(Physics.SphereCast(interpolationRay,width*0.5f,out hit,segmentLength,rayCastMask))
// if(Physics.Raycast(interpolationRay,out hit, segmentLength*1.1f, rayCastMask))
{
float closestInterpPoint = spline.GetNearestPointTF(hit.point);
// Debug.Log (hit.point);
Debug.DrawRay(spline.Interpolate(closestInterpPoint), Vector3.up*5, Color.magenta, 45);
//TODO Add detection for if there are no Controlpoints nearby, create one and edit the curve.
Color ccTYpe = Color.white;
CurvySplineSegment oldSegment = spline.TFToSegment(closestInterpPoint);
CurvySplineSegment cc = spline.Add (oldSegment);
//Grab the previous segment if there was no next segment
// if(oldSegment==null)
// {
// oldSegment = spline.PreviousSegment(spline.TFToSegment(closestInterpPoint));
// cc
// }
// else
// cc = spline.Add (true, oldSegment);
cc.Position = hit.point;
if(oldSegment!=null)
{
float distanceToClosestCC = Mathf.Abs(closestInterpPoint-oldSegment.LocalFToTF(0));
// Debug.Log (distanceToClosestCC + "Is this distance to closest CC");
//if the distance to the precviousCVis less than 35 units, delete
if(distanceToClosestCC<0.25f)
{
Debug.DrawRay(oldSegment.Position, Vector3.up*5, Color.black, 30);
cc.ConnectTo(oldSegment.PreviousControlPoint);
cc.ConnectTo(oldSegment.NextControlPoint);
oldSegment.ClearConnections();
oldSegment.Delete();
}
//
// spline.RefreshImmediately();
//Debug.DrawRay(cc.Position, Vector3.up*5,ccTYpe);
}
//Debug.DrawRay(cc.Position, Vector3.up*5, Color.yellow, 45);
Vector3 newPosition = FindOpenSpace(cc);
cc.Position = newPosition;
}
else
Debug.DrawRay(interpolationRay.origin, interpolationRay.direction*segmentLength, Color.green, 30);
}
}