Get Vector3 Point from Curve3 by Percentage?

So I can’t find anywhere in the math and curve stuff to pull a position out of the curve.

so instead of it generating sed number of points, I was wondering what the easiest method to locate a percentage position of the curve and only return that point?

ie if I had a straight line curve between 0-10 and I wanted to get the point at 0.6 with the method it will return 6 as a simplified example.

function getCurvePoint(curve, at) {
    let curvePoints = curve.getPoints();
    let curveLength = curve.length();
    let previousPoint = curvePoints[0];
    let currentPoint;
    let currentLength = 0;
    let targetLength = at * curveLength;

    for (let i = 0; i < curvePoints.length; i++) {
        currentPoint = curvePoints[i];
        let distance = BABYLON.Vector3.Distance(previousPoint, currentPoint);
        currentLength += distance;
        if (currentLength === targetLength) {
            return currentPoint.clone();
        } else if (currentLength > targetLength) {
            let toLength = currentLength - targetLength;
            let diff = toLength / distance;
            let dir = previousPoint.subtract(currentPoint);
            return currentPoint.add(dir.scale(diff));
        }
        previousPoint = currentPoint;
    }
    return BABYLON.Vector3.Zero();
}

those are fixed points though, I am looking for it with like infinite detail.

Where 0.2 and 0.200001 would return different values.

I can prolly look at how the points are generated and just modify that.

It is infinite detail actually:

https://playground.babylonjs.com/#NT56I3

1 Like

I love you long time ^_^.

Not sure why I thought this was going to set steps.

this should be added to the Curve class honestly…

1 Like

:smiley: Maybe it should, it’s come up before… I fancy doing a PR.

Hmmmm let me make a PG, cause maybe you can help me work something out really quick, because I went ahead and started using it but its not giving me the result one would expect. Maybe you see why.

Give me like 10 mins to whip up a PG.

UPDATE
Ok I lied, maybe it is doing what I want. On my local scene it was not going to the end of the path, but I see from this its localized.
https://playground.babylonjs.com/#NT56I3#2

UPDATE UPDATE
Fixed it on my local scene, @Gijs 10 pts gryffindor

UPDATE UPDATE UPDATE
Here is it in action with my animation flag system, just a little preview of things to come. The camera animation and all the other dom effects are being controlled by a pretty nifty flag system I came up with cause none of the scrolling animation APIs were doing the trick I needed.

1 Like