Javascript 随时间的加速 脚本
我正在制作一个自上而下的视图游戏,在这个游戏中,敌人会向特定的位置移动。被移动的目的地经常会发生剧烈的变化——有时当敌人仍在向前一个目的地移动时 我想实现比直线运动更真实的运动,所以当敌人在目的地之间切换时,应该有一些加速和减速 转向(方向)不是一个因素。你可以假设精灵会像气垫船一样移动,在目的地之间以加速和减速的速度漂移 为简单起见-假设只有1维(Y)而不是X和Y。。。运动应该模仿只能向北或向南移动的汽车 由于我们正在考虑此场景中的真实运动,您可能不会惊讶于随着时间的推移,最大速度也是一个考虑因素。敌人绝不能超过自己的最大速度;敌人将自己的最大速度存储在一个变量中 最后一个考虑因素是,敌人不仅有一个“最大速度”值,而且还有一个“最大加速度”值——这将指导每个敌人对向相反方向移动的反应速度 为简单起见,假设敌人没有任何移动摩擦。。。当它停止加速时,它就永远以当前速度巡航 例子 对于上下文,让我们详细介绍一下汽车示例。特定的汽车具有:Javascript 随时间的加速 脚本,javascript,jquery,math,geometry,intersection,Javascript,Jquery,Math,Geometry,Intersection,我正在制作一个自上而下的视图游戏,在这个游戏中,敌人会向特定的位置移动。被移动的目的地经常会发生剧烈的变化——有时当敌人仍在向前一个目的地移动时 我想实现比直线运动更真实的运动,所以当敌人在目的地之间切换时,应该有一些加速和减速 转向(方向)不是一个因素。你可以假设精灵会像气垫船一样移动,在目的地之间以加速和减速的速度漂移 为简单起见-假设只有1维(Y)而不是X和Y。。。运动应该模仿只能向北或向南移动的汽车 由于我们正在考虑此场景中的真实运动,您可能不会惊讶于随着时间的推移,最大速度也是一个考虑
- 最高速度:10米/秒
- 最大加速度:可在2秒内达到最高速度
- (其他因素,如目的地y-pos、位置y-pos、流速等)
currentDY = 5; // Current 'velocity' (also called Delta Y or 'DY')
currentY += currentDY * time // Current Y pos is increased by movement speed over time.
throttle = -1
currentDY += throttle * time;
currentY += (currentDY * time);
//Throttle being -1 eventually decelerates the DY over time...
如您所见,在任何给定时刻,Y位置都会随着时间的推移而增加,这是由于“速度”或DY(随着时间的推移)。时间只是一个因素,所以一旦我们到达目的地,我们就把DY设置为零。非常尖锐的不切实际的动作。为了平滑运动,速度也需要随时间变化
示例2-随时间变化的速度
currentDY = 5; // Current 'velocity' (also called Delta Y or 'DY')
currentY += currentDY * time // Current Y pos is increased by movement speed over time.
throttle = -1
currentDY += throttle * time;
currentY += (currentDY * time);
//Throttle being -1 eventually decelerates the DY over time...
在这里,油门为'-1'(最大倒车!),因此随着时间的推移,这将降低速度。这对于真实的加速效果非常好,但没有提供真实的预期或减速
如果我们这次到达目的地,我们可以将油门设置为“0”。。。但现在刹车已经太迟了,因此最终的敌人将永远不停地越过目标。我们可以用节流阀='1'返回,但最终我们将永远来回摆动
(还要注意的是,最大加速度和速度甚至还不是一个因素——它肯定是需要的!敌人不能永远提高速度;速度增量和加速度增量需要有限制)
尽管如此,仅仅改变速度是不够的,但我们还需要能够在减速发生之前预测减速的程度(即“向后油门”)。这是我到目前为止得到的,我几乎可以肯定这是错误的方法
示例3-节气门随时间变化??(我卡住了)
正如你所看到的,这次我们试图通过猜测敌人在未来任意时间(即3秒)的位置来预测要使用多少油门。如果指南
通过了目的地
if (sprite is more than predictionTime seconds away from the target){
guideX = targetX + predictionTime * targetVX;
guideY = targetY + predictionTime * targetVY;
}
else{
guideX = targetX + remainingTime * targetVX;
guideY = targetY + remainingTime * targetVY;.
}
//You will have to choose the jerk factor -- the rate at which acceleration changes with respect to time.
//We need to figure out what direction we're going in first. Note that the arc tangent function could be atan or whatever your library uses.
dir = arctan((guideY-currentY)/(guideX-currentX));
t = time; //For ease of writing.
newAX = currentAX + jerk*t*cos(dir);
newAY = currentAY + jerk*t*sin(dir);
magnitudeA = sqrt(newAX^2+newAY^2);
if (magnitudeA > maxA){
currentAX = maxA * cos(dir);
currentAY = maxA * sin(dir);
}
newVX = currentVX + currentAX*t;
newVY = currentVY + magnitudeA*t*sin(dir);
magnitudeV = sqrt(newVX^2+newVY^2);
if (magnitudeV > maxV){
currentVX = maxV * cos(dir);
currentVY = maxV * sin(dir);
}
if (remainingTime < closeTime){
//We are very close to the target and will stop the boomerang effect. Note we add the target velocity so we don't stall while it's moving.
//Edit: We should have a min speed for the enemy so that it doesn't slow down too much as it gets close. Lets call this min speed, relative to the target.
currentVX = targetVX + currentVX * (closeTime - remainingTime);
currentVY = targetVY + currentVY * (closeTime - remainingTime);
if (minSpeed > sqrt(currentVX^2+currentVY^2) - sqqrt(targetVX^2-targetVY^2)){
currentVX = minSpeed * cos(dir);
currentVY = minSpeed * sin(dir);
}
}
magnitudeV = sqrt(currentVX^2+currentVY^2);
newX = currentX + currentVX*t;
newY = currentY + currentVY*t;
//Check if x, y values are good.
current X = newX; currentY = newY;