Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Missile / rocket flight with gravity

Discussion in 'Scripting' started by Desprez, Nov 7, 2015.

  1. Desprez

    Desprez

    Joined:
    Aug 31, 2012
    Posts:
    305
    I'm working out how to get a missile to fly towards its target while compensating for gravity. The missile needs to keep its current velocity vector aimed at the target, as opposed to aiming directly at it.
    Basically, it will need to angle a little bit upwards of the target, so the vertical component of its thrust will counteract gravity.

    My first attempts aren't great so far, so I'm looking for advice.

    My current attempt was a reactive solution, in that missile compensates based of the situation at the moment:
    1) Find the angle between target direction and velocity direction.
    2) Add this to the current direction its pointing, to find the goal angle.
    As the velocity direction approaches the target direction, the compensation angle drops to zero.

    While this works, there is a bit of time from when the missile turns to when its velocity direction changes, leading to over-correction and oscillation around the ideal flight line. This can be minimized somewhat with higher drag and higher thrust, but it remains a problem.

    The result is that it is very inefficient flight and looks awful.

    Maybe there's a better way to go about this. A pure math solution perhaps?
     
    Last edited: Nov 7, 2015
  2. Spoke44

    Spoke44

    Joined:
    Feb 16, 2015
    Posts:
    11
  3. Desprez

    Desprez

    Joined:
    Aug 31, 2012
    Posts:
    305
    No, those are for projectiles without propulsion. As are most other searches on the subject.
     
  4. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    356
  5. Desprez

    Desprez

    Joined:
    Aug 31, 2012
    Posts:
    305
    Wait a second, is my problem essentially just a simple linear intercept course?
    If so, I may have overthought the whole thing.
     
  6. Desprez

    Desprez

    Joined:
    Aug 31, 2012
    Posts:
    305
    TL;DR
    Pro-nav has some drawbacks, but I'm now getting excellent results using a variation of a linear intercept formula.

    Ok, so I tried proportional navigation out.
    It's not working terribly well, in large part due to a couple of assumptions when based in real-world applications.

    1) Rigidbody physics. I was still getting some oscillation even with pro-nav, because again, the rigidbody takes a little time for a course adjustment to take effect. (Though it wasn't nearly as bad.)
    In the real world, the high speed of missiles and their control surfaces would have a significant stabilizing influence, reducing the effect of the oscillation from Unity rigidbodies sliding around.
    For this reason, I'm fairly certain that the missiles shown in the video are not actually using pure rigidbody physics, and probably have a flight model doing quite a bit of work. It's possible that they may be using very high drag values, however.

    2) It doesn't work if the missile is heading away from the target. Because pro-nav relies on watching the rate of target bearing change, and assuming that 'no change' is an intercept course. The thing is, heading directly away from the target also produces zero bearing change.
    In real missiles, this is irrelevant because a missile heading the wrong way has lost the target and is worthless anyway. But in sci-fi, and countless game environments, this isn't the case.
    Some care would need to be taken to re-orient the missile back to the general direction - and this runs into the gravity problem again - especially so for slow missiles barely held aloft.

    The thing is, proportional navigation is a solution that shines when you can't rely on finding the exact velocities or the exact distances of the objects involved. But since this is a game environment, we have those values.
    And while pro-nav is a powerful solution, it has a few drawbacks - and you can start to see this in the use of "fudge" values like "navigation gain"

    So I did some tinkering with a linear intercept formula typically used to aim a gun to hit a moving target.
    In this case, shooter velocity isn't relevant because the missile will fly under its own power (and it's velocity is constantly changing).
    For shot speed I'm using the velocity.magnitude of the missile. This won't exactly correlate to closing speed when the missile vector isn't heading right for the target, but its close enough when it counts.

    Once an intercept point is found, I'm adjusting that point by the effect of gravity over the estimated time of impact.
    It should be noted that adding the gravity vector to the missile velocity (or the the gravity inverse to the target velocity) is problematic because the intercept formula can then result in no valid solutions. Doing it after gets around that.
     
    Last edited: Nov 9, 2015
    JoeStrout likes this.
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Whats your motivation for it using gravity?

    Sounds like you are making life harder for yourself doing it, but maybe you have a good reason.
     
  8. Desprez

    Desprez

    Joined:
    Aug 31, 2012
    Posts:
    305
    I'm putting together a fairly comprehensive vehicle weapons package. And as such, They should accomodate a wide variety of applications.

    In addition, some missile types will only burn for a short duration. These will try to enter a ballistic arc to reach the target on residual momentum. gravity would be important in such a scenario.

    Turning gravity on and off to fake it seems a bit hack-y. And will probably look a little unnatural.
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Theres always a line in game dev. Do I spent ages making something true to real world, or make something look real.

    I found with missiles in my game I turned off gravity, then turned it on when the missile ran out of fuel. It looks good, and not un-natural.

    I cant think of any real world missiles that fly like you mention. But then I dont know of them all :p.. An Arrow is the only thing I can think of like that. But thats not a missile, thats a projectile fired with initial velocity.
     
  10. Desprez

    Desprez

    Joined:
    Aug 31, 2012
    Posts:
    305
    Well sure there's a line in game dev. But as an asset, I have no way of knowing where that line is for any given developer. But the more scenarios it can handle, the more attractive it is. :D

    I'm pretty sure plenty of real-world missiles utilize parabolic arcs towards the longer portion of their range envelope.
    Once fuel is spent, you still have a projectile moving at some multiplier of mach, and that's quite a bit of extra range. Control surfaces can use this residual momentum to fly the missile to the target, but extra range can be realized by reducing the need to constantly steer against gravity (this creates extra drag and bleeds speed). By aiming in a ballistic fashion, the flight path is more efficient since less correction is needed to keep the missile on course.

    Off the top of my head, I believe FGM-148, AIM-120, AGM-65 all have the capability to use a parabolic arc when engaging long range targets. In the case of the Javelin, a high arc is sometimes used even at closer ranges to defeat a tank's top armor, and not just for distant targets. It also has a direct flight path mode, but this would also limit range.
    In the case of the Maverick, it has a rocket burn of about 4 seconds and a max effective range of 22 km going just under mach 1. That's over a minute of unpowered flight using napkin math, so it's hard to see how it wouldn't use some ballistics.

    Now it's entirely possible that no real-world missile really does this with any kind of efficiency (though I'm not sure why they wouldn't) but in a realistic-but-not-real scenario there certainly could be a missile that is advanced enough to extend to its maximum possible range using ballistics.