Waypoint help

Blitz3D Forums/Blitz3D Beginners Area/Waypoint help

wizzlefish(Posted 2004) [#1]
I have TWO questions on simple topics:

1. How would I create a simple waypoint? I want to make it so that a plane flies overhead. We have the models, we just need the code.

2. Also, how would I make a gun kick? Does it involve animation?


semar(Posted 2004) [#2]
1.
- set up a waypoint type to store the waypoint pivot entity. The plane should always point toward the current waypoint, and once reached, you should switch to the next waypoint until you reach your final target.
type t_wayp
field pivot
Global wayp.t_wayp

for n = 1 to num_of_waypoints_to_create
wayp.t_wayp = new t_wayp
wayp\pivot = createpivot() ;or createcube for testing
positionentity wayp\pivot,where_you_like_x,where_you_like_y,where_you_like_z
next


..
if entitydistance(my_plane,wayp) < minimum_distance then
wayp\reached = true
wayp.t_wayp = next t_wayp
if not wayp then
;end of waypoint list reached
endif
else
pointentity my_plane,wayp\pivot
endif


2.
An easy way would just involve a simple moveentity back and forth - the following example is intended to make a kick back and forth on the z-axis:
global kick = false
global kick_amount = 0
global kick_max_amount = 10
global kick_speed = 2
global delta = 10 ;limits the movement
global z_memo# ;holds the previous gun's z position
; 
;
if shoot then
kick = true
kick_amount = kick_max_amount
z_memo = entityz(my_gun)
endif
;
;
if kick then
positionentity my_gun,x,y,z_memo - kick_amount/delta
kick_amount = kick_amount - kick_speed
if kick_amount <= 0 then
kick = false
endif
endif



A more refined way is to move the gun using a sin() or cos() function in order to smooth the movement - that is, make the movement with a fast start and a slow end.

Sergio.


wizzlefish(Posted 2004) [#3]
I'm 11 - not into trig yet :)


ckob(Posted 2004) [#4]
theres a couple waypoint examples in blitz3d's sample folder look for the gargoyle example


puki(Posted 2004) [#5]
I did a waypoint code archive - nothing special - but it might help:
http://www.blitzbasic.com/codearcs/codearcs.php?code=968

I did mine on the basis that I thought "Simon Harrison's" was to hard to decode. Some people may not like my version.


wizzlefish(Posted 2004) [#6]
Thanks guys - I think I have a grasp on this :)

But any more simple help would be useful.


Ross C(Posted 2004) [#7]
Hey, create an array with 2 dimensions:

Dim waypoints(10,2) ; create an array to hold 11 way points


the last dimension will be the X - 0 Y - 1 and Z - 2

Create a global variable to hold which waypoint your on.
Global WayPoint = 0


simply increase the global and pull out the co-ords from the array

WayPoint = WayPoint + 1
x = waypoints(WayPoint,0)
x = waypoints(WayPoint,1)
x = waypoints(WayPoint,2)



You could probably create a pivot and place it at the location of the way point and point and move the entity towards the pivot :o)


Ross C(Posted 2004) [#8]
Here's some code i had lying about.

Left click to put down a way point. Press space to start the waypoint following. Space to Stop and start again.

; **** 2D waypoint editor, modified. Original Code by Ross C *****
; ****                 Modified code by Ross C               *****
; ****     create smooth transitions between way points      *****


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




puki(Posted 2004) [#9]
It's missing an 'End Function' after the final 'Next'.

Edit:
and what's with the 'Original Code by Ross C - Modified code by Ross C'?

How about adding 'designed and implemented by Ross C'? - that's what 'John Carmack' does.


wizzlefish(Posted 2004) [#10]
how the heck do you copy stuff to blitz? it always comes in one line!!!


semar(Posted 2004) [#11]
From Blitz IDE - Edit - Paste.

Don't use CTRL-V to paste in the IDE editor, otherwise it goes messy.

I'm 11 - not into trig yet :)

It's time to begin m8 !

;-)

Sergio.


Ross C(Posted 2004) [#12]
Yeah, i kind of modified it to do something else :S


wizzlefish(Posted 2004) [#13]
thanks