Click here to Skip to main content
15,898,134 members
Articles / Programming Languages / C#

Hitting a Moving Target: The Missile Guidance System

Rate me:
Please Sign up or sign in to vote.
4.95/5 (80 votes)
14 Aug 2007CPOL27 min read 330.3K   5.7K   160  
The Mathematics of Targeting and Simulating a Missile: From Calculus to the Quartic Formula
using System;
using System.Collections.Generic;
using System.Drawing;

namespace SimpleGuidanceSystemUI
{
	class Target : Shooter
	{
		public double DeltaTheta = Math.PI / 8.0;
		public double Acceleration = 0.5;

		public enum TurnType
		{
			Left,
			Right,
			None
		}

		public enum ThrustType
		{
			Forward,
			Backward,
			None
		}

		public Target(double pX, double pY, double vX, double vY, double Theta)
			: base(pX, pY, vX, vY, Theta)
		{
			this.pX = pX;
			this.pY = pY;
			this.vX = vX;
			this.vY = vY;
			this.Theta = (Theta + Math.PI) % (2.0 * Math.PI);
		}

		public override void OffsetX(double value)
		{
			this.offsetX += value;
			this.lastX += value;
		}

		public override void OffsetY(double value)
		{
			this.offsetY += value;
			this.lastY += value;
		}

		public void Update(TurnType Turn, ThrustType Thrust)
		{
			double deltaTheta = 0.0;
			switch (Turn)
			{
				case (TurnType.Left):
					deltaTheta = DeltaTheta;
					break;
				case (TurnType.Right):
					deltaTheta = -DeltaTheta;
					break;
			}
			switch (Thrust)
			{
				case (ThrustType.Forward):
					base.Update(Acceleration, deltaTheta, true);
					break;
				case (ThrustType.Backward):
					base.Update(-Acceleration, deltaTheta, true);
					break;
				default:
					base.Update(0.0, deltaTheta, true);
					thrusting = false;
					break;
			}
		}

		private const double DRAWSCALEFACTOR = 10.0;

		public override void DrawOn(Graphics g)
		{
			g.FillPolygon(thrusting ? Brushes.Orange : Brushes.Red, new Point[] {
				new Point((int)(this.drawX + 2.0 * DRAWSCALEFACTOR * Math.Sin(this.Theta)), (int)(this.drawY + 2.0 * DRAWSCALEFACTOR * Math.Cos(this.Theta))),
				new Point((int)(this.drawX + DRAWSCALEFACTOR * Math.Sin(this.Theta + 1.0 * Math.PI / 8.0)), (int)(this.drawY + DRAWSCALEFACTOR * Math.Cos(this.Theta + 1.0 * Math.PI / 8.0))),
				new Point((int)(this.drawX + 2.0 * DRAWSCALEFACTOR * Math.Sin(this.Theta + 3.0 * Math.PI / 8.0)), (int)(this.drawY + 2.0 * DRAWSCALEFACTOR * Math.Cos(this.Theta + 3.0 * Math.PI / 8.0))),
				new Point((int)(this.drawX + 0.7 * DRAWSCALEFACTOR * Math.Sin(this.Theta + 9.0 * Math.PI / 16.0)), (int)(this.drawY + 0.7 * DRAWSCALEFACTOR * Math.Cos(this.Theta + 9.0 * Math.PI / 16.0))),
				new Point((int)(this.drawX + DRAWSCALEFACTOR * Math.Sin(this.Theta + 3.0 * Math.PI / 4.0)), (int)(this.drawY + DRAWSCALEFACTOR * Math.Cos(this.Theta + 3.0 * Math.PI / 4.0))),
				new Point((int)(this.drawX + DRAWSCALEFACTOR * Math.Sin(this.Theta + 5.0 * Math.PI / 4.0)), (int)(this.drawY + DRAWSCALEFACTOR * Math.Cos(this.Theta + 5.0 * Math.PI / 4.0))),
				new Point((int)(this.drawX + 0.7 * DRAWSCALEFACTOR * Math.Sin(this.Theta + 23.0 * Math.PI / 16.0)), (int)(this.drawY + 0.7 * DRAWSCALEFACTOR * Math.Cos(this.Theta + 23.0 * Math.PI / 16.0))),
				new Point((int)(this.drawX + 2.0 * DRAWSCALEFACTOR * Math.Sin(this.Theta + 13.0 * Math.PI / 8.0)), (int)(this.drawY + 2.0 * DRAWSCALEFACTOR * Math.Cos(this.Theta + 13.0 * Math.PI / 8.0))),
				new Point((int)(this.drawX + DRAWSCALEFACTOR * Math.Sin(this.Theta + 15.0 * Math.PI / 8.0)), (int)(this.drawY + DRAWSCALEFACTOR * Math.Cos(this.Theta + 15.0 * Math.PI / 8.0)))
			});
			g.DrawLine(Pens.Yellow, new Point((int)this.drawX, (int)this.drawY), new Point((int)this.LastX, (int)this.LastY));
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions