Training A Drone To Intercept A Moving Target
Drone Trainer is a custom Gymnasium environment for a question that sounds simple until physics gets involved: can a learning agent steer a pursuer drone through the position of a moving target in 3D?
Defining an interception
My first job was making “caught the target” precise. Checking only the distance between both drones at the end of a simulation step can miss a high-speed pass, or count a side-by-side near miss as a success.
The environment instead treats the pursuer’s movement during a step as a line segment. Success occurs when that segment passes through the target’s capture volume with the required closing behavior. That fly-through definition matches the task better and gives the tests a geometric condition to verify.
The target drone can follow straight, orbiting, or evasive scripts. The pursuer controls its own acceleration in 3D, while the environment handles kinematics, boundaries, termination, and reward reporting.
Taking away perfect information
The privileged debug observation exposes direct target state. That is useful when verifying the environment, but it makes the final task too easy and too unlike a camera-guided system.
The default observation is therefore viewport-based. The agent receives its own state plus information that would be available through a camera-like field of view: visibility, angle, a target estimate, and how long it has been since the target was seen. A short lock memory lets the agent continue a maneuver after the target leaves the viewport, but fresh target-relative state is not available forever.
This creates a more interesting loop. The policy must acquire the target, turn toward it, manage closing speed, and reacquire it when the geometry changes.
Reward shaping as debugging
A single reward at interception is technically clean and practically slow. I added dense signals for useful intermediate behavior, including first acquisition, visibility, reacquisition, closing on the target, and aiming through it. Losing the target can carry a penalty.
The important part was keeping the reward breakdown visible on every step. When a trained policy spins in place or learns to hover near the target without completing the intercept, I can inspect which reward it is exploiting instead of treating training as a black box.
The change shown below came from that process. The drone initially had a habit of spinning rather than committing to the interception. Adding lead-time gain and a maneuver threshold gave the controller a better way to decide when to lead the moving target and when to make a stronger correction.
Making the environment testable
The environment includes checks for Gymnasium API compliance, kinematics, reward terms, episode termination, and fly-through geometry. I also wrote small deterministic demos before relying on PPO training. A manual policy and regression scenario can prove that interception math works without waiting for a model to learn it.
That separation matters. If a policy fails, I want to know whether the problem lives in the learning algorithm, the reward, the observation, or the environment itself.
The current training path uses PPO through Stable-Baselines3. Straight target motion is the easier curriculum step; evasive motion comes after the agent learns the basic task. There is still plenty to tune, but the environment is now a useful place to run those experiments instead of a one-off animation.
Tech used
Python, Gymnasium, Stable-Baselines3, PPO, NumPy, Pygame, and Pytest.