获得移动摄像机的速度

时间:2015-09-23 08:58:23

标签: javascript three.js

假设相机正在从一个位置移动到另一个位置,或者说相机正在跟随曲线轨迹(通过在样条曲线上获取点并在每个渲染中将位置复制到相机)。

我们如何计算透视相机的速度?

1 个答案:

答案 0 :(得分:0)

简单又脏,您可以设置相机或任何其他具有测量对象位置的对象来监控其速度:

var DebugSpeedometer = (function() {

    var lastMeasurement = 0,
        currentSpeed = 0;

    var lastMeasurementPosition = new THREE.Vector3(),
        distance = new THREE.Vector3();

    return {

        update: function( measureObject, timeStep ){

            if( lastMeasurement + timeStep < clock.getElapsedTime() ) {

                lastMeasurement = clock.getElapsedTime();
                distance = measureObject.getWorldPosition().distanceTo( lastMeasurementPosition );
                currentSpeed = distance / timeStep;
                lastMeasurementPosition = measureObject.getWorldPosition();

             }

             return currentSpeed;
        }

    };

})();

// globals
var clock = new THREE.Clock();
var speedoMeter = new DebugSpeedometer();

在您的更新功能中:

console.log( speedoMeter.update( camera, 1 ) );