# Controlling a Spaceship to Intercept a Target

Posts: 8Member
edited September 2019

Hi everyone, I'm working on a kind of 2D space dogfighting game with asteroid-style newtonian physics.

I wanted to show off an algorithm I've written to control the motion of spaceships. Check out the video on YouTube here. Apologies for the bad frame rate, it's the best I could do with my laptop and the video capture program I am using. If you want to see the demo in full FPS, I've included my Godot project below, you can download it and run it yourself.

The spaceships are trying to intercept a target (the white dot) moving in a circle at constant speed. The acceleration of the target's circular motion is very close to the max acceleration allowed to be produced by the spaceships, so it's actually a very challenging problem.

To demonstrate that this is not as easy as it might seem, I've included two (translucent) ships using naive control. One of them just accelerates straight towards the target (pure pursuit), while the other one tries to pursue the target while also matching its velocity.

The opaque spaceship uses my algorithm, which is a kind of modified PD controller that also matches velocity in addition to position, with dynamically scaling gain. I love how the controller directs the spaceship to circle around and gently intercept the target (a must since the acceleration of the target is so close to the limit), without knowing ANYTHING about how the target is moving other than it's current position and velocity, and without overshooting and going crazy like the other two spaceships.

Anyways, the next thing I'm looking forwards to is programming AI to evade bullets and eventually guided missiles, as well as programming APN guidance for the missiles.

• Posts: 8Member

Also, if anyone is interested in the code, you can download the project here!

To run it, open up the Demos/MotionDemo/MotionDemo.tscn scene and run (F6).

• Posts: 8Member

Another thing that's really cool, that you can find in the code, is that I can define a set of thrusters for a spaceship - their direction and max thrust. Then, given a desired acceleration, I can use Transform2D.AffineInverse() to find out which two thrusters I need to fire in order to produce that acceleration!