#`path path Length 1 `
1 messages · Page 1 of 1 (latest)
Something about the math in the GetPosAlongPath is funky, but it's a great start! I have looped the movement in a possibly naive way, but the numbers all look correct outside of the GetPositionAtPath:```c#
float age = Time.time - timeSpawned;
float distanceTravelled = age * moveSpeed;
float tripDistance = GetTripDistance(pointsOfInterest);
while(distanceTravelled > tripDistance)
{
distanceTravelled -= tripDistance;
}
Vector3 computedPosition = GetPositionAlongPath(pointsOfInterest, distanceTravelled);
transform.position = computedPosition;```
And heres my implementation of the GetPositionAlongPath:
Vector3 GetPositionAlongPath(Vector3[] path, float distance)
{
if (path.Length < 2)
throw new ArgumentException(nameof(path));
if (distance <= 0)
return path[0];
float distAtP1 = 0;
for(int i = 1; i < path.Length; i ++)
{
Vector3 p0 = path[i - 1];
Vector3 p1 = path[i];
float distanceBetweenPoints = Vector3.Distance(p0, p1);
distAtP1 += distanceBetweenPoints;
if(distAtP1 > distance)
{
float overshoot = distance - distAtP1;
return (p1 - p0) * (distanceBetweenPoints / overshoot);
}
}
return path[^1];
}```
This is the part that I don't quite get:
return (p1 - p0) * (distanceBetweenPoints / overshoot);
why are we dividing distanceBetweenPoints by a negative number?
it shouldn't be negative, if it is, thats a bug and overshoot should be = distAtP1 - distance
all this code is untested and provided for illustrating a principle
Yeah, I realize it's psuedocode. I was just wondering if dividing by a negative number was some advanced programming concept I hadn't yet grokked
This is how I got it working (I wish syntax highlighting worked in thread)
Vector3 GetPositionAlongPath(Vector3[] path, float desiredDistance)
{
if (path.Length < 2)
{
throw new ArgumentException(nameof(path));
}
if (desiredDistance <= 0)
{
return path[0];
}
float distanceFromFirstStep = 0;
for(int i = 1; i < path.Length; i ++)
{
// Assign points to vars
Vector3 p0 = path[i - 1];
Vector3 p1 = path[i];
// Get distance between last point and this point
float distanceBetweenPoints = Vector3.Distance(p0, p1);
// Add the distance to distance from beginning
distanceFromFirstStep += distanceBetweenPoints;
// If the computed distance from beginning is greater than the distance we want to be at
if(distanceFromFirstStep > desiredDistance)
{
// Compute the amount that distanceFromFirstStep is greater than desiredDistance
float overshoot = distanceFromFirstStep - desiredDistance;
return Vector3.Lerp(p1, p0, (overshoot / distanceBetweenPoints));
}
}
return path[^1];
}```
I had to lerp between p1 and p0 or else the movements went backwards and I cannot grok why!
It’s backwards because the ratio is measuring the fraction not completed, so the lerp gives you an implicit 1 - fraction and fixes that
ah right, so just return Vector3.Lerp(p0, p1, 1 - (overshoot / distanceBetweenPoints));
Yes, should work, it’s the same as swapping the points
Thanks so much for your help. I still don't 100% understand the math but I'm starting to.
And certainly better than my old approach
Don’t worry about there math details, look for the idea of what’s going on
the algorithm idea that is