寻找具有可变速度的目标的时间

时间:2013-07-16 13:18:47

标签: c# algorithm math

所以我在我的游戏中有鱼雷,它们以每秒0米的速度开始并且逼真地加速。经过这么多秒钟后,它们停止加速并以恒定的速度前进。

我与目标有一段距离,我基本上正在尝试计算自动对准的提前期。

所以给出

与目标的距离;

加速度(每秒);

燃烧时间(加速停止前的秒数);

我需要基本确定我相信弹丸正在行进的平均每秒米数。

我能看到的唯一方法就是这样。

        curdistance; //stores distance traveled per second
        currentspeed; //stores speed at a given second
        acceleration;

        for(int timer = 1; curdistance < distanceToTarget;timer++)
{
        currentspeed = currentspeed + acceleration; 
        curdistance = curdistance + ( currentspeed);

    if(timer >= burnTime)
    {
    acceleration = 0;
    }

}

现在这样可行,但它有两个问题。

燃烧时间必须是一个int,否则分数越小,运行次数越多,以保持准确性。

如果我想要4.2刻录时间,例如为了保持准确性,我必须运行42次并计算每10秒钟。

此外,平均值可能会相当低,具体取决于它超出目标的程度,具体取决于计时器的精确程度。

如果我的射弹以每秒30米的距离进行,并且它需要走121米,那么它会增加另一整秒的旅行,然后它就可以进入/超过目标,这意味着它将实际上是瞄准因为它比它真正应该的距离要长29米。

使用此算法解决此问题的唯一方法是每10秒或100秒更频繁地检查一次。

我觉得可能有一个数学方程式我不知道能让我准确地解决这个问题。

任何帮助?

4 个答案:

答案 0 :(得分:2)

正如您所描述的那样,您的运动分为两部分。第一部分是加速运动(恒定加速度),第二部分是恒速运动。

您可以单独计算每一个的行进距离(或时间),然后将它们组合起来以获得所需的结果。

请记住,您需要检查目标是否比燃烧距离更近的特殊情况。下面的代码使用支票if (distanceToTarget < burnDistance)

执行此操作
// these will be the results
float timeToTarget; 
float averageSpeed;

// assign values to these
float distanceToTarget;    
float acceleration;     
float burnTime;

float burnDistance = acceleration * burnTime * burnTime * 0.5;

if (distanceToTarget < burnDistance)
{
    timeToTarget = Math.Sqrt(2 * distanceToTarget / acceleration);
}
else
{
    float velocity = acceleration * burnTime;
    timeToTarget = burnTime + (distanceToTarget - burnDistance) / velocity;
}

averageSpeed = distanceToTarget / timeToTarget;

答案 1 :(得分:1)

在加速运动期间,您可以使用d = a*t^2/2或等效t = sqrt(2*d/a),此时速度v = a*t。 然后,您可以使用v推断目标。

答案 2 :(得分:1)

如果

d = initial distance to the target
b = burn time
a = acceleration

当射弹停止加速时,它会有

speed = a*b
distance (traveled) = dt = a*b^2/2

从那一刻起,它将需要

time for impact = ti = (d-dt)/(a*b)

总时间

total time for impact = ti + b

答案 3 :(得分:0)

这是一种方式:

Function VelocityGivenTime(burnTime, givenTime)
(
    T = givenTime
    If T > burnTime Then T = burnTime

    return acceleration * T
)

Function DistanceGivenTime(burnTime, givenTime)
(
    If burntime >= givenTime Then
        T = givenTime
        return 0.5 * acceleration * T^2
    Else
        T = burnTime
        D = 0.5 * acceleration * T^2
        D = D + VelocityGivenTime(T) * (givenTime - burnTime)
        return D
    End IF
)

然而,如果您真正想要的是到目标的时间给它的距离,你可以这样做:

Function TimeGivenDistance(burnTime, distance)
(
    burnDistance = DistanceGivenTime(burnTime)

    If distance > burnDistance Then
        return burnTime + (distance - burnDistance) / VelocityGivenTime(burnTime)

    Else
        return SQRT(2 * distance / acceleration)
    End If
)