用力跟随点

Following point using force

我正试图让我的 Entity.cs Follow(Vector2 point) 成为地图上的任何一点。这必须使用 "force" 来完成。 Force是EntityVector2,用于在地图上移动Entity。在 Entity 的每个 Update(float elapsedTime) 上,Entity 移动 Force * elapsedTime。像这样:

public virtual void Update (float elapsedTime)
{
    position += force * elapsedTime;
}

所以,我想创建一个类似于 Update 函数的函数,称为 Follow,它将为 Entity 增加足够的力,使其移动到点,并在到达该点时停止。

bullet class(Entity 的 child)可以使用它来跟踪敌人,或者类似的东西,我相信你们都能看到我用的是什么在游戏中。

目前,代码如下所示:

public virtual void Follow (Vector2 follow, float intensity)
{
    if (position != follow) AddForce((follow - position) * intensity);
    else AddForce(-force);
}

调用此函数的代码如下所示

Follow(followThisPoint, 300 * elapsedTime);

请注意,每 Update 都会调用此行,这就是我想要的方式。

我在使用此功能时遇到的问题是对实体施加了太多的力,它刚好通过我想要的点,然后,当它通过时,它变慢了, 并尝试返回,但随后我得到了与刚才描述的相同的结果,但方向相反。

我想控制 Entity 跟随兴趣点的速度,让它在兴趣点处立即停止,或者在接近时减速,然后停在兴趣点上慢慢点。

编辑 1: 根据要求,这里是 AddForce 函数:

public void AddForce (Vector2 addForce)
{
    force += addForce;
}

在回答问题之前,我想澄清一些术语。

你的变量force实际上是一个速度。它表示物体移动的速度。力可以通过加速度改变物体的速度。如果你对一个物体施加力,这个物体就会加速。对速度的影响取决于施加力的时间和物体的质量。

鉴于此,让我们来看看你的问题。您想要施加一个力,使得生成的速度矢量为:

vTarget = (follow - position) * intensity

这意味着当物体到达目标时速度下降到零。您还可以将速度限制在上限。

根据您的 AddForce 定义,您必须施加以下力 F

vTarget            = vCurrent + F
vTarget - vCurrent = F

同样,您可以限制 F 的长度。这就是全部。这是一些 C#/伪代码:

public virtual void Follow (Vector2 follow, float intensity)
{
    float vMax = 1000;
    float fMax = 1000;
    Vector2 vTarget = (follow - position) * intensity;
    if (vTarget.Length() > vMax) vTarget = vTarget * (vMax / vTarget.Length());
    Vector2 force = vTarget - this.force; //where this.force is actually the velocity
    if (force.Length() > fMax) force = force * (fMax / force.Length());
    AddForce(force);
}