-1
\$\begingroup\$

The following code creates an accurate shot to be made at a moving target from a stationary position. (ie a tower or non-moving vector). It works GREAT, but I need it to consider the movement of the shooter. I believe I subtract the shooter's movement from the target's movement, but I don't know what part of the code this would apply to. Please help. I have an assignment due tomorrow and this is working perfectly except for calculating the shooters movement.

    Vector totarget =  target.position - tower.position;

    float a = Vector.Dot(target.velocity, target.velocity) - (bullet.velocity * bullet.velocity);
    float b = 2 * Vector.Dot(target.velocity, totarget);
    float c = Vector.Dot(totarget, totarget);

    float p = -b / (2 * a);
    float q = (float)Math.Sqrt((b * b) - 4 * a * c) / (2 * a);

    float t1 = p - q;
    float t2 = p + q;
    float t;

    if (t1 > t2 && t2 > 0)
    {
        t = t2;
    }
    else
    {
        t = t1;
    }

    Vector aimSpot = target.position + target.velocity * t;
    Vector bulletPath = aimSpot - tower.position;
    float timeToImpact = bulletPath.Length() / bullet.speed;//speed must be in units per second
\$\endgroup\$
  • \$\begingroup\$ a, b, c, p, q, t... that's good, you still have 20 variables that you can name. Seriously, though: you want other to help you? Name your variables meaningfully so we don't have to reverse engineer what you're trying to achieve. \$\endgroup\$ – Alexandre Vaillancourt Oct 29 '15 at 15:51
0
\$\begingroup\$

You'll want to do it right at the beginning. As you probably know, if the target is moving at the same velocity as the tower, it should aim as if neither are moving. If the tower is unmoving, it should be the same calculation you already have there. So, right at the beginning:

var velocityDiff = target.velocity - tower.velocity;

Then, replace all "target.velocity" with "velocityDiff".

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.