# Using physics to maneuver a spaceship through a point with a specific velocity

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.

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!

• There are of course an infinite number of paths, which one is "best"? Minimum distance/time/deltaV? Accelerating along a simple arc between the start and end vectors seems like a decent but perhaps not optimal approach - if such a maneuver exceeds the capabilities of the ship, you just need to fly away from the target until the you have enough space to do it. Jun 9, 2023 at 13:48
• @NuclearHoagie Good point. I would say "best" is minimum time within acceleration and maxSpeed constraints. If it's not possible to hit the target point within the constraints, ideally the ship would simply join with the target line as soon as it can; the point is the "ideal" start of the line but not absolutely necessary. Jun 9, 2023 at 14:12
• I'll also add the wrinkle that assembling a formation requires not only the position and velocity, but also timing - it's no good for one ship to hit its mark quickly and fly off in "formation" by itself, all the ships in the formation need to complete their maneuver simultaneously. This may not be such an issue for a "school of fish" where everybody just needs to head in the same direction, but definitely an issue if you want the ships to create specific formations in flight. I guess you could find the longest minimum time and reduce other ships' accelerations to take that long. Jun 9, 2023 at 14:27
• @NuclearHoagie, indeed, and I have mechanisms for finding e.g. minimum accel of a group of ships and limiting them all to that, and will also build upon the above mechanisms for picking a "target point" and speed etc to maintain relative position. But this is the next step towards that :) (Also updated diagram to show the "overshoot" possibility which should converge on the line eventually anyway) Jun 9, 2023 at 14:29
• You may be interested in spaceship acceleration for following waypoints and how to maneuver an AI pirate ship for a sea battle. In both those answers I use a Bézier curve approach that is not quite a match for your ship's physics, but might provide a useful relaxation for a first approximation to the correct path, from which you can converge to a fully feasible trajectory. Jun 10, 2023 at 3:38