Guided missile physic

Monkey Forums/Monkey Programming/Guided missile physic

Rushino(Posted 2013) [#1]
Hello,

I have some question about physics of a missile or i would say behavior of a guided missile.

If i target an object that are in movement the missile must launch and rotate until it is aligned to that object all the time while thrusting forward.

My problems are...

1- How i detect that an object is aligned to another object or pointing in the direction of that object ?

2- Also.. how to detect which side the missiles must turn or rotate when i launch it ?

Thanks!


Xaron(Posted 2013) [#2]
I did this and can post some code tomorrow.

A first solution would be to just follow the target until it's hit.
I have another solution where the missile uses the target vector/lead angle to compute the point where the target will be when the rocket is there.

Is that 2d or 3d what you need?


mteo77(Posted 2013) [#3]
Hello.
One solution would be to check the distance of your missile against that object.
To do that you have to assign a specific instance of the target when the missile is created so you can check the coordinates of it and change direction accordingly.
One way of doing it would be to have a kind of "lock on" code.
So let's say when the missile get created check it's coordinates against the list of targets, and return the right one assigned to a local variable, so you ca start using it's values.
Hope it make sense


Paul - Taiphoz(Posted 2013) [#4]
http://www.monkeycoder.co.nz/Community/posts.php?topic=5558

Function TurnTo:Float(_start : Float, _End : Float, _step : Float)
	Local min:Float = -180;
	Local max:Float = 180;
	Local half:Float = 0
	Local retval:Float = 0.0;
	
 	If (_End - _start) < -half And (_End - _start)>min And (_End - _start)<max Then
			retval = (_start-_step)
			
		Elseif (_End - _start) > half And (_End - _start)>min And (_End - _start)<max Then
			retval = (_start+_step)	
					
		Elseif (_End - _start) < min Then
			
			retval = (_start+_step)
			Repeat
				If retval<-180 Then retval+=360
				If retval>180 Then retval -=360
			Until retval>=-180 And retval<=180
			
		Elseif (_End - _start) > max Then

			retval = (_start-_step)
			Repeat
				If retval<-180 Then retval+=360
				If retval>180 Then retval -=360
			Until retval>=-180 And retval<=180
						
		Else
			retval = _start
	Endif
 	
   	Return retval
End



Gerry Quinn(Posted 2013) [#5]
The simplest option is to try to move to where the target currently is (and in many games, this will also create more fun).

The more sophisticated option is to try to move to where the target will be when you meet.


GW_(Posted 2013) [#6]
direction += Sgn(direction-(Sgn(direction-target)*180)-target)*stepvalue



Paul - Taiphoz(Posted 2013) [#7]
That is for sure a nice function, but it results in that anomaly where your unit eventually gets caught in a never ending loop never quite able to reach its destination.


Gerry Quinn(Posted 2013) [#8]
I guess you could just home in directly once you are within a certain range.


zoqfotpik(Posted 2013) [#9]
Having an accelerating missile home in on a moving target is a somewhat nontrivial problem and is more difficult than having a purely ballistic object (eg a bullet) hit its target.

It will be much easier if you have fuzzy outputs for the directional thrust-- if I am pointed very close to (targetx + targetvx, targety + targetvy) my directional thrusters will fire softer or at a much decreased pulse rate.

Also you can find the change in angle between your target and your missile [transversal] using arctan, then have your missile's turn direction be the same as that angle change, with a magnitude that is proportional to that angle change.

Depending on the application it might be ok to fudge it a little by nudging the missile's position toward the target. This won't really be noticeable if it's subtle enough.

If your missile is going to intersect its target's position at current velocity without taking thrust into account, you could cut your thrust and let it continue on a ballistic path momentarily. This will avoid the problem of your missile overcorrecting its path again and again.

Since this is a video game, the easiest solution is to abandon missile thrust entirely and just move the missile's coordinates toward the target coordinates. Then you find the angle between the missile's old position and its new position and rotate the graphic of the missile to fit. If you are positing highly accurate and maneuverable missiles this might be just fine. If you want them to be somewhat inaccurate, have them track on the target's current position instead of its position + velocity.