Search Unity

PID inputs for linear forward & reverse thrust?

Discussion in 'Physics' started by MV10, Feb 28, 2016.

  1. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    Like many others, I am chasing the holy grail of steering spacecraft with Actual Physics. This is a screen saver, actually, something I've been meaning to write for probably 10 or 15 years. I point this out because others inevitably point out that real physics are often not much fun for the player in space games.

    A simple test project is attached. It is intended to be viewed from the Scene window, there isn't much of interest in the game view. Incidentally it has a decent PID class supporting both Vector3 and floats, if anybody needs one. The PID also has a Reset which seems to be missing from the other PID classes I've seen around. (You'll want to reset the accumulated error data if you cache the users or if the target changes etc.)

    I'm having little success figuring out how to design the error inputs to produce the thrust behaviors I need. I have decent PID control over heading corrections and damping of angular momentum (all working off AddTorque, mostly a Vector3 variant of this 2D demo from several years back; the thrust problems in this test project make it appear to work less-well than it really does). Purely for the sake of good looks, some of the ships in my scene must have airplane-like motion, for lack of a better description.

    Now I am trying to generate thrust values (PID corrections), both positive and negative, that will be applied with AddForce along the ship's forward vector (and only the forward vector). I think I need to combine the corrections for two types of error, but I'm not certain I'm on the right track with my choice of error conditions.

    The first error input is the relationship between the object's forward vector and the rigidbody's velocity vector. The second error input is the relationship between the forward vector and the target heading.

    I want positive force to (a) speed up when alignment is good, or (b) counteract significant off-axis velocity (including, sometimes, backwards velocity after a sharp turn at high speeds). I want negative force simply to slow down when target alignment is very bad. It sounds simple but it has proven elusive.

    Note that the attempt to address this in the attached project is very broken -- just one of many approaches I've tried. In lieu of long-winded explanations of things that didn't work, I'll just leave it with the problem description in case somebody out there has already been there, done that. :)
     

    Attached Files:

  2. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    Well, I ended up going with a non-PID solution (for velocity, steering is still PID-driven) that works pretty well and looks reasonably natural (especially in the real project with moving targets).

    I cheated and set the Rigidbody velocity directly, and I avoid going backwards by always multiplying the magnitude by the transform's forward vector, and I track a separate velocity float to which I apply arbitrary accel and decel values. Then I added a few simple rules to decide when to speed up (when the target is more or less dead-ahead) or slow down (target is far away or far out of alignment). This can be tweaked further (especially in terms of using squared magnitudes and distances) but if somebody needs a similar solution this should be a good start, with a test project that should make it easy to understand.
     

    Attached Files: