0
\$\begingroup\$

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

Example of capsule chain

  • 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.

\$\endgroup\$
2
  • \$\begingroup\$ As more of a generic programming question, I believe this question would belong on either the Code Review Stack Exchange or perhaps Stack Overflow. \$\endgroup\$
    – user35344
    Commented Oct 15, 2021 at 17:28
  • \$\begingroup\$ That's a good point, I wasn't sure where to post this. \$\endgroup\$ Commented Oct 15, 2021 at 17:37

0

You must log in to answer this question.

Browse other questions tagged .