Path

Blitz3D Forums/Blitz3D Programming/Path

Alberto(Posted 2004) [#1]
Hello

How to create a character following a pre-fabricated path at a variable speed?

thanks


Ross C(Posted 2004) [#2]
Use way points. Create an array:

Dim waypoint(100,2) ; 101 way points 2= x,y and z


then fill each part of the array with the way points co-ords. You'll probably read these in from data statements or what not :)

You could use types as well:

Type waypoint
   Field x,y,z
End Type


w.waypoint=new waypoint
w\x=100
w\y=100
w\z=10


for each way point.

Then you would have a global variable which kept track of which waypoint you were on. Also, have a pivot (just one), and move it to each new way point, and point the character at the pivot. Then just move the character towards it :)


Alberto(Posted 2004) [#3]
RossC
Thanks for your suggestion
My path should be a spline , I am concerned that a lot of waypoints are necessary to create smooth curves
what do you think?


CyberHeater(Posted 2004) [#4]
Ross C: Sweet mate. I like the idea.

Alberto: Is this some kind of race game your making?


Ross C(Posted 2004) [#5]
Hey Alberto, this should maybe get you started, whipped it up in 10 mins, so it doesn't have smooth path following ;)

Press 1 to reveal the waypoints locations :)

Graphics3D 800,600
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Global cam=CreateCamera()
PositionEntity cam,0,7,-20

light=CreateLight()

; SET UP SOME CUBES, JUST FOR SCENARY PURPOSES, TO HIGHLIGHT THE MOVEMENT
Dim cubes(10)
For loop=0 To 10
	cubes(loop)=CreateCube()
	PositionEntity cubes(loop),Rand(-15,15),1,Rand(-17,17)
	EntityAlpha cubes(loop),0.2
Next
;/ END OF CUBES SET UP

; CREATE THE 'PLAYER'
Global sphere=CreateSphere()

; SET THE NUMBER OF WAYPOINTS YOU WANT
Global no_of_waypoints=10 ; number of waypoint you want, - 1

; SET UP THE WAYPOINT DATA
Dim waypoints#(no_of_waypoints,3); the 3 is for X Y & Z co-ords and the speed
                                ; 0 = x
                                ; 1 = y
                                ; 2 = z
                                ; 3 = speed
waypoint_data(no_of_waypoints); get the waypoint positions and speed

; SET UP THE WAYPOINT MARKERS. SIMPLY A VISUAL REPRESENTATION OF WHERE THE WAYPOINTS ARE, NOTHING MORE
Dim waypoint_markers(no_of_waypoints); visible representations of the waypoints, purely for clarity
For loop=0 To no_of_waypoints
	waypoint_markers(loop)=CreateSphere(2)
	EntityAlpha waypoint_markers(loop),0
	EntityColor waypoint_markers(loop),50,50,200
Next

position_markers(no_of_waypoints); position the waypoint markers


; CREATE THE PIVOT THAT THE PLAYER WILL GOTO. THIS PIVOT IS SET TO THE CURRENT WAYPOINT POSITION, WHICH
; THE PLAYER POINTS TO AND FOLLOWS
Global waypoint_pivot=CreatePivot()
Global waypoint_number=0 ; what waypoint the program will start on

While Not KeyHit(1)


	If KeyDown(2) Then ; IF KEY 1 IS PRESSED THEN
		show_waypoints() ; SHOW THE WAY POINTS
	End If
	
	update_waypoints(waypoint_number); UPDATE THE WAYPOINTS AND THE PLAYER FOLLOWING THEM
	PointEntity cam,sphere; POINT THE CAMERA AT THE SPHERE
	UpdateWorld
	RenderWorld
	Flip
Wend
End

Function show_waypoints()
	For loop=0 To no_of_waypoints
		EntityAlpha waypoint_markers(loop),0.6
	Next
End Function

Function waypoint_data(number) ; Set random waypoint positions and a random speed for each waypoint

	For loop=0 To number
		waypoints(loop,0)=Rnd(-20,20) ; x
		waypoints(loop,1)=Rnd(-7,9)   ; y
		waypoints(loop,2)=Rnd(-20,20) ; z
		waypoints(loop,3)=Rnd(0.1,0.5) ; random speed
	Next

End Function

Function position_markers(number); simply position the markers at the waypoint locations

	For loop=0 To number
		PositionEntity waypoint_markers(loop),waypoints(loop,0),waypoints(loop,1),waypoints(loop,2); position the way point markers
	Next

End Function



Function update_waypoints(waypoint_no) ; update the player, so he follows each waypoint

	; Line Below: position the waypoint pivot at the location of the current waypoint
	PositionEntity waypoint_pivot,waypoints(waypoint_no,0),waypoints(waypoint_no,1),waypoints(waypoint_no,2)
	PointEntity sphere,waypoint_pivot; Point the sphere (player) at the current waypoint location
	MoveEntity sphere,0,0,waypoints(waypoint_no,3); move the sphere (player) towards the current way-point
												  ; using the current way-points speed
	If EntityDistance#(sphere,waypoint_pivot)<2 Then ; if the sphere (player) comes pretty close to the waypoint
													 ; within 2 units, then...
		waypoint_number=waypoint_number+1 ; move to the next way-point
		If waypoint_number > no_of_waypoints Then ; if the waypoint is past the last way-point then...
			waypoint_number=0 ; loop it back to the very first waypoint
		End If
	End If
End Function



Alberto(Posted 2004) [#6]
cyberheater

I like the RossC's idea myself.
however It is a kind of sport game , my character should quicly change direction and the straight segments of the path are rather short (you can imagine a runner of american football, for example )
That's way I am concerned that RossC's methods takes too many waypoints, also it may be not reliable to keep track of waypoints, being the distance beetwen two adjacent waypoints rather small
I had in mind a pattern filled with op codes
something like : run,50,bend,45°, etc
I will try both methods.
Thanks in advance for any further comments \ suggestions


Alberto(Posted 2004) [#7]
RossC
thanks a lot for your help
I will try to edit your code to fit my needs


turtle1776(Posted 2004) [#8]
This may also be worth a look:

A* Pathfinding for Beginners
http://www.policyalmanac.org/games/aStarTutorial.htm


Alberto(Posted 2004) [#9]
turtle1776

thanks, very interesting