2d trajectory planning of a spaceship with physics

后端 未结 6 1065
逝去的感伤
逝去的感伤 2020-12-23 18:19

I\'m implementing a 2D game with ships in space.

In order to do it, I\'m using LÖVE, which wraps Box2D with Lua. But I believe that my question can be answered by an

6条回答
  •  梦毁少年i
    2020-12-23 18:49

    It's called motion planning, and it's not trivial.

    Here's a simple way to get a non-optimal trajectory:

    1. Stop. Apply thrust opposite to the direction of velocity until velocity is zero.
    2. Calculate the last leg, which will be the opposite of the first, a steady thrust from a standing start that gets the ship to x0 and v0. The starting point will be at a distance of |v0|^2/(2*thrust) from x0.
    3. Get to that starting point (and then make the last leg). Getting from one standing point to another is easy: thrust toward it until you're halfway there, then thrust backward until you stop.

    If you want a quick and dirty approach to an optimal trajectory, you could use an iterative approach: Start with the non-optimal approach, above; that's just a time sequence of thrust angles. Now try doing little variations of that sequence, keeping a population of sequences that get close to the goal. reject the worst, experiment with the best -- if you're feeling bold you could make this a genetic algorithm -- and with luck it will start to round the corners.

    If you want the exact answer, use the calculus of variations. I'll take a crack at that, and if I succeed I'll post the answer here.

    EDIT: Here's the exact solution to a simpler problem.

    Suppose instead of a thrust that we can point in any direction, we have four fixed thrusters pointing in the {+X, +Y, -X, -Y} directions. At any given time we will firing at most one of the +/-X and at most one of the +/-Y (there's no point in firing +x and -X at the same time). So now the X and Y problems are independent (they aren't in the original problem because thrust must be shared between X and Y). We must now solve the 1-D problem -- and apply it twice.

    It turns out the best trajectory involves thrusting in one direction, then the other, and not going back to the first one again. (Coasting is useful only if the other axis's solution will take longer than yours so you have time to kill.) Solve the velocity problem first: suppose (WLOG) that your target velocity is greater than your initial velocity. To reach the target velocity you will need a period of thrust (+) of duration

    T = (Vf - Vi)/a
    

    (I'm using Vf: final velocity, Vi: initial velocity, a: magnitude of thrust.)

    We notice that if that's all we do, the location won't come out right. The actual final location will be

    X = (Vi + Vf)T/2
    

    So we have to add a correction of

    D = Xf - X = Xf -(Vi+Vf)T/2
    

    Now to make the location come out right, we add a period of thrust in one direction before that, and an equal period in the opposite direction after. This will leave the final velocity undisturbed, but give us some displacement. If the duration of this first period (and the third) is t, then the displacement we get from it is

    d = +/-(at^2 + atT)
    

    The +/- depends on whether we thrust + then -, or - then +. Suppose it's +. We solve the quadratic:

    t = (-aT + sqrt(a^2 T^2 + 4 a D))/2a
    

    And we're done.

提交回复
热议问题