I've been tinkering with a space simulation. There are ships, and the flight code can modify the velocities and orientations of the ships directly, with limits for maximumSpeedChangePerSec (basically acceleration) and a maxDegreesPerSecond for orientation change, as well as an overall 'maxSpeed' per ship for sanity. I'd rather not get into using forces, inertia tensors, etc, unless it somehow simplifies things. This is "good enough" for now.
The big issue is that I'd like to be able to give the ships not only a target point to pass through, but a desired velocity, essentially turning the target point into a target line extending from that point. A ship should attempt to hit the line and fly along it, starting from as close to the given point as possible (so that, for example, if the point is far in the distance with a velocity pointing back near to the ship's current position, it doesn't simply turn around and never go near the point).
Current Setup
https://i.imgur.com/DcwlB4C.mp4
(The green curve is a flight predictions and can be ignored for this discussion)
- Short yellow = ship orientation
- Long yellow = dir to target point
- Orange = deltaV ((dirToPoint * targetSpeed) - shipVelocity)
- Long white = ship velocity * very large number (i.e. practically infinite length)
- Red sphere = closest point to targetPoint along the long white line
Note: the ship has different accel values for side vs forward thrust, hence it rotating to face DeltaV under most conditions, but you can see the DeltaV start to change before the rotation finishes - it's using side thrust there.
The ship burns against that deltaV to cancel it out to length 0, with the criteria for a finished maneuver being:
- Is the closest position (red sphere) to the target point along the velocity direction (long white) within some "acceptable error" distance? AND
- Is the ship's current velocity roughly equal to the desired velocity (desiredVelocity = (dirToPoint * targetSpeed))?
This makes the ship "turn and burn" against deltaV nicely and pass through the point at the desired speed, with the above logic making sure the ship swings itself into the target point in a nice arc. Note: I force it to rotate back to face the targetPoint before I let it complete the maneuver, just for looks.
Issues
This works well for simple scenarios, but you can imagine trying to organize a formation. If you can't specify a desired direction other than the direction to the point, you will have a lot of trouble. How does a group of ships coming in from different directions all assemble into a formation? You need to specify a target direction not just a target point!
Notice how the above isn't intensely maths-y, but instead built out of DeltaV and some logic? I'm hoping there's a solution in that vein for my problem, but I'm open to all suggestions. I'm struggling to make the final connection, but I have a hunch there's maybe some method of sliding that targetPoint down the line as the ship gets closer, making it curve "into" the line and eventually fly along it. But I don't know how to link that to trying to get as close to the original targetPoint as possible. Ideally, the ship would fly the fastest path it could, within the given acceleration and max speed limits, and trying to hit the targetPoint if possible, but if it will over-shoot it, it should just converge on the line extending from the targetPoint.
An example of what would happen if we just used target point and target velocity at that point like we currently calculate DeltaV, i.e. naively. It wouldn't necessarily ever hit the line, just fly parallel. It also shows the desired outcome for an "overshoot" due to a turn that's just not doable - the ship should at least converge on the line ASAP
Thanks!