how to choose which waypoint next?

Blitz3D Forums/Blitz3D Beginners Area/how to choose which waypoint next?

pimpom(Posted 2007) [#1]
Hello, I am using a waypoint system for my opponents in a race, and i-m varying their speed and distance to next waypoint randomly. Thing is, I woul like to do something like:

waypoints system a, from 1 to 104
waypoints system b, from 1 to 104
waypoints system c, from 1 to 104

My waypoint structure is like:
numer of waypoint, position in x, in y, in z
PointNew.wayPoint( 1, 1.014, 2, -3)

And from those, make opponent pick waypoint 2, from a, waypoint 3 from c and waypoint 4 from b, all of this randomly.

Which method would you recommend to achieve this? Does this technique has a name? If someone knows please let me know. Thanks.


puki(Posted 2007) [#2]
What you are doing is fine. The way you are storing them is fine (x, y, z) - I'd store the lane they are in too - this will make it easy when making them overtake.

Start simple (an oval track is ideal) - make the cars favour the inside line this forces the idea that they only need to change lanes to overtake.

So, if you are driving a car on the track on the inside lane (driving slowly on purpose) and a CPU AI car comes up behind you - allow the AI to check your speed compared to theirs - ie assist the AI - you can also use EntityDistance too.

Once EntityDistance is so close, and the car in front is going slow enough to overtake, let your AI car pick a forthcoming waypoint in Lane 2 - make sure the waypoints are not too close together to allow it to swing out before getting to the waypoint. The next waypoint can be the same waypoint number as the current lane, but you just make sure it picks lane 2. Remember to make them move back to the inside - again using EntityDistance, etc.

You can also use overtaking waypoints - in real-life racing, some circuits have good overtaking spots on them so feel free to give the waypoints an 'overtaking value' - you can rate these say 0 to 5 - 0 being void (ie a default waypoint) and then 5 being the highest valued one where an AI driver will try to overtake when they hit it - ie they have just reached the waypoint (and if there is something in front and the EntityDistance is low enough) then let them go for the overtake.

Start simple and build the AI up - there isn't much need to make them overtake randomally unless you are using a track that is not oval-like and widens and narrows - personally, I'd recommend starting simple.


pimpom(Posted 2007) [#3]
Hey Puki, thank you very much, these ideas are indeed very helpful. I will change structure of waypoint to be:

numer of waypoint, lane, position in x, in y, in z, overtake value

PointNew.wayPoint( 1, 1, 2.014, 2, -3, 5); = lane 1
PointNew.wayPoint( 1, 2, -2.014, 2, -3, 5); = lane 2

I'm using at the moment:

 
Function drivePath(p.cars)
	For this.waypoint = Each wayPoint
		If this\number = p\nextpoint Then

;choose which lane based on distance to player  + overtake value, from 1 to 3
if entitydistance(p\geo, player) < 10 then
changelane...; stuck in here :S
end if

			Point_Entity(p\geo,EntityX(this\geom),EntityY(this\geom),EntityZ(this\geom))
			Local sp#=Rnd (1.7,2.3 )
			MoveEntity p\geo,0,0,sp
			closeRange= Rand(3,7)
			If EntityDistance(p\geo,this\geom) < closeRange Then
				p\nextpoint = p\nextpoint+1
				If p\nextpoint > MAX_POINTS Then p\nextpoint = 1; MAX_POINTS = total number of waypoints
			End If
		End If
	Next
End Function



But... how do you actually change lanes? As you can see, still trying to learn.

Thanks.


jfk EO-11110(Posted 2007) [#4]
change lanes maybe be easer/faster if the waypoints have one more dimension, be it a type within a type that defines the lane, or an Array that allows to use the current lane as one of the indicies.
If you keep it like this, where lane is a simple field in the type, you'd have to do a search for the right lane each time.


pimpom(Posted 2007) [#5]
Hey Jfk, thank you :)

i have:



ButI am getting 3 repeating prints in the second loop, what am i doing wrong? and, is there another way to acess more like:
p.lane = 1
if c.child=2.....


Thanks for looking into this.


Stevie G(Posted 2007) [#6]
THat's because you're seaching through all children 3 times.


Use an array of types .. much quicker to access as no need for loops to find the one you need ... e.g.

Type Waypoint
	Field x#, y#, z#
End Type

Const Lanes = 3
Const Waypoints = 5
Dim WP.waypoint( Lanes-1, Waypoints-1 )

; fill with data
For i = 0 To Lanes - 1
	For j = 0 To Waypoints - 1
		WP( i, j ) = New waypoint
		WP( i, j )\x =Rnd(1,7)
		WP( i, j )\y = 0
		WP( i, j )\z = i
	Next
Next

;print current data
For i = 0 To Lanes - 1
	Print "Lane " + (i+1)
	For j = 0 To Waypoints-1
		w.waypoint = WP( i, j )
		Print "waypoint number: "+(j+1)+" x="+WP(i,j)\x + " y="+ WP(i,j)\y + " z="+WP(i,j)\z
	Next
Print""
Next

;print position 2 in lane 1
Print "****************"
Print "x="+WP( 0, 1)\x + " y="+ WP( 0, 1 )\y + " z="+WP( 0, 1 )\z


;Flip
WaitKey()

End



jfk EO-11110(Posted 2007) [#7]
Uh, Array of types I meant, not type within a type, Stevie is absolutely right. I'm such an Oldschool coder, I still prefere simple arrays and banks.


pimpom(Posted 2007) [#8]
ahhhh very nice

After a small detour to hopefully get around arrays working in my head, I will continue my little game. Thank you very much.


jfk EO-11110(Posted 2007) [#9]
Stevies example is indeed a very good one. Now you only have to add a field "current_lane" to your car objects. Simply alter this parameter to jump to an other lane.