Code archives/Algorithms/Homing Missile Algorithm

This code has been declared by its author to be Public Domain code.

Download source code

Homing Missile Algorithm by Mr Brine2004
See the example
; (c)oded by Mr Brine
;

Graphics 640, 480

SetBuffer(BackBuffer())

Global o.gradobj = New gradobj

Repeat

	Cls

	Text 0, 0, "* = your ship"
	Text 0, 12, "# = homing missile"
	Text 0, 24, "click the lmb to make homing missile home in on your ship"
	Text 0, 36, "press esc to quit"


	If(MouseHit(1)) CalcGrad(o, MouseX(), MouseY())
	
	Text MouseX(), MouseY(), "*"
	Text o\x, o\y, "#"

	AddGrad(o, 2)

	Flip
	VWait
	
Until KeyHit(1)


; ----------------------------------------------------------------------------------------------------


Type GradObj
	
	Field x#, y#
	Field xg#, yg#
	Field xd#, yd#
	Field ld#
	
End Type



Function CalcGrad(o.GradObj, newx#, newy#)

	o\xd = newx - o\x
	o\yd = newy - o\y

	o\ld = Sqr(o\xd * o\xd + o\yd * o\yd)

	o\xg = o\xd / o\ld
	o\yg = o\yd / o\ld

End Function 



Function AddGrad(o.GradObj, speed#)

	o\x = o\x + o\xg * speed
	o\y = o\y + o\yg * speed
	
End Function

Comments

Floyd2004
The uploaded code is broken.

All the \ characters are gone, e.g. o\x has become ox.


Mr Brine2004
Thanks floyd, now fixed!


elseano2004
Thanks!


Code Archives Forum