Predictable autopilot AI for a physics-based spaceship moving on a 2D plane

Started by
14 comments, last by emirtaessss 10 months ago

I found the following educational resources:

Do you think any of those could be useful?

Advertisement

Typical real-world spacecraft control applies to enormous distances, days or weeks of travel, and slow maneuvers with a tight fuel budget: just about the opposite of an action game.

Instead of designing optimal control schemes, focus on respecting constraints and physical accuracy and on controlling the player's spaceship reliably and enemy spaceships with imperfections that the player can learn and exploit (e.g. tricking them into crashing or losing good positioning during a dogfight).

Omae Wa Mou Shindeiru

@matskosan How are you going with this? I'm trying to solve a very similar problem! Just without the goal of stopping on a position; more like passing through it with some desired velocity, and I also have orientation constraints like you (namely, at present, the ability to thrust only in a ship's forwards dir, for initial simplicity). As well as maximumDegPerSecond for rotation and maxSpeedChangePerSecond for linear movement (rather than going as far as trying to apply forces/accelerations, even if that's “better”. I have enough to worry about!). Trying to avoid real-life spaceship control theory as that seems nuts and way overkill when we could, potentially, touch velocity directly by “magic” but still respect constraints and give weighty movement.

I'm trying to change my iterative approach that kind of abuses my math functions to push the ship in a direction, eventually by way of clamping slowly chipping away at velocity's other directions until it's only going forwards, to something more numerical, kinda like what you're saying.

I'm thinking;

  • We have current position P1 with initial velocity V1, and target position P2 (ideally with target velocity V2 but unsure how to use it atm)
  • We find some velocity that gets us from P1 to P2 (somehow clamping to our linear speed limits)
  • We calculate numTicks to turn to face opposite the velocity component we're trying to cancel out, then the number of ticks to actually cancel it out
  • We repeat the same for the other velocity component
  • Thus predicting the path becomes a few additions of time/ticks, and while it isn't a single equation, it's at least not 100% iterative (which is my current solution, and even one ship turning kills the frametime)

I'm not sure yet how to incorporate a target speed/velocity/direction into the point intersection, nor how to find the “optimal” path. How did you go? We can share notes and implementations if you'd like - I know how hard it can be to get assistance with such a weird problem! Cheers.

Two approaches that haven't been mentioned:

1) Implement your simulation in a way that allows you to compute gradients, for instance implementing it in Torch or TensorFlow, or using a C++ automatic differentiation library. Then you can write an objective function and optimize your path using gradient descent. I believe this is not far from how NASA optimizes trajectories.

2) Describe your problem as Gym environment (a Python class that inherits from gym.Env). You can then use Reinforcement Learning algorithms to train an agent that will solve any instances of your problem, for instance by picking a ready-made algorithm from Stable-Baselines3.

I've been playing around with 2) for the last few days and I think something like PPO (one of the RL algorithms implemented in Stable-Baselines3) would work well.

HateDread said:
As well as maximumDegPerSecond for rotation and maxSpeedChangePerSecond for linear movement (rather than going as far as trying to apply forces/accelerations, even if that's “better”. I have enough to worry about!). Trying to avoid real-life spaceship control theory as that seems nuts and way overkill when we could, potentially, touch velocity directly by “magic” but still respect constraints and give weighty movement.

If you depart arbitrarily from physical laws, your simulation will be more complicated because it will be a pile of hacks, more difficult to write because you won't be able or willing to follow established algorithms that do the right thing, less dependable because it will be incorrect with unnatural failure cases and exploits.

Particularly if you want to apply machine learning, you need the solid foundation of correct dynamics and straightforward equations.

Omae Wa Mou Shindeiru

@HateDread So far we went a maths way and implemented the required thrust calculation using third-degree Newtonian motion formulas - thrusters then change the applied acceleration linearly using constant jerk. This works fantastic for single axes but becomes complicated when for example maximum possible thrust on the velocity axis changes because of rotation, which causes ships to “overshoot” beyond the target and fly in circles. For now, such cases are ignored because it seems to be exactly what we need for player-controlled gameplay purposes… Later we will need to implement some sort of logic to control velocity on approach though.

@alvaro very curious about the approach you are taking with ML! I was thinking about applying some ML but unfortunately lack any knowledge in the area. Would be glad if you could share the results of your work.

This topic is closed to new replies.

Advertisement