Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在一个不工作的循环中用JavaScript实现速度/位移方程_Javascript_Algorithm_Velocity_Acceleration - Fatal编程技术网

在一个不工作的循环中用JavaScript实现速度/位移方程

在一个不工作的循环中用JavaScript实现速度/位移方程,javascript,algorithm,velocity,acceleration,Javascript,Algorithm,Velocity,Acceleration,我是JS的初学者,只有几个月的经验,我面临的问题对专家来说应该是非常明显的,所以请耐心和容忍 我有JSON格式的加速计值。我想动态计算速度和位移,而不将所有值存储在一个数组中,以防止长期记录的内存溢出,因此我只想记住加速度计的最后一个值,速度和位移 目标是在浏览器上实时显示车辆的加速和减速。因此,我提出了这种类型的流: 这是我的代码(没有给出预期的结果): 基本上,当currentIndex为1时,我想计算初始速度并将其存储在数组中(一个只能容纳一个整数的数组),然后对于currentInde

我是JS的初学者,只有几个月的经验,我面临的问题对专家来说应该是非常明显的,所以请耐心和容忍

我有JSON格式的加速计值。我想动态计算速度和位移,而不将所有值存储在一个数组中,以防止长期记录的内存溢出,因此我只想记住加速度计的最后一个值,速度和位移

目标是在浏览器上实时显示车辆的加速和减速。因此,我提出了这种类型的流:

这是我的代码(没有给出预期的结果):

基本上,当currentIndex为1时,我想计算初始速度并将其存储在数组中(一个只能容纳一个整数的数组),然后对于currentIndex大于1的情况,我想使用之前的速度来计算当前速度,这个过程应该重复,从数组中删除以前的速度并插入新的速度(我只想记住以前的速度来计算δV和δX)。我不认为我在代码方面做得对,我最初认为我需要实现一个递归算法,但我认为这也可以通过分析来解决。我不确定是否有更有效的方法,但我非常愿意接受其他建议

最后,数据应该是这样的

我从最初的价值中得到了很大的偏离,我想知道什么是解决这个问题的好办法

let currentIndex        = 0
let time                = data[currentIndex].time
var initialVelocity     = 0.11;
var initialDisplacement = 0;
var velocityArray       = []; // array that will hold only velocity value at each iteration
var valid               = 1;
let velocity            = new THREE.Vector3()

requestAnimationFrame(render);

function render(dt) {
    dt   *= 0.8
    time += dt
    while (data[currentIndex].time < time) {
        currentIndex++
        if (currentIndex >= data.length) return
    }
    // compute the first velocity value and insert it in the velocityArray
    if (currentIndex < 2) {
        velocityArray[currentIndex-1] = initialVelocity + ((data[currentIndex-1].acc + data[currentIndex].acc)/2)*((data[currentIndex].time/(24 * 60 * 60)) - (data[currentIndex-1].time/(24 * 60 * 60)))
        // console.log(velocityArray[currentIndex-1]);
    } else { // use the previous velocity value to compute the new velocity value
        valid++;
        const finalVelocity = velocityArray[currentIndex-valid] + ((data[currentIndex-1].acc + data[currentIndex].acc)/2)*((data[currentIndex].time/(24 * 60 * 60)) - (data[currentIndex-1].time/(24 * 60 * 60)))
        // delete old value
        velocityArray.pop();
        // push new value
        velocityArray[currentIndex-1] = finalVelocity;
        // repeat
        console.log(velocityArray);
    }
    
    const {acc,vel,disp} = data[currentIndex]

    document.querySelector("#time").textContent = time.toFixed(2)
    document.querySelector("#acc").textContent = acc;
    document.querySelector("#vel").textContent = vel;
    document.querySelector("#disp").textContent = disp.toFixed(2);
    object.position.y = disp*0.07; // z for rightLeft, y for upDown

    var relativeCameraOffset = new THREE.Vector3 (5,0,0); // change camera offset
    var cameraOffset = relativeCameraOffset.applyMatrix4( object.matrixWorld );
    camera.position.x = cameraOffset.x;
    camera.position.y = cameraOffset.y;
    camera.position.z = cameraOffset.z;
    camera.lookAt( object.position );

    resizeToClient();
    renderer.render(scene, camera);
    requestAnimationFrame(render);
}
[
    {
        "time": 0,
        "acc": 0.11,
        "vel": 0,
        "disp": 0
    },
    {
        "time": 86400,
        "acc": 0.11,
        "vel": 0.11,
        "disp": 0.055
    },
    {
        "time": 172800,
        "acc": 0.11,
        "vel": 0.22,
        "disp": 0.22
    },
    {
        "time": 259200,
        "acc": 0.11,
        "vel": 0.33,
        "disp": 0.495
    },
    {
        "time": 345600,
        "acc": 0.35,
        "vel": 0.56,
        "disp": 0.940
    }
]