0

First, for my specific problem, we can assume we're working in a 1D space.

I have a point B moving at a specific velocity V. I want to smoothly move the body A so it reaches the point B and moves at the same speed, without oscillating too much. A could be behind or before B.

A is a physic body, on which is applied 2 forces, the thrust and the drag, so I don't directly control it's speed.

I've come to some kind a solution but it's not clean at all and involves a lot of thresholds, so I'm looking for something more clever.

I've already used PIDs to handle steering, and I suspect they may be useful there but for some reason my brain can't find how to properly do it, which annoys me a lot because it looks like a trivial problem.

enter image description here

Thanks.

  • A cubic Bézier curve is equivalent to an object moving with linearly varying thrust, and lets you specify both position and velocity at the start and end of the maneuver to get continuity with your previous frame's motion, and matching the target's motion at the end. I show in this answer how you can use this to plan an intercept/rendezvous course in 2D, but the same can be applied one dimension lower. – DMGregory Oct 01 '21 at 13:22
  • Or alternatively, if you switch to looking at the problem in B's inertial frame (in which B is stationary at the origin), then this is equivalent to decelerating and stopping at a point. – DMGregory Oct 01 '21 at 13:31
  • Thanks, the second link helped me a lot to address the problem.

    I'm now trying to get the time it would take for speed to decrease from \$v_0\$ to $v_1$ using only the drag defined as $d(v) = -bv^2$ where $b$ is a constant drag factor. With that I can tell if I'll overshoot or not

    This gives me $acc = \frac{dv}{dt} = \frac{-bv^2}{m}$ where $m$ is the mass of the body.

    Now I think to calculate the time I need to do something like :

    $dt = dv \frac {m}{bv^2} $

    $t = \int_{v_1}^{v_0} \frac{m}{b v^2} dv$

    Aaand here I'm stuck and not even sure it's right so far

    – Toopzor Oct 04 '21 at 09:56

1 Answers1

0

I said in the OP I was struggling to find the way to use a PID controller in this case, but I've figured how to do it. Here are the few steps:

  1. Calculate the thrust required to go at the speed of B
  2. Use the distance to B as an input error for the PID
  3. Use the values returned by the PID as an offset applied to the thrust
  4. Tune the PID (the funniest part of course)

After some tuning, it works like a charm.

  • In this kind of problems you might want to use linear interpolation, where B does its motion freely, and A adjust its position using lerp with a factor from (0, 1) depending on "how slow" A reaches B (the higher the slower). More information: https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html – Andrea Nov 07 '21 at 18:04