What I'm trying to do
I'm trying to create a list of positions which I can use to define a centripetal catmull–rom spline; however I can't think of a solution for creating the list of positions which doesn't seem brittle or hacked together.
A catmull–Rom spline with four points will only draw between p1 and p2:
p0 --- p1 ——— p2 --- p3
However I want to draw the spline between all points which means I need to include two more (ghost) points in the chain:
g0 --- p0 ——— p1 ——— p2 ——— p3 --- g1
The start and end of the capsule I refer to is where the cylinder part of the capsule starts & ends, similarly the 'ghost' start & end positions are the same except for a capsule collider which doesn't exist.
Example GIF
- The pink wire spheres represent the calculated ghostStart and ghostEnd positions of the first and last capsules.
- The white wire spheres are the calculated start positions of all the capsules (slightly offset for easier visibility).
- The yellow wire spheres are the calculated end position of all the capsules (slightly offset for easier visibility).
What I've tried
Currently I have the code below which does create a list of positions, including the two 'ghost' positions needed to draw the entire catmull-rom spline between all the points:
capsules.SelectMany (capsule => {
if (capsule == firstCapsule) return (new List<Vector3> { capsule.ghostStart, capsule.start, capsule.end });
if (capsule == lastCapsule) return (new List<Vector3> { capsule.start, capsule.end, capsule.ghostEnd });
return (new List<Vector3> { capsule.start, capsule.end });
});
However as each end position of the capsule is also the same as the start position of the next capsule, this causes the points to overlap which I think breaks the way the spline works.
I've tried returning either the start or end position of the capsule - however doing this means I always miss a position (either the start or end of the first or last capsule) unless I write a statement to add the missing point.
I've tried to remove duplicate positions in the list - however the positions in the list are never exactly the same Vector3 value and I have also tried to remove duplicate positions which were approximately the same - this didn't really work.
In conclusion
Instead of creating a list which look like this:
g0 --- p0 p1 ——— p2 p3 --- g1
p0 ——— p1 p2 ——— p3
How do I create a list which looks like this:
g0 --- p0 ——— p1 ——— p2 ——— p3 --- g1
Sorry, I know there is a lot of text to read for what is probably an extremely simple problem to most people but I don't know what I'm doing and I've been banging away at this for what feels like forever so I'd really appreciate any help any one can give me.
Cheers.