follow an attack pattern

Blitz3D Forums/Blitz3D Beginners Area/follow an attack pattern

Steve Elliott(Posted 2004) [#1]
Has anybody got any code for moving enemies along a path?

I've written some code that does this - and rotated my sprite 360 degrees in 10 degree increments so if follows this path.

But the movement of the alien is obviously following a set of waypoints and is not smooth.

I've also been able to move an alien along a sine wave and this was much smoother - but this can only move along a sine wave horizontally or vertically.

I guess maths is the answer - but I'm hopeless at maths!

btw I'm talking about 2d here - not 3d.

Thanks


Ross C(Posted 2004) [#2]
Here what i proposed to do with my camera system. It's in 3d, but princible remains the same.

Have a very small image, make it invisble if you wish, or just use an X and Y. Get this to follow the waypoints exactly. Now, get the actual enemies to move to the location of the point. Every time the point moves, read adjust the angle and move the enemy towards the point.

Move the enemy slightly slower, not much, than the point. The enemy should look as if it's moving smoothly.


puki(Posted 2004) [#3]
You may want to nose through this lot:

http://www-cs-students.stanford.edu/~amitp/gameprog.html

I haven't looked through it all - but I think there is a lot of good stuff in there about various things that people want to know the answer to. You'll probably find it is all C++ stuff though. I like the site though - interesting reading.


Ross C(Posted 2004) [#4]
Here's some code. It's my waypoint editor, modified. Click the mouse to put down waypoints. Then press the space bar to get the enemy (Big square) to smoothly (sorta) follow. Press Return to reset, and space bar to play again. You can add waypoints anytime you wish :)

Graphics 800,600
SetBuffer BackBuffer()

Global max_path_points=100

Dim pathx#(max_path_points-1); set up 101 path points
Dim pathy#(max_path_points-1); -1 because arrays start at zero

Global path_counter=-1
Global current_path=0
Global cx#=0
Global cy#=0
Global ex#=400
Global ey#=100
Global movement_started=0


While Not KeyHit(1)
	Cls
	
	If MouseHit(1) Then
			setpath(MouseX(),MouseY()); set path point with the mouses current coords
	End If
	
	If KeyHit(57) And movement_started=0 And path_counter>0 Then; if spacebar pressed then move rectangle
		current_path=0; reset the current path to 0
		cx=pathx(0); set the rectangles coords to the first path point
		cy=pathy(0)
		movement_started=1; set flag for movement started
		ex=400
		ey=100
	End If
	If KeyHit(28) Then; if the enter key is pressed then stop movement
		movement_started=0; clear flag for movement
		cx=pathx(0); set rectangle coords to the first path point
		cy=pathy(0)
		current_path=-1; set current path to none exsistant
	End If

	
	
	
	draw_path_points(); function for drawing the pathpoints
	If movement_started=1 Then move(); draw the moving rectangle and move it
	Color 200,200,200; set drawing color
	Rect MouseX()-1,MouseY()-1,2,2; draw mouse location with a rectangle
	Text 0,0,"Click mouse to set path point. Press spacebar to start animation. Press enter to stop."
	Text 0,10,"movement started="+movement_started
	Flip
Wend
End

Function move()
	ang#=0
	If current_path<path_counter Then; make sure the current path point is less than the highest
		ang=ATan2(pathy(current_path+1)-pathy(current_path),pathx(current_path+1)-pathx(current_path))
		;/\ basically means find the angle between the current path point and the next one /\
		cx=cx+Cos(ang)*3; increase the x coord of the rect following the path points based on the angle
		cy=cy+Sin(ang)*3; increase the y coord of the rect following the path points based on the angle
		Color 100,200,100; change drawing color
		Rect cx-3,cy-3,6,6; draw the rectangle central to it's coords
		If Abs(cx-pathx(current_path+1))<2 And Abs(cy-pathy(current_path+1))<2 Then
			; /\ if the rects coords are with a 1 pixel distance of the path point then /\
			current_path=current_path+1; increase the current path point number
			cx=pathx(current_path); set the rectangles coords to that of the next path point
			cy=pathy(current_path)
		End If
	End If
	angle=ATan2( cy - ey, cx - ex ); find the angl between the enemies and the waypoint follower
	dist=Sqr( (ex-cx)*(ex-cx)+(ey-cy)*(ey-cy) ); find the distance between them
	DebugLog dist
	ex=ex+(Cos(angle)/1)*(dist/30); move the enemy at speed relative to the distance
	ey=ey+(Sin(angle)/1)*(dist/30); move the enemy at speed relative to the distance
	Rect ex-15,ey-15,30,30; draw the enemy
End Function

Function setpath(x,y)
	path_counter=path_counter+1; increase counter
        If path_counter>max_path_points-1 Then; if counter is higher than the available number of path points then set it back
               path_counter=max_path_points-1
        Else; else 
	       pathx(path_counter)=x; record the x and y positions
	       pathy(path_counter)=y
        End If
End Function

Function draw_path_points()
	For loop=0 To path_counter; loop through all the path points
		Color 100,100,200
		Rect pathx(loop),pathy(loop),2,2
		If loop<path_counter Then; do this loop while the loop value is less than the highest path value
			Rect pathx(loop+1)-1,pathy(loop+1)-1,2,2; draw a rectangle at the path point so its central
			Color 50,50,150; change drawing color
			Line pathx(loop),pathy(loop),pathx(loop+1),pathy(loop+1); draw a line between the current point and the next one
		End If
	Next
End Function



cermit(Posted 2004) [#5]
It looks funny when the big box is chasing the little :) its cool


Steve Elliott(Posted 2004) [#6]
Excellent - thanks Ross!


puki(Posted 2004) [#7]
That is quite good "Ross". Maybe you should put it in 'code archives'. Plus stick your name in there at the top. Too many people post code without putting a comment in with their name (in the actual code block). I have a lot of code on my hard-drive which is anonymous - I don't know who wrote half of it.


Agamer(Posted 2004) [#8]
yeh ross u come up with loads of kwl things. I know I ahve found them usefull


Ross C(Posted 2004) [#9]
;)

I'll stick it in the code archives then. Thanks guys.


Zace(Posted 2004) [#10]
WOW, just tried this code - its great, simple and easy tro adapt.

Thanks Ross.


wmaass(Posted 2004) [#11]
For a blithering idiot like myself, how can this be adapted for 3D? I could really use that.


wmaass(Posted 2004) [#12]
Never mind all, I found some other stuff that Ross did that works.


sswift(Posted 2004) [#13]
Hey Wmaass... Your paypal email says it's adelphia, but it comes back as a bad address when I try to reply to you. I tried to reply to your address in your profile, but have gotten no reponses yet. Send me an email from a legitimate address so I can reply to you. :-)


wmaass(Posted 2004) [#14]
Sorry sswift, I just sent you mail.