Using physics/AI to maneuver a spaceship through a point with a specific velocity

Started by
2 comments, last by JoeJ 11 months, 2 weeks ago

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!

Advertisement

HateDread said:
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).

This is a good video about splines:

If you watch it, they mention a certain type of spline which is often used to model physics. I just can't remember which one, and never used splines for physics control problems myself. But i assume it gives you an easy to use and intuitive solution.

Personally i have addressed this same problem purely physically this way:
Input is current and target positions and velocities, plus a given constant deceleration and acceleration the controller is allowed to apply. You did not mention the latter, but notice you need some extra constraints, otherwise you have an underdetermined system (if i'm correct), which is hard to solve because you have infinite solutions.

With those inputs and considering the equation of motion under constant acceleration i get very smooth and natural output, which i use for things like procedural character animation and simulation.
Basically the solution gives a point in time where we need to switch from acceleration to deceleration to hit the target at desired velocity. If you would get acceleration from using thrusters, this would be like a hard switch of turning one set of thrusters off and another on, but they all run either at full power or are completely off. So while this is smooth and predictable to both program and player, it's surely not how real space ships navigate.
It's also a one dimensional equation. To use it for 3D points or rotations i just apply it to each dimension individually.
There are also cases where the math is right but results are not what we want. E.g. if current velocity is high but goes away from the target, the controller can correctly calculate multiple turning points. It will form something like an S curve, and finally hit the target as desired, but the complex curve feels no longer predictable to the observer. Probably you'll run into such issues no matter what methods you end up with, requiring some tweaking and hacks.

When i have plotted results of this control method for testing, i thought this would be useful for a smooth spline type as well. After having seen the video, i conclude that's very similar to what i did, but much easier. So i'd look up the spline thing first. Idk which extra constraint applies to the spline method, maybe something like minimizing acceleration.

Beside such methods to predict such smooth trajectories, another concept very useful for control problems is damping.
I use it for example if we are very close to the target already. In this case the perfect world analytical methods can't be applied because we run at quantized timesteps, so we get some error from missing the exact time of switching acc and deacc phases.
Related blog post: https://www.alexisbacot.com/blog/the-art-of-damping

[forum software crash…]

Another thing people often use for control problems is PD or PID controllers, but i have only little experience with this.
Like damping that's hard to predict, which can often be a problem depending on usecase.

This topic is closed to new replies.

Advertisement