javascript - The object doesnt go fully in spline direction - Stack Overflow

admin2025-04-25  2

Iam Implementing a roller coaster Here's the pseudocode of few functions in the code Computearclength computes t and its relevant distnace upto that t

MapSToT computes and maps s to t in spline position

Lastly the main function is involved in computing the positions and rotations for object

function computeArcLength(controlPoints, segments = 40) {
    const arcLengthTable = [];
    let totalLength = 0;

    let prevPoint = getCatmullRomPosition(0, controlPoints.map(p => p.position));
    arcLengthTable.push({ t: 0, length: 0 });

    for (let i = 1; i <= segments; i++) {
        const t = i / segments * (controlPoints.length - 1);
        const currentPoint = getCatmullRomPosition(t, controlPoints.map(p => p.position));
        const segmentLength = prevPoint.distanceTo(currentPoint);
        totalLength += segmentLength;

        arcLengthTable.push({ t, length: totalLength });
        prevPoint = currentPoint;
    }

    return { arcLengthTable, totalLength };
}
function mapStoT(s, arcLengthTable, totalLength) {
    const targetLength = s * totalLength;

    for (let i = 1; i < arcLengthTable.length; i++) {
        if (arcLengthTable[i].length >= targetLength) {
            const prev = arcLengthTable[i - 1];
            const curr = arcLengthTable[i];

            // Linear interpolation to find t
            const segmentLength = curr.length - prev.length;
            const localS = (targetLength - prev.length) / segmentLength;

            return prev.t + localS * (curr.t - prev.t);
        }
    }

    return arcLengthTable[arcLengthTable.length - 1].t;
}

function moveObjectWithVelocity(object, controlPoints, velocityCurve, stepSize) {
    const { arcLengthTable, totalLength } = computeArcLength(controlPoints);
    let s = 0; // Normalized arc length parameter [0, 1]

    function animate() {
        if (s >= 1) {
            s = 0; // Loop back for continuous movement
        }

        // Map s to t
        const t = mapStoT(s, arcLengthTable, totalLength);

        // Get interpolated position
        const interpolatedPos = getCatmullRomPosition(t, controlPoints.map(p => p.position));
        object.position.copy(interpolatedPos);

        // Interpolate normals for smooth tilting
        const currentIndex = Math.floor(t);
        const nextIndex = Math.min(currentIndex + 1, controlPoints.length - 1);
        const normal = new THREE.Vector3().lerpVectors(
            controlPoints[currentIndex].normal,
            controlPoints[nextIndex].normal,
            t - currentIndex
        );
        const up = new THREE.Vector3(0, 1, 0); // Reference up vector
        const quaternion = new THREE.Quaternion().setFromUnitVectors(up, normal);
        object.quaternion.copy(quaternion);

        // Increment s based on velocity
        const velocity = velocityCurve(s); // Get velocity at the current s
        s += 0.03;

        requestAnimationFrame(animate);
    }

    animate();
}

The velocity curve is just simple parabola BUT the object doesn't go all the way to splines end point Could u help figuring out where I'm doing wrong things Thanks!

转载请注明原文地址:http://anycun.com/QandA/1745530173a90823.html