2

i was given a homework to simulate a Guided missile following a target.

Answers in diffrent languages accepted. i just need to understand how to develop the right algorithm.

Problem Definition:

A target is seen at some (x,y) coordinates with constant speed V2 both taken as input by the user.

At this time a missile is launched at (0,0) with a user defined V1 constant speed to hit the target.

The target has to pass x=500 to escape the missile and should also keep a secure distance from the missile.

Develop a program to simulate this progress

If i let the target fly straight without considering the location of the missile it works fine. But considering the missiles location it flies in a weird way.

I wrote this code but the problem is that if the target arrives at around x=499 it just flies upwards and never crosses x=500.

My code so far (Qbasic):

CLS
SCREEN 12
WINDOW (-50, 600)-(600, -50)
LINE (0, 0)-(500, 0)
LINE (0, 500)-(0, 0)
INPUT "Speed of Missile, v1=", v1
INPUT "Speed of target, v2=", v2
INPUT "Coordinate x of the plane, x=", x2
INPUT "Coordinate y of the plane, y=", y2
CIRCLE (x2, y2), 1
x = 500: y = y2
xx1 = 0: yy1 = 0
xx2 = x2: yy2 = y2
dlt = 0.05
x1 = 0
y1 = 0
draww:
LINE (xx1, yy1)-(x1, y1), 3
LINE (xx2, yy2)-(x2, y2), 1
xx1 = x1: yy1 = y1: xx2 = x2: yy2 = y2

x1 = x1 + dlt * v1 * (xx2 - xx1) / SQR((xx2 - xx1) ^ 2 + (yy2 - yy1) ^ 2)
y1 = y1 + dlt * v1 * (yy2 - yy1) / SQR((xx2 - xx1) ^ 2 + (yy2 - yy1) ^ 2)

x2 = x2 + dlt * v2 * (x - xx2) / SQR((x - xx2) ^ 2 + (yy2 - yy1) ^ 2)
y2 = y2 + dlt * v2 * (yy2 - yy1) / SQR((x - xx2) ^ 2 + (yy2 - yy1) ^ 2)


Delay! = TIMER
DO
LOOP UNTIL TIMER - Delay! > 0
IF x2 >= x GOTO escaped
IF x1 > x2 GOTO caught
GOTO draww
caught: PRINT "Target was hit": GOTO endd
escaped: PRINT "Target escaped"
endd: END

Some weird results i get with this code:

http://prntscr.com/q8e0z1

http://prntscr.com/q8e1bn

How can i accomplish to keep some distance from the missile while flying towards x=500

Community
  • 1
  • 1
Therepower
  • 31
  • 1
  • @NicoSchertler: speed and velocity are not equivalent terms. Constant *speed* allows a change in heading. – Prune Dec 09 '19 at 17:19

0 Answers0