- Home /
Problem with curved cornered spline that i need help with.
Hello i am trying to do this in Pic1 but i have been getting the error argument is out of range in my code(which is below Pic1) i do not know how to fix it, does anyone know how to fix this??
-Pic1
Here is my code so you know what i have done.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Spline : MonoBehaviour
{
[System.Serializable]
public class SplinePoint
{
public Transform t;
public Transform cpAhead;
public Transform cpBehind;
public int seg;
public SplinePoint(Transform thetrans, Transform a, Transform b, int s)
{
t = thetrans;
cpAhead = a;
cpBehind = b;
seg = s;
}
}
public List<Transform> controlPointsList;
public List<SplinePoint> splinePoints;
public Transform segment;
public int segCount = 2;
public int curveSegments = 3;
public float updateRate = 60f;
public bool setup = false;
public bool running = false;
void Start()
{
splinePoints = new List<SplinePoint>();
CreateObjects(segment);
}
public void CreateObjects(Transform original)
{
for (int i = 0; i < controlPointsList.Count - 1; i++)
{
for (int j = 0; j < segCount; j++)
{
var a = Instantiate(original, Vector3.zero, Quaternion.identity) as Transform;
splinePoints.Add(new SplinePoint(a.transform, controlPointsList[i + 1], controlPointsList[i], j));
}
}
setup = true;
}
void Update()
{
if (setup && !running)
{
UpdateSplinePos();
}
}
void UpdateSplinePos()
{
for (int i = 0; i < splinePoints.Count; i++)
{
SetPosition(splinePoints[i]);
}
}
SplinePoint GetSplinePoint(int s)
{
SplinePoint sp = null;
if (splinePoints.Count > 0)
{
for (int i = 0; i < splinePoints.Count; i++)
{
if (splinePoints[i].seg == s)
{
sp = splinePoints[i];
break;
}
}
}
return sp;
}
void OnDrawGizmos()
{
Gizmos.color = Color.white;
for (int i = 0; i < controlPointsList.Count; i++)
{
Gizmos.DrawWireSphere(controlPointsList[i].position, 0.3f);
}
for (int i = 0; i < controlPointsList.Count - 1; i++)
{
Gizmos.DrawLine(controlPointsList[i].position, controlPointsList[i + 1].position);
}
Gizmos.color = Color.red;
for (int i = 0; i < controlPointsList.Count - 1; i++)
{
List<SplinePoint> associatedNodes = NodesAssociated(controlPointsList[i]);
List<SplinePoint> associatedNodesAhead = NodesAssociated(controlPointsList[i + 1]);
Vector3 p0 = associatedNodes[(i - 1)].t.position;// point left
Vector3 p1 = controlPointsList[i].position;// Control point
Vector3 p2 = associatedNodesAhead[(i + 1)].t.position; // point right
Vector3 lastPos = Vector3.zero;
if (associatedNodes.Count > 0 && associatedNodesAhead.Count > 0)
{
Gizmos.DrawLine(associatedNodes[associatedNodes.Count - 1].t.position, associatedNodesAhead[0].t.position);
//Gizmos.DrawWireSphere(associatedNodes[associatedNodes.Count - 1].t.position, 1f);
//Gizmos.DrawWireSphere(associatedNodesAhead[0].t.position, 1f);
}
for (int n = 0; n < curveSegments; n++)
{
float t = (float)n / curveSegments;
Vector3 newPos = GetPoint(p0, p1, p2, t);
if (t == 0)
{
lastPos = newPos;
continue;
}
Gizmos.DrawLine(lastPos, newPos);
lastPos = newPos;
}
}
}
void SetPosition(SplinePoint sp)
{
float dist = Vector3.Distance(sp.cpAhead.position, sp.cpBehind.position);
float step = dist / (segCount + 1);
Vector3 dir = (sp.cpAhead.position - sp.cpBehind.position).normalized;
sp.t.position = sp.cpBehind.position + ((dir * step) * (sp.seg + 1));
sp.t.rotation = Quaternion.LookRotation(dir);
}
List<SplinePoint> NodesAssociated(Transform behindNode)
{
List<SplinePoint> p = new List<SplinePoint>();
for (int i = 0; i < splinePoints.Count; i++)
{
if (splinePoints[i].cpBehind == behindNode)
{
p.Add(splinePoints[i]);
}
}
return p;
}
public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, float t)
{
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
oneMinusT * oneMinusT * p0 +
2f * oneMinusT * t * p1 +
t * t * p2;
}
}
it looks like your problem is most likely lines 100 and 102 at line 100 you are trying to access element -1 of a newly created list. I think you should just use i ins$$anonymous$$d of i-1 and i+1
That kind of worked, as you can see in the picture the problem i am having, and i am also still getting the same error.
And i Changed this
Vector3 p0 = associatedNodes[(i - 1)].t.position;// point left
Vector3 p1 = controlPointsList[i].position;// Control point
Vector3 p2 = associatedNodesAhead[(i + 1)].t.position; // point right
To this(The results are in the picture :P)
Vector3 p0 = associatedNodes[i].t.position;// point left
Vector3 p1 = controlPointsList[i + 1].position;// Control point
Vector3 p2 = associatedNodesAhead[i].t.position; // point right
i am a bit confused with your code but maybe ins$$anonymous$$d of looping through the control points you should be looping through your spline points then p0 p1 and p2 can be cpBehind t and cpAhead
Answer by b1gry4n · Sep 04, 2016 at 03:31 AM
The null reference happens because of two things. One, "OnDrawGizmos" it is trying to iterate through the SplinePoint list, but it hasnt been set up yet. Add this as the first part of your "OnDrawGizmos" because we do not want to draw anything that isnt ready to be displayed.
if(!setup ){
return;
}
The second is you are trying to access a list element with i - 1. If i == 0, you are trying to get the -1 element of the list, which does not exist.
Also, you want to set the spline to start at the list[list.Count - 1].t.position and to end at the nextlist[0].t.position. The reason why we say "list.Count-1" is because in a list, say you have only 1 element... to reference that element we say list[0], but when you call "list.Count" it will return "1". It counted 1 element in your list, but you access it with list.Count-1. If you have 5 elements in the list, the reference for each element will be 0,1,2,3,4. list.Count will return 5 because it counted 5. To get the last element in the list, again, list.Count-1 == 4
Vector3 p0 = associatedNodes[associatedNodes.Count - 1].t.position;// point left
Vector3 p1 = controlPointsList[i + 1].position;// Control point
Vector3 p2 = associatedNodesAhead[0].t.position; // point right
An explanation of what is happening:
We cycle through all the control points, but we do not care about the very last control point as we will be referencing the + 1 control point throughout iterations and + 1 on the last controlpoint does not exist
Each control point has nodes associated with it. The amount of nodes spawned are controlled by the "segcount"
If we want to get any of the nodes relative to a control point, we need to collect a list of nodes that have the "cpbehind" transform as the current control point in the iteration.
to make a curve where you want we need the end node of one list and the start of the other. We reference the current control point, collect the nodes, retrieve the last node in the list. That will give us the start of the curve. The mid point of the curve will be the "ahead" controlpoint transform, not a node, which will be "whatever control point we are on right now + 1". The last node we want can be found by retrieving a list of nodes from the "controlpoint + 1" point and retrieving the first node of the list (aheadnodes[0]). Now you have your 3 points to form a spline
cpAhead exists for the purpose of object orientation and nothing more. When positioning it will use the cpbehind and the cpahead to figure out which direction it should face
If you want a simpler way to retrieve these 3 vectors, I wrote a new function. Screenshot provided to show working solution. Heres the entire script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SplineTest : MonoBehaviour {
[System.Serializable]
public class SplinePoint
{
public Transform t;
public Transform cpAhead;
public Transform cpBehind;
public int seg;
public SplinePoint(Transform thetrans, Transform a, Transform b, int s)
{
t = thetrans;
cpAhead = a;
cpBehind = b;
seg = s;
}
}
public List<Transform> controlPointsList;
public List<SplinePoint> splinePoints;
public GameObject segment;
public int curveSegments = 3;
public int segCount = 2;
public float updateRate = 0.25f;
public bool setup = false;
public bool running = false;
void Start()
{
splinePoints = new List<SplinePoint>();
CreateObjects(segment);
}
public void CreateObjects(GameObject original)
{
for (int i = 0; i < controlPointsList.Count - 1; i++)
{
for (int j = 0; j < segCount; j++)
{
GameObject a = (GameObject)Instantiate(original, Vector3.zero, Quaternion.identity);
splinePoints.Add(new SplinePoint(a.transform, controlPointsList[i + 1], controlPointsList[i], j));
}
}
setup = true;
}
void Update()
{
if (setup && !running)
{
running = true;
InvokeRepeating("UpdateSplinePos", 0, updateRate);
}
}
void UpdateSplinePos()
{
for (int i = 0; i < splinePoints.Count; i++)
{
SetPosition(splinePoints[i]);
}
}
SplinePoint GetSplinePoint(int s)
{
SplinePoint sp = null;
if (splinePoints.Count > 0)
{
for (int i = 0; i < splinePoints.Count; i++)
{
if (splinePoints[i].seg == s)
{
sp = splinePoints[i];
break;
}
}
}
else {
Debug.Log("No spline points");
}
return sp;
}
void SetPosition(SplinePoint sp)
{
float dist = Vector3.Distance(sp.cpAhead.position, sp.cpBehind.position);
float step = dist / (segCount + 1);
Vector3 dir = (sp.cpAhead.position - sp.cpBehind.position).normalized;
sp.t.position = sp.cpBehind.position + ((dir * step) * (sp.seg + 1));
sp.t.rotation = Quaternion.LookRotation(dir);
}
void OnDrawGizmos()
{
if (!setup)
{
return;
}
Gizmos.color = Color.white;
for (int i = 0; i < controlPointsList.Count; i++)
{
Gizmos.DrawWireSphere(controlPointsList[i].position, 0.3f);
}
for (int i = 0; i < controlPointsList.Count - 1; i++)
{
Gizmos.DrawLine(controlPointsList[i].position, controlPointsList[i + 1].position);
}
//Get a list of associated nodes relative to control point. We want the last of the current control point and the first of the next control point
for (int i = 0; i < controlPointsList.Count - 1; i++)
{
if (i == controlPointsList.Count - 2)
{
break;
}
//retrieve the 3 points of the curve
List<Vector3> curvepoints = SplineCurve(controlPointsList[i], controlPointsList[i + 1]);
Vector3 p0 = curvepoints[0];
Vector3 p1 = curvepoints[1];
Vector3 p2 = curvepoints[2];
//draw a line between the start of the curve and the end of the curve for reference
//Gizmos.color = Color.red;
// Gizmos.DrawLine(p0, p2);
//draw spheres at each point of the curve
//Debug.Log(curvepoints.Count);
// Gizmos.color = Color.green;
//Gizmos.DrawWireSphere(p0, 0.15f); // start
//Gizmos.color = Color.yellow;
//Gizmos.DrawWireSphere(p1, 0.15f); // mid
//Gizmos.color = Color.blue;
// Gizmos.DrawWireSphere(p2, 0.15f); // end
Vector3 lastPos = Vector3.zero;
Gizmos.color = Color.blue;
for (int n = 0; n < curveSegments + 1; n++)
{
float t = (float)n / curveSegments;
Vector3 newPos = GetPoint(p0, p1, p2, t);
if (t == 0)
{
lastPos = newPos;
continue;
}
Gizmos.DrawLine(lastPos, newPos);
lastPos = newPos;
}
}
}
List<SplinePoint> NodesAssociated(Transform behindNode)
{
List<SplinePoint> p = new List<SplinePoint>();
for (int i = 0; i < splinePoints.Count; i++)
{
if (splinePoints[i].cpBehind == behindNode)
{
p.Add(splinePoints[i]);
}
}
return p;
}
List<Vector3> SplineCurve(Transform current, Transform ahead)
{
List<Vector3> curvePoints = new List<Vector3>();
//Find the first point in the curve
for (int i = 0; i < splinePoints.Count; i++)
{
//we need the nodes associated with the current transform
//since we store the segment value in the spline point, all we need is the segment that has the max segment count stored
if (splinePoints[i].cpBehind == current && splinePoints[i].seg == segCount - 1)
{
curvePoints.Add(splinePoints[i].t.position);
break;
}
}
//the second point in the curve is the next control point
curvePoints.Add(ahead.position);
//Find the last point in the curve
for (int i = 0; i < splinePoints.Count; i++)
{
//we need the nodes associated with the ahead transform
//since we store the segment value in the spline point, all we need is the segment that has 0 as its seg
if (splinePoints[i].cpBehind == ahead && splinePoints[i].seg == 0)
{
curvePoints.Add(splinePoints[i].t.position);
break;
}
}
return curvePoints;
}
public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, float t)
{
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
oneMinusT * oneMinusT * p0 +
2f * oneMinusT * t * p1 +
t * t * p2;
}
}
Ah ok that explains that.
However there is still a $$anonymous$$or problem as you see in the picture below, the blue line(Curve) does not finish to the end point, if this is in the explanation of yours i may have misread it. Thanks again @b1gry4n.
Ah, i see what you mean. So now you do the opposite of what we did with the control points. Ins$$anonymous$$d of -1 you +1. Updated the answer with some new info
List<Vector3> curvepoints = SplineCurve(controlPointsList[i], controlPointsList[i + 1]);
Vector3 p0 = curvepoints[0];
Vector3 p1 = curvepoints[1];
Vector3 p2 = curvepoints[2];
Vector3 lastPos = Vector3.zero;
for (int n = 0; n < curveSegments + 1; n++)
{
float t = (float)n / curveSegments;
Vector3 newPos = GetPoint(p0, p1, p2, t);
if (t == 0)
{
lastPos = newPos;
continue;
}
Gizmos.DrawLine(lastPos, newPos);
lastPos = newPos;
}