0

如何:限制推力的影响,同时允许其他力是无限的。

示例:火箭可以沿其旋转方向推进。只有爆炸才能使它超过最高速度。我正在寻找比代码更多的理论。

任何帮助,将不胜感激。

解决了

编辑:最高速度由推力速度和摩擦力决定。

推力可以叠加到速度上,但是当摩擦力大于推力速度时,可以达到最高速度。

vx = (vx + fx) *fr -- velocity = (velocity + force) *friction
vy = (vy + fy) *fr

当速度足够高时,增加的力会因摩擦而减少。

fr = .9: 很难看到最高速度

fr = .6:很容易看到最高速度

4

2 回答 2

0

控制:左,右和推力

推力的最高速度可以通过摩擦来调整。

-- VARIABLES
player = {
    frc = .95, -- friction
    acc = .05, -- acceleration
    max = .5, -- acceleration max
    deg = 0, -- rotation in degree
    rad = 0 -- rotation in radian
    -- rotation is CW and 0 points to the right
}

-- MAIN UPDATE
function love.update()
    control(player)
    applyVelocity(player)
end

-- UPDATE
function control(a)
    if  L then addRotation(a,-5) end
    if  R then addRotation(a, 5) end
    if  U then thrustOn(a) else thrustOff(a) end
end
function applyVelocity(a)
    -- velocity + force
    a.vx = (a.vx + a.fx) *a.fr
    a.vy = (a.vy + a.fy) *a.fr
    -- position + velocity
    a.x = a.x + a.vx
    a.y = a.y + a.vy
end

-- OTHER
function thrustOn(a)
    accelerate(a)
    rotateDist(a)
end
function thrustOff(a)
    a.f = 0 -- integer of force is used to convert to point (x,y) with (rad)
    a.fx = 0
    a.fy = 0
end
function addRotation(a,deg)
    a.deg = a.deg + deg
    a.rad = math.rad(a.deg) -- math.sin/cos functions use radian instead of degree
end
function accelerate(a)
    a.f = a.f + a.acc
    if a.f > a.max then a.f = a.max end
end
function rotateDist(a)
    -- end point of force rotated from player
    a.fx = a.f *math.sin(a.rad)
    a.fy = a.f *math.cos(a.rad)
end
于 2016-04-19T07:16:57.180 回答
0

使用摩擦限制加速度精确设置最大速度

前面的答案是正确的,即摩擦可以限制速度,但答案错过了有关设置限制的一些细节。

首先你的速度变量

vel = 0; //in pixels per frame. Start at 0 (or wherever you want)

你想要一个特定的最大速度(以每帧像素为单位)

maxVelocity  = 100; // set the maximum speed

而且你有一个特定的最大加速度,以每帧2的像素为单位

maxAccel = 1; // The max acceleration will occur when accelerating from 0 

您现在需要获得当速度达到 100 时停止加速所需的摩擦力。我们将称为未知摩擦力

friction = null;  // what we need to find out

您将使用等式

vel = vel + maxAccel;  // accelerate 
vel = vel * friction;  // add friction

你知道friction加速后应该降低速度,这样它就不会过去maxVelocityfriction因此,当应用于maxVelocity + maxAccel等于时,我们需要一个值maxVelocity

maxVelocity  = (maxVelocity + maxAccel) * friction;

现在只需重新排列方程以求解未知数friction即可

friction = maxVelocity / (maxVelocity + maxAccel);

现在你有了一个值来friction确保你的速度不会超过maxVelocity.

为了稍微扩展它,您可能想要添加一个提升,这将为您提供短期额外速度,但您不希望提升速度超过限制。你可以重新计算friction一个新的maxVelocity,但这可能不会给你你想要的踢,因为速度曲线的顶端有低加速度。

另一种方法是在保持相同摩擦力的同时提供额外的加速度,即计算所需的额外加速度。我们需要新的变量。

maxBoostVelocity = 120; // max boost velocity
boostAccel = null; // this will have to be calculated using the friction we have

使用与上述相同的逻辑,但根据新的最大速度和我们得到的已知摩擦重新排列摩擦解决方案

boostAccel = ((maxBoostVelocity / friction )- maxBoostVelocity);

我喜欢只使用额外的加速度来提升,所以我不必触及现有的加速度值。所以我只是从计算的提升中减去原始加速度来得到我想要的。

boostAccel = boostAccel - maxAccel;

所以在一些代码中

// the known limits
vel = 0;                 //in pixels per frame. Start at 0 (or wherever you want)
maxVelocity  = 100;      // set the maximum speed
maxAccel = 1;            // The max acceleration will occur when accelerating from 0 
maxBoostVelocity = 120;  // max boost velocity
accelerate = false;      // flag to indicate that we want to accelerate
boost = false;           // flag to indicate we want to boost
// the unknown friction and boost accel
boostAccel = null;       // this will have to be calculated using the friction we       
friction = null;         // what we need to find out
function applyAcceleration()
    if(boost){                 // is boosr flag true then add boost
        vel += boostAccel ;    // boost
    }
    if(accelerate || boost){   // is accelerate or boost flag true 
        vel += maxAccel;       // accelerate 
    }
    // always apply the friction after the accelerations 
    vel *= friction;           // apply friction
}
// in the init function calculate the friction and boost acceleration
function init(){
    friction = maxVelocity / (maxVelocity + maxAccel);
    boostAccel = ((maxBoostVelocity / friction )- maxBoostVelocity) - maxAccel;
}
于 2016-04-28T07:50:33.993 回答