Programming a smooth change of thrust from current velocity vector to a target vector

后端 未结 7 1687
北海茫月
北海茫月 2021-02-04 16:47

TL;dr: \"I am not sure how to calculate a smooth transition of thrust between one vector and another.\"

I am programming a simple game where an enemy chases after the pl

7条回答
  •  深忆病人
    2021-02-04 17:46

    I've solved problems like this professionally, and I advise you to start with simple versions and work up. Make sure you get expected behavior at each step before you try the next.

    1. The seeker seeks a stationary target at the origin in one dimension. That's right, one dimension. It can thrust back and forth on the x-axis and it's trying to get to x=0. The thruster has no throttle (like a solid rocket) but the seeker can point it in either direction. If you program this right, the seeker will oscillate around x=0, overshooting every time.
    2. The same, but the target is stationary somewhere other than x=0. Just make x relative, not absolute (that is, the seeker cares about the difference in x, not the target's x).
    3. Now the target is moving (or jumping). The seeker should be able to follow it around. The oscillation will grow or shrink depending on how the target moves -- you'll see what I mean.
    4. Now two dimensions. The seeker always thrusts directly toward the target, which means you must divide the thrust into x and y components by simple trig. If you move the target, the seeker may go into orbit around it.
    5. Back to one dimension and a stationary target, but now the seeker is trying a rendezvous, not a flyby. This is the hard part. The goal is to have distance and velocity become zero at the same time, no overshooting, so the seeker must know its own braking ability. When x is less than v^2/2a, the seeker must reverse thrust, thrusting away from the target in order to slow down and meet it. It's nice to allow the seeker to stop thrusting when it's very close to the target.
    6. The target starts moving again. This is easy; just make x and v relative, not absolute.
    7. Multiple dimensions. This is remarkably easy; the x, y, and z parts are independent.

    Now if you want a seeker that can't turn on a dime, but must curve around, things get tricky...

提交回复
热议问题