ricardojmendez / UnitySteer

Steering, obstacle avoidance and path following behaviors for the Unity Game Engine
https://numergent.com/tags/unitysteer/
Other
1.21k stars 277 forks source link

MapPointToPathDistance seems wrong #20

Closed danzel closed 9 years ago

danzel commented 9 years ago

Specifically this part: https://github.com/ricardojmendez/UnitySteer/blob/development/Vector3Pathway.cs#L205-L208

We add the whole segment length even if we are only part way along this segment segmentProjection is a fraction (0-1) along the length of the segment, so we need to multiply it by this segments length.

I think this is what it should look like:

if (d < minDistance) {
    minDistance = d;
    pathDistance = segmentLengthTotal + segmentLength * segmentProjection;
}

segmentLengthTotal += segmentLength;
danzel commented 9 years ago

Sorry, was wrong about the fraction. The other part is definitely a bug though. Corrected code:

if (d < minDistance) {
    minDistance = d;
    pathDistance = segmentLengthTotal + segmentProjection;
}

segmentLengthTotal += segmentLength;
ricardojmendez commented 9 years ago

Do you mean that instead of

segmentLengthTotal += segmentLength;
if (!(d < minDistance)) continue;
minDistance = d;
pathDistance = segmentLengthTotal + segmentProjection;

you would expect it to be

if (d < minDistance) {
    minDistance = d;
    pathDistance = segmentLengthTotal + segmentProjection;
}
segmentLengthTotal += segmentLength;

which you post as the corrected code?

Because if so, both are equivalent.

danzel commented 9 years ago

They are not equivalent.

segmentLengthTotal += segmentLength; //previous + segmentlength
if (!(d < minDistance)) continue;
minDistance = d;
pathDistance = segmentLengthTotal + segmentProjection; //previous + segmentlength + projected
if (d < minDistance) {
    minDistance = d;
    pathDistance = segmentLengthTotal + segmentProjection; //previous + projected
}
segmentLengthTotal += segmentLength;

The current code counts segmentLength before progressing over that segment (unless I'm missing something)

ricardojmendez commented 9 years ago

You are absolutely right, and that's what I get for reviewing these things late at night. I'll take care of it.

Thanks!

ricardojmendez commented 9 years ago

Fixed on commit 6517eba

danzel commented 9 years ago

Awesome