Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Curvy for a "Ghost" player
#1
So I'm recording my player as they trek through my map's splines.  Each control point, I mark the Vector3 Position and a TimeStamp of the level time played (which is a float).  The original map will have hundreds of separate splines that dynamically join based on the choice of the player, however the GhostSpline is a single spline that freezes all the decisions the player made as a single path.  So far it's working like a charm.  Got it recording, saving to file, loading from file, and building the ghost spline dynamically from the file's position data.

Ok, so here's the fun part.  I'm going to make a Ghost that travels along this spline.  So here's a math question for you:


Code:
                                    X                             Y                              Z
CP0000 272.885009765625    29.2959995269775    238.434997558593
CP0001 249.352996826171    37.8520011901855    234.132003784179
Delta     -23.532012939454      8.556001663208     -4.30299377441401

Straight distance between CP0000/CP0001 is 25.40623 (using Pythagorean), and it took exactly 6.7206934094429 seconds for this distance.

Please note that I used a static 7.5 movement speed on the original spline.  Normally the player's speed will be variable, but I don't want to mark down movement speeds every frame since that would generate huge data files.  So the ghost isn't going to speed up/slow down between splines, but will get to the end of the spline at the same time the player did even if the player did speed up/slow down.

So with the above stats, I need to tell my ghost spline to move at a speed of 7.5 (or close to it).  If I do the same test again but with a movement speed of 10.0 (exact same coordinates above), it takes 5.270451307296753 seconds..

Now, I could probably use some of your build in functions to get a more accurate spline length between the two segments, as it curves so a straight line calc won't be accurate and why reinvent the wheel anyway. 

So, bottom line, say I deduce the distance between two control points, 25.4, and I want it to take 5.2 seconds to traverse that distance, what should my movement speed be?
Reply
#2
Hi,

To get the length of the segment between CP0000 and CP0001, you cal call CP000.Length. The Length is computed using linear approximations of the curve, so the less cache density your spline will have, the less precise Length will be.
So based on this, your movement speed should be CP000.Length / 5.2

Did this answer your question?
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#3
And since you build your spline dynamically, if you compute the speed the same frame you build the spline, make sure yourSpline.Refresh() is called before computing the speed, to give the spline the opportunity to store its cache and length
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#4
(04-08-2019, 12:38 PM)_Aka_ Wrote: Hi,

To get the length of the segment between CP0000 and CP0001, you cal call CP000.Length. The Length is computed using linear approximations of the curve, so the less cache density your spline will have, the less precise Length will be.
So based on this, your movement speed should be CP000.Length / 5.2

Did this answer your question?

Ok so this pretty much works, it's funny because the final result I don't get 7.5 but somewhere close to 7.5 each time, and that's even using the spline length as provided by curvy.  Weird.  One thing I realized is that the length needs to be grabbed from the previous spline because it's the length going forward (thus the last spline has a length of 0).

I wonder if this is the best way to grab that? 

Code:
float distanceLastSpline = ghostSpline.transform.GetChild(ghostSpline.transform.childCount - 2).GetComponent<CurvySplineSegment>().Length;

Seems pretty messy, but does the trick.

Basically will go to the object level, find how many children are there (with a 1 start, not 0 start), so -1 would be the last segment, and -2 is the second last segment. I can get the length here to get the second last segment to the last segment length.
Reply
#5
Code:
ghostSpline.transform.GetChild(ghostSpline.transform.childCount - 2).GetComponent<CurvySplineSegment>()
is equivalent to
Code:
ghostSpline[ghostSpline.Count - 1]
which is equivalent to
Code:
ghostSpline.Segments[ghostSpline.Count - 1]

And to avoid you confusion, when you say:
"One thing I realized is that the length needs to be grabbed from the previous spline because it's the length going forward (thus the last spline has a length of 0)."
What you speak about are not splines, but control points:
A spline has (except special cases) N control points, and N-1 segments. Each segment is the spline's chunk between two control points. Spline.ControlPointsList gives you the list of control points, and Spline.Segments gives you the list of segments. Both of them are instances of CurvySplineSegment classes
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Import Curvy from json Issue mlndb10wN 5 147 05-29-2019, 06:46 PM
Last Post: _Aka_
  Curvy Coppy all points of a Spline Into Another UsernameHed 3 199 05-13-2019, 11:37 AM
Last Post: _Aka_
  Curvy Offset with Lerp UsernameHed 2 145 05-10-2019, 07:36 AM
Last Post: UsernameHed
Information Curvy 5.2.2 is live _Aka_ 0 113 04-30-2019, 11:00 AM
Last Post: _Aka_

Forum Jump: