WebPlayer: http://bojlahg.com/proto/ShotTest/
функция возвращает true если имеет смысл стрелять (снаряд может догнать цель)
shoterPos - положение стрелка
targetPos - положение цели
targetVel - скорость цели
projVel - скорость снаряда
shotDir - расчитанное направление выстрела
Синтаксис:
Используется csharp
private bool CalcShot(Vector2 shoterPos, Vector2 targetPos, Vector2 targetVel, float projVel, out Vector2 shotDir)
{
Vector2 shot1, shot2;
float a = targetPos.y - shoterPos.y;
float b = targetVel.y;
float c = targetPos.x - shoterPos.x;
float d = targetVel.x;
float e = projVel;
float a2 = a * a, b2 = b * b, c2 = c * c, d2 = d * d, e2 = e * e;
shot1.x = (-Mathf.Sqrt(-c2 * (a2 * d2 - a2 * e2 - 2 * a * b * c * d + b2 * c2 - c2 * e2)) + a2 * d - a * b * c) / (a2 + c2);
shot1.y = (-a * Mathf.Sqrt(-c2 * (a2 * d2 - a2 * e2 - 2 * a * b * c * d + b2 * c2-c2 * e2)) - a * c2 * d + b * c2 *c) / (c * (a2 + c2));
shot2.x = (Mathf.Sqrt(-c2 * (a2 * d2 - a2 * e2 - 2 * a * b * c * d + b2 * c2 - c2 * e2)) + a2 * d - a * b * c) / (a2 + c2);
shot2.y = (a * Mathf.Sqrt(-c2 * (a2 * d2 - a2 * e2 - 2 * a * b * c * d + b2 * c2-c2 * e2)) - a * c2 * d + b * c2 *c) / (c * (a2 + c2));
float t1 = (shoterPos.x - targetPos.x) / (targetVel.x - shot1.x);
float t2 = (shoterPos.x - targetPos.x) / (targetVel.x - shot2.x);
if(t1 > 0 && t2 > 0)
{
if(t1 < t2)
{
shotDir = shot1.normalized;
return true;
}
else
{
shotDir = shot2.normalized;
return true;
}
}
else
{
if(t1 > 0)
{
shotDir = shot1.normalized;
return true;
}
else if(t2 > 0)
{
shotDir = shot2.normalized;
return true;
}
else
{
shotDir = Vector2.zero;
return false;
}
}
}
{
Vector2 shot1, shot2;
float a = targetPos.y - shoterPos.y;
float b = targetVel.y;
float c = targetPos.x - shoterPos.x;
float d = targetVel.x;
float e = projVel;
float a2 = a * a, b2 = b * b, c2 = c * c, d2 = d * d, e2 = e * e;
shot1.x = (-Mathf.Sqrt(-c2 * (a2 * d2 - a2 * e2 - 2 * a * b * c * d + b2 * c2 - c2 * e2)) + a2 * d - a * b * c) / (a2 + c2);
shot1.y = (-a * Mathf.Sqrt(-c2 * (a2 * d2 - a2 * e2 - 2 * a * b * c * d + b2 * c2-c2 * e2)) - a * c2 * d + b * c2 *c) / (c * (a2 + c2));
shot2.x = (Mathf.Sqrt(-c2 * (a2 * d2 - a2 * e2 - 2 * a * b * c * d + b2 * c2 - c2 * e2)) + a2 * d - a * b * c) / (a2 + c2);
shot2.y = (a * Mathf.Sqrt(-c2 * (a2 * d2 - a2 * e2 - 2 * a * b * c * d + b2 * c2-c2 * e2)) - a * c2 * d + b * c2 *c) / (c * (a2 + c2));
float t1 = (shoterPos.x - targetPos.x) / (targetVel.x - shot1.x);
float t2 = (shoterPos.x - targetPos.x) / (targetVel.x - shot2.x);
if(t1 > 0 && t2 > 0)
{
if(t1 < t2)
{
shotDir = shot1.normalized;
return true;
}
else
{
shotDir = shot2.normalized;
return true;
}
}
else
{
if(t1 > 0)
{
shotDir = shot1.normalized;
return true;
}
else if(t2 > 0)
{
shotDir = shot2.normalized;
return true;
}
else
{
shotDir = Vector2.zero;
return false;
}
}
}