Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions Assets/ZestKit/Splines/AbstractSplineSolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ public abstract class AbstractSplineSolver
{
protected List<Vector3> _nodes;
public List<Vector3> nodes { get { return _nodes; } }
protected float _pathLength;

protected List<Vector3> _nodesWithDivisions;
public List<Vector3> nodesWithDivisions { get { return _nodesWithDivisions; } }


protected float _pathLength;

public float pathLength
{
Expand All @@ -18,10 +23,58 @@ public float pathLength
}
}

public float allPathLength
{
get
{
return _nodesWithDivisions.Count;
}
}

//get the distance from the beginning to a point
public float getPathLength(int endPoint)
{
float length = 0f;
var totalSudivisions = _nodes.Count * totalSubdivisionsPerNodeForLookupTable;
float timePerSlice = 1f / totalSudivisions;
Vector3 lastPoint = getPoint(0);

for (int i = 1; i < endPoint + 1; i++)
{
float currentTime = timePerSlice * i;
var currentPoint = getPoint(currentTime);
length += Vector3.Distance(currentPoint, lastPoint);
lastPoint = currentPoint;
}

return length;
}

//get the distance from the initpoint to the endpoint we want
//this points are NOT the waypoints or nodes, these are the subdivisions
public float getPathLength(int initPoint, int endPoint, float normalizedInitPos)
{
float length = 0f;
var totalSudivisions = _nodes.Count * totalSubdivisionsPerNodeForLookupTable;
float timePerSlice = 1f / totalSudivisions;
//get the initial point to work with
Vector3 lastPoint = getPoint(normalizedInitPos);

for (int i = initPoint+1; i < endPoint + 1; i++)
{
float currentTime = timePerSlice * i;
var currentPoint = getPoint(currentTime);
length += Vector3.Distance(currentPoint, lastPoint);
lastPoint = currentPoint;
}

return length;
}


// how many subdivisions should we divide each segment into? higher values take longer to build and lookup but
// result in closer to actual constant velocity
protected int totalSubdivisionsPerNodeForLookupTable = 5;
// how many subdivisions should we divide each segment into? higher values take longer to build and lookup but
// result in closer to actual constant velocity
protected int totalSubdivisionsPerNodeForLookupTable = 5;
protected Dictionary<float, float> _segmentTimeForDistance; // holds data in the form [time:distance] as a lookup table


Expand All @@ -36,6 +89,7 @@ public virtual void buildPath()
// we dont care about the first node for distances because they are always t:0 and len:0
_segmentTimeForDistance = new Dictionary<float, float>( totalSudivisions );

_nodesWithDivisions = new List<Vector3>();

var lastPoint = getPoint( 0 );

Expand All @@ -49,6 +103,7 @@ public virtual void buildPath()
_pathLength += Vector3.Distance( currentPoint, lastPoint );
lastPoint = currentPoint;

_nodesWithDivisions.Add(currentPoint);
_segmentTimeForDistance.Add( currentTime, _pathLength );
}
}
Expand Down
44 changes: 31 additions & 13 deletions Assets/ZestKit/Splines/Spline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class Spline
// used by the visual path editor
public List<Vector3> nodes { get { return _solver.nodes; } }

private bool _isReversed; // internal flag that lets us know if our nodes are reversed or not
public List<Vector3> nodesWithDivisions { get { return _solver.nodesWithDivisions; } }

private bool _isReversed; // internal flag that lets us know if our nodes are reversed or not
private AbstractSplineSolver _solver;

public float pathLength
Expand All @@ -35,14 +37,24 @@ public float pathLength
}


/// <summary>
/// generates an arc from start to end with the arc axis perpendicular to start and end points
/// </summary>
/// <returns>The arc.</returns>
/// <param name="start">Start.</param>
/// <param name="end">End.</param>
/// <param name="curvature">how far away from the line from start to end the arc extends</param>
public static Spline generateArc( Vector3 start, Vector3 end, float curvature )
public float getPathLength(int endPoint)
{
return _solver.getPathLength(endPoint);
}


public float getPathLength(int initPoint, int endPoint, float normalizedInitPoint) {
return _solver.getPathLength(initPoint, endPoint, normalizedInitPoint);
}

/// <summary>
/// generates an arc from start to end with the arc axis perpendicular to start and end points
/// </summary>
/// <returns>The arc.</returns>
/// <param name="start">Start.</param>
/// <param name="end">End.</param>
/// <param name="curvature">how far away from the line from start to end the arc extends</param>
public static Spline generateArc( Vector3 start, Vector3 end, float curvature )
{
return Spline.generateArc( start, end, curvature, Vector3.Cross( start, end ) );
}
Expand Down Expand Up @@ -146,10 +158,16 @@ public Vector3 getLastNode()
}


/// <summary>
/// responsible for calculating total length, segmentStartLocations and segmentDistances
/// </summary>
public void buildPath()
public float allPathLength {
get {
return _solver.allPathLength;
}
}

/// <summary>
/// responsible for calculating total length, segmentStartLocations and segmentDistances
/// </summary>
public void buildPath()
{
_solver.buildPath();
}
Expand Down