Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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_Jquery_Math_Geometry_Intersection - Fatal编程技术网

Javascript 随时间的加速 脚本

Javascript 随时间的加速 脚本,javascript,jquery,math,geometry,intersection,Javascript,Jquery,Math,Geometry,Intersection,我正在制作一个自上而下的视图游戏,在这个游戏中,敌人会向特定的位置移动。被移动的目的地经常会发生剧烈的变化——有时当敌人仍在向前一个目的地移动时 我想实现比直线运动更真实的运动,所以当敌人在目的地之间切换时,应该有一些加速和减速 转向(方向)不是一个因素。你可以假设精灵会像气垫船一样移动,在目的地之间以加速和减速的速度漂移 为简单起见-假设只有1维(Y)而不是X和Y。。。运动应该模仿只能向北或向南移动的汽车 由于我们正在考虑此场景中的真实运动,您可能不会惊讶于随着时间的推移,最大速度也是一个考虑

我正在制作一个自上而下的视图游戏,在这个游戏中,敌人会向特定的位置移动。被移动的目的地经常会发生剧烈的变化——有时当敌人仍在向前一个目的地移动时

我想实现比直线运动更真实的运动,所以当敌人在目的地之间切换时,应该有一些加速和减速

转向(方向)不是一个因素。你可以假设精灵会像气垫船一样移动,在目的地之间以加速和减速的速度漂移

为简单起见-假设只有1维(Y)而不是X和Y。。。运动应该模仿只能向北或向南移动的汽车

由于我们正在考虑此场景中的真实运动,您可能不会惊讶于随着时间的推移,最大速度也是一个考虑因素。敌人绝不能超过自己的最大速度;敌人将自己的最大速度存储在一个变量中

最后一个考虑因素是,敌人不仅有一个“最大速度”值,而且还有一个“最大加速度”值——这将指导每个敌人对向相反方向移动的反应速度

为简单起见,假设敌人没有任何移动摩擦。。。当它停止加速时,它就永远以当前速度巡航

例子 对于上下文,让我们详细介绍一下汽车示例。特定的汽车具有:

  • 最高速度:10米/秒
  • 最大加速度:可在2秒内达到最高速度
  • (其他因素,如目的地y-pos、位置y-pos、流速等)
就像我开车时,我想象所有这些值都存在,但我无法改变它们。我所能真正改变的就是我在加速/刹车上踩了多少脚。让我们称之为“油门”。就像汽车的加速踏板一样,我可以在任何时候,在任何时间内,将这个值更改为任何数量

我可以把脚放低(油门=1),立即松开踏板(油门=0),甚至换成倒车档,再次把脚放低(油门=1)。让我们假设油门的这些变化是瞬间的(与速度或加速度不同,速度或加速度随时间而增长/收缩)

尽管如此,我想我真正需要计算的唯一值是油门应该是多少,因为这是我在车上唯一可以控制的东西

那么,我如何知道在任何给定的时刻使用多少油门,以尽可能快地到达目的地(如一些交通灯),而不超过我的目的地?我需要知道踩下多少油门,暂时不加速,然后在接近目的地时减速有多困难

先发制人的运动 这个游戏可能会有一个在线组件。也就是说,玩家将通过套接字连接传输他们的位置。。。然而,即使是最好的连接也永远无法实现足够频繁的发送位置,以实现平滑移动-您需要插值。您需要发送“速度”(speed)以及坐标,以便我可以在接收数据包之间的时间内假设未来的位置

出于这个原因,青少年是不可能的。没有办法发送一个tween,然后准确地通知其他各方每个实体当前在每个tween上的位置(我的意思是,我想这是可能的,但是非常复杂,可能涉及相当大的数据包发送,并且可能在在线组件方面也非常容易被利用),然后,您需要考虑在目的地更改等情况下中止Twitter等。 别误会,我可能已经可以用Tween的ease-in/ease-out功能模拟一些非常现实的动作,看起来很棒,但在在线环境中,这将是。。很乱

到目前为止看起来怎么样? 所以,基本上,我已经确定,在任何时候都需要计算的主要方面是使用多少油门。让我们研究一下我的逻辑

想象一个非常基本的,随时间线性移动的公式。。。它看起来是这样的:

示例1-随时间变化的位置

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;