A long time ago someone asked me something like "How does the Bad Guy AI know where to go to get the good guy?" The simplest way to do this is simply something like this:
If BadGuyX>GoodGuyX then BadGuyX=BadGuyX-1
If BadGuyX<GoodGuyX then BadGuyX=BadGuyX+1
If BadGuyY>GoodGuyY then BadGuyY=BadGuyY-1
If BadGuyY<GoodGuyY then BadGuyY=BadGuyY+1
This is a great start, but what happens if you use this code is the Bad Guy will go diagonally until the X or Y is the same as the Good Guy's, and then he will go straight. This looks really funny and unnatural, because basically, it is moving in only eight possible directions. We don't want the Bad Guy to move robotically, unless he's a robot of course.
What we want is a smooth movement from the Bad Guy's current position, to the Good Guy's position, only in a proportional way, so the Bad Guy moves at a diagonal straight at the Good Guy. How do we figure out what angle to use and how to move the Bad Guy? I am working on a similar problem in my current game. I need to start a bad guy in a random position, and then move toward the good guy.
One of the best ways to figure something out is to go out of your main program and make a small demo to test the concept. Show all the variables onscreen so you can see where the numbers are wrong. Once you figure it out, put the code in the main program.
Here is the code I've come up with to smoothly move the bad guys:
;XYChange
Graphics 800, 600
CenterX=400
CenterY=300
SlopeM#=0
DeltaX#=0
DeltaY#=0
XChange#=0
YChange#=0
SX#=400
SY#=300
X=400
Y=300
SetBuffer BackBuffer()
While Not KeyHit(1)
Cls
MX=MouseX()
MY=MouseY()
DeltaY#=MY-SY
DeltaX#=MX-SX
If (Abs(DeltaX) > Abs(DeltaY)) Then ; slope < 1
SlopeM# = DeltaY / DeltaX ;compute slope
YChange=SlopeM#
If DeltaX>=0 Then
XChange=1
Else
XChange=-1
EndIf
If DeltaY>=0 Then ;if Y Delta is positive,
YChange=Abs(YChange) ;make sure YChange is positive
ElseIf DeltaY<0 And YChange=>0 Then ;if Y Delta is negative,
YChange=YChange*-1 ;make sure YChange is negative
EndIf
LessGreater=-1
Else ;slope >= 1
SlopeM# = DeltaX / DeltaY ;compute slope
XChange=SlopeM#
If DeltaY>=0 Then
YChange=1
Else
YChange=-1
EndIf
If DeltaX>=0 Then ;if X Delta is positive,
XChange=Abs(XChange) ;make sure XChange is positive
ElseIf DeltaX<0 And XChange=>0 Then ;if XDelta is negative,
XChange=XChange*-1 ;make sure XChange is negative
EndIf
LessGreater=1
EndIf
Color 255, 255, 255
Line CenterX, CenterY, MX,MY
Color 255,0,0
; Plot X, Y ;this dot is very small and hard to see, so...
Oval X-3, Y-3, 6, 6, 1 ;make a dot big enough to easily see, -3s to put the dot in the middle
Color 255, 255, 255
Text 10,40, " MX=" + MX + " MY=" + MY
Text 10,60, "DeltaX#=" + DeltaX + " DeltaY#=" + DeltaY
Text 10,80, "SlopeM#=" + SlopeM#
If LessGreater=-1 Then
Text 10,100, "Slope<1"
Else
Text 10,100, "Slope>=1"
EndIf
Text 10,120, "XChange#=" + XChange# + " YChange#=" + YChange#
Text 20,160, "Hold the Left Mouse Button to Make the dot chase the mouse pointer"
If MouseDown(1) Then
SX#=SX#+XChange#
SY#=SY#+YChange#
X=SX#
Y=SY#
EndIf
Flip
Wend
When you run it, you will see the red dot in the middle that represents a bad guy. the mouse pointer is where the good guy is. Hold down the mouse button to have the bad guy chase the good guy. If you move the pointer, the bad guy will recalculate the angle and still head straight for the pointer. Press ESC to quit.
Recent Comments