Zeeshan Anjum
Zeeshan Anjum

Reputation: 450

Hit incoming missile (simple projectile missile) in Unity3D

I want to make Surface to Air Missile System in unity3D that could predict the incoming missile position after time 't' and set its interceptor missile angle and position in 3D coordinates so that it could hit incoming missile. I am using following function to get prediction of incoming missile.

    void UpdateTrajectory(Vector3 initialPosition, Vector3 initialVelocity, Vector3 gravity)
{
    int numSteps = 500; 
    float timeDelta = 1.0f / initialVelocity.magnitude; 

    LineRenderer lineRenderer = GetComponent<LineRenderer>();
    lineRenderer.SetVertexCount(numSteps);

    Vector3 position = initialPosition;
    Vector3 velocity = initialVelocity;
    for (int i = 0; i < numSteps; ++i)
    {
        lineRenderer.SetPosition(i, position);


        position += velocity * timeDelta + 0.5f * gravity * timeDelta * timeDelta;
        velocity += gravity * timeDelta;
    }
}

I am using line renderer to get visual trajectory display. Now I can hit missile only at few position means I :P have to adjust manually. my SAM Missile System set its angle properly but it can't set exact time and velocity so it could hit missile.

Upvotes: 3

Views: 1305

Answers (1)

huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11926

You need something professional: exponential curve fitting.

http://mathworld.wolfram.com/LeastSquaresFittingExponential.html http://mste.illinois.edu/malcz/ExpFit/FIRST.html

http://www.ni.com/cms/images/devzone/tut/image7_20080714213910.JPG

you get all points as a curve and find the coefficients of curve then extrapolate the next point. Your algorithm seems a little linear for the whole curve but only true for the latest points and this is not enough.

Alos known as non-linear regression.

Here, another stackexchange answer: https://stats.stackexchange.com/questions/20271/exponential-curve-fitting-with-a-constant

Upvotes: 2

Related Questions