0

so i have torpedos in my game and they start out at 0 meters per second and accelerate realistically. after so many seconds they stop accelerating and travel at a constant rate forward.

I have a distance to the target and I basically am trying to calculate lead time for autoaiming.

So given

Distance to target;

Acceleration (per second);

burn time (number of seconds before acceleration stops);

I need to basically determine I believe the average meters per second the projectile is travelling.

The only way I can see to do it is something like this.

        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;
    }

}

Now this works but it has 2 problems.

The burn time has to be an int or else the smaller the fraction the greater the number of runs to keep accuracy.

If i want a 4.2 burn time for example in order to keep the accuracy i have to run it 42 times and calculate for every 10th of a second.

Also the average could be off by quite a bit depending on how much it overshoots a target depending again on how precise the timer is.

if my projectile is going at 30 meters per second and it needs to go 121 meters it'll add another full second of travel before it goes ok you've gone to/past the target which will mean it will actualy be aiming as it were at a point 29 meters further than it really should.

The only way to combat this with this algorithm is to check more often every 10th or 100th of a second.

I feel like though there might be a math equation I don't know that lets me solve this precisely.

Any Help?

4

4 回答 4

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;
于 2013-07-16T13:38:03.743 回答
2

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

于 2013-07-16T13:26:38.193 回答
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
于 2013-07-16T13:31:09.730 回答
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
)
于 2013-07-16T13:37:57.527 回答