Missile Guidance

Alright, so I’ve been working on a missile for a couple days now, and I’ve come to this code. I’ve been trying to get the missile to properly lock in and track onto the target. However, I’ve repeatedly been having the problem that the missile will track just fine, if the target is moving. However, if the missile falls into a circle around the target, it will simply close in until it reaches its maximum turning speed. Sure, I could increase the turning speed to a high amount, but I’m not interested in doing that. I also know that this doesn’t predict velocity. It’s not something I’m worried about.

I’d like some kind of change to the code that will cause the missile to stop orbiting, and either close in or do a longer loop. I realize it probably has more to do with mathematics than with lua, but it is a problem nonetheless. Any help or suggestions would be welcome. Here is the functional part of the code. It goes in ENT:PhysicsUpdate, if you’re interested. The target is acquired through a trace, for now.

	if self.target and self.target:IsValid() then 
		local TargetPhys = self.target:GetPhysicsObject()
		local TargetPos = TargetPhys:GetPos()
		local CurrentPos = phys:GetPos()
		local CurrentAngles = self.Entity:GetAngles()
		local Distance = (TargetPos - CurrentPos):Length()
		local DesiredAngle = (TargetPos - CurrentPos):Angle()
		DesiredAngle.p = math.AngleDifference(DesiredAngle.p, CurrentAngles.p)
		DesiredAngle.y = math.AngleDifference(DesiredAngle.y, CurrentAngles.y)
		DesiredAngle.r = math.AngleDifference(DesiredAngle.r, CurrentAngles.r)
		local FinalAngle = Angle(DesiredAngle.r, DesiredAngle.p, DesiredAngle.y) * (0.25 + (200/Distance)) // <-- This is actually the important part, the modifier here determines the turn speed.
		phys:AddAngleVelocity((phys:GetAngleVelocity() * -1) + FinalAngle)
		phys:AddVelocity(self.Entity:GetForward() * 1000)


	phys:AddVelocity(self.Entity:GetForward() * 1000)


I’m also open to other methods of guiding the missile entirely. Constant forward velocity and an angular velocity don’t really appeal to me anyways.



Been there, done that, didn’t like it.

It was difficult at best to put proper restrictions on it. The problem is that since it always gives a force directly to the target, it’s very difficult to make it fly realistically. I already have one written up and commented out in my code. I don’t particularly want to put it back.

EDIT: Problem solved. Thanks.