我毕业以来已经有一段时间了,所以我正在用Javascript构建模拟器,并努力理解物理学和运动学的基础知识。无论如何,我有一个应该模拟时间的循环,并且该循环的每次迭代等于1秒,并且我有一个对象要从点A([150, 50])移到点B([1, 1])。该对象的最大速度为10,加速度为4.9,减速为-4.9。我会在每次循环迭代(1秒)时重新计算目标位置,但是当我必须减速时,它就无法正常工作,因为在某些时候速度为负。 我是否可以使用任何公式来计算从A点到B点每x秒同时考虑加速和减速的插值?
这是我的代码的当前状态:

const math = require('mathjs');
const { distance } = require('mathjs');

let currentPos = [150, 51];
const targetPosition = [1, 1];

const MAX_SPEED = 10;
const BASE_ACCELERATION = 4.9;
let currentVelocity= 0;
let stopping = false;

const interpolate = (pos, velocity, target, acceleration, t) => {
    const d = math.distance(target, pos);
    const delta = math.subtract(target, pos);
    const ratio = math.divide(delta, d);

    const v = Math.min(velocity + (acceleration * t), MAX_SPEED);
    const newPos = move(pos, ratio, lerp(velocity, v, t));

    return { pos: newPos, d , v, ratio };
};

const move = (pos, ratio, velocity) => {
    return math.chain(ratio)
        .multiply(velocity)
        .add(pos)
        .done();
};

const lerp = (v0, v1, t) => {
    return v0 + t * (v1 - v0);
};

const getStopDistance = (v0, a) => v0 / 2 * a;


// Let's say I'm simulating 15 seconds
for (let i = 0; i < 15; i++) {
    console.log(`####### sec ${i} #######`);
    console.log(`currentPos -> `, currentPos);
    console.log(`currentVelocity -> `, currentVelocity);
    console.log(`stopping -> `, stopping);

    const sd = getStopDistance(currentVelocity, BASE_ACCELERATION);
    const a = (stopping) ? -BASE_ACCELERATION : BASE_ACCELERATION;
    const it = interpolate(currentPos, currentVelocity, targetPosition, a, 1);

    if (it.d == 0)
        break;

    console.log('sd -> ', sd);
    console.log('it -> ', it);

    if (!stopping && sd >= it.d) {
        // Trying to break it down in 2 equations within 1 sec. The first with the current velocity and accelerations and the rest should be the time I should start stopping ?**strong text**
        const d1 = sd - it.d;
        const t1 = (2 * d1) / (currentVelocity + currentVelocity);
        const i1 = interpolate(currentPos, currentVelocity, targetPosition, BASE_ACCELERATION, t1);

        const t2 = 1 - t1;
        const i2 = interpolate(i1.pos, i1.v, targetPosition, -BASE_ACCELERATION, t2);

        console.log('d1 -> ', d1);
        console.log('t1 -> ', t1);
        console.log('i1 -> ', i1);
        console.log('t2 -> ', t2);
        console.log('i2 -> ', i2);

        stopping = true;
        currentPos = i2.pos;
        currentVelocity = i2.v;
    } else {
        currentPos = it.pos;
        currentVelocity = it.v;
    }
}

最佳答案

让我们将问题背后的数学视为一维问题。让我们沿着连接起点和终点的直线找到对象的运动曲线。
给定点L,最大速度v_max和可用的加速和减速a之间的距离,将运动分为三个状态。以下是总行驶距离x以及速度v(给出的伪代码)的数学公式

  • 加速
     t = 0 ... v_max/a
     x = 0.5*a*t^2
     v = a*t
    
  • 滑行
     t = v_max/a ... L/v_max
     x = t*v_max - 0.5*v_max^2/a
     v = v_max
    
  • 减速
     t = L/v_max ... v_max/a+l/v_max
     x = t*v_max - a*(L-t*v_max)^2/(2*v_max^2)-v_max^2/(2*a)
     v = v_max - a*(t - L/v_max) + v_max
    

  • 这些是从受最大速度和总行驶距离约束的标准运动学方程式得出的。

    关于javascript - 同时考虑加速和减速的对象插值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62614488/

    10-11 16:37