2021-11-27

Get angle to land ballistic arc on target with fixed velocity projectile

So I was trying to follow the code in this question to get a turret that can fire ballistic projectiles with a fixed starting velocity and no drag to a given point on a 3D surface. Find an angle to launch the projectile at to reach a specific point

But It's not quite working. The turret ends up aiming too high when the target is close, and too low when the target is further away. There is of course a specific distance at which it does hit the target but that distance is arbitrary, so that's not at all helpful to me.

The way the error scales makes me think I have a multiplication mistake, or am missing some multiplication or division, but I can't for the life of me figure out where I am going wrong. Can anyone point me in the right direction?

Code Below:

float CalculateAngle(float velocity)
{
    float gravity = -Physics.gravity.y;

    Vector3 modPos = target.position;
    if (modPos.x < 0) modPos.x -= 2 * modPos.x;
    if (modPos.y < 0) modPos.y -= 2 * modPos.y;
    if (modPos.z < 0) modPos.z -= 2 * modPos.z;
    modPos.x /= 10;
    modPos.y /= 10;
    modPos.z /= 10;
    
    float deltaX = modPos.x - FirePoint.position.x;
    float deltaZ = modPos.z - FirePoint.position.z;
    float deltaY = modPos.y - FirePoint.position.y;
    float horzDelta = Mathf.Sqrt(deltaX * deltaX + deltaZ * deltaZ);

    float RHSFirstPart = (velocity * velocity) / (gravity * horzDelta);
    float RHSSecondPart = Mathf.Sqrt(((velocity * velocity) * ((velocity * velocity) - (2 * gravity * deltaY))/ (gravity * gravity * horzDelta * horzDelta)) - 1);
    float tanθ = RHSFirstPart - RHSSecondPart;
    float angle = Mathf.Atan2(tanθ, 1) * Mathf.Rad2Deg;
    if (angle < 0) return angle;
    return -angle;
}

Close Range

Just Right

Long Range

Edit 1: Still struggling heavily with this. I just can't get the math to work. I went back to the original root of the knowledge here https://physics.stackexchange.com/questions/56265/how-to-get-the-angle-needed-for-a-projectile-to-pass-through-a-given-point-for-t then wrote a function that did the exact equation given in the answers, copying the input values and everything. Except when I run it it fails, as one of the values that needs to be squared is negative which throws a NaN. I assume I am going wrong somewhere in my equation but I've gone over it a hundred times and I am not spotting the error. My code:

float CalculateAngle3(float velocity)
{
    float deltaX = 500;
    float deltaY = 20;
    float v = 100;
    float vSqr = v * v;
    float g = 9.81f * 9.81f;

    float a = vSqr * (vSqr - 2 * g * deltaY);
    float b = (g * g) * (deltaX * deltaX);
    float c = a / b - 1;
    float d = Mathf.Sqrt(c); //c is negitive causing an NaN
    float e = vSqr / g * deltaX;
    float tanθ = e - d;

    return tanθ;
}

Edit 2: Gave up. This guy solved it so I am just going to use his logic instead : P https://www.forrestthewoods.com/blog/solving_ballistic_trajectories/

Using it like such:

Vector3 s0;
Vector3 s1;
if (fts.solve_ballistic_arc(FirePoint.position, bomb.StartingVelocity.z, target.position, -Physics.gravity.y, out s0, out s1) > 0)
{
    targetPosition = s1;
    SafetyEnabled = false;
}
else
{
    //Don't fire if we don't have a solution
    SafetyEnabled = true;
}

I'm going to leave the question open for now since it's still technically not answered. I still don't know why the original implementation wasn't working.



from Recent Questions - Stack Overflow https://ift.tt/3p6BChB
https://ift.tt/315rOMM

No comments:

Post a Comment