How do you make an entity follow a path?

Blitz3D Forums/Blitz3D Beginners Area/How do you make an entity follow a path?

nerdy_kid(Posted 2008) [#1]
I want to make a tower defence game, but I don't know how to make the enemys follow a path. Can someone help me out?


Pax(Posted 2008) [#2]
hi jessebugs, I think you need to look out for waypoints. There are some good goodies in the code archives.

Basic idea is recording positions (x,y,z if using 3D), orienting enemy to waypoint 1 and making him go into that direction, when it comes near to waypoint one, change its orientation to waypoint 2 and walk forwards to it, and so on. Good luck!


Gabriel(Posted 2008) [#3]
Forgive me, I don't think I've ever played a Tower Defence game, although I have a vague idea what they are. Straight path or curved path?


Ross C(Posted 2008) [#4]
I believed it used straight paths, but laid out to give the appearance of curving.


IPete2(Posted 2008) [#5]
Hey there,
There's a really lazy way and a more elegant way.

First the elegant way:

Use TYPEs to create a load of pivots (invisible entities in B3d) which you then place to make the main WAYPOINTs in your game.

Then get your characters to point at the next waypoint in their sequence, until the character is within a short distance of the actual waypoint itself, at which point, increment the waypoint index so your character should head for the next one in the sequence. Once the character reaches the end, well either do it all in reverse or make the last one and the first one near each other, and reset the index again.

Now the lazy way:
Make a physical entity in the shape of your path, but make it a hollow corridor, kep it tight to the size of your entity who will patrol inside it. Place your patrolling entity inside this corridor and set the collisions to slide.
Ensure no other entity in the game collides with the corridor. Set your patrolling entity off and at certain co-ords, rotate him to face the new direction you need.

(This last idea is soooo bad - please ignore it! lol - I can't believe I even recalled it!!!)

Good luck.

IPete2.


jhocking(Posted 2008) [#6]
I wouldn't call that the lazy way. That sounds considerably harder than using waypoints, both to set up in the first place and then to edit later if/when you tweak the path of the character.


jfk EO-11110(Posted 2008) [#7]
Waypoints include some nasty problems. I would suggest not to interpolate the distances between the waypoints, but simply use the right speed for the characters. You may use some vector drawing algorithms to smooth the way. Basicly you need a straight point to point system as a basement anyway. Here's some guide on how to achieve it.

Let us assume you have a number of waypoints stored in an array, you've got x,y and z positions of each waypoint stored in the array. Furthermore each character has a waypoint track counter variable that defines what waypoint the character should cross next.

Simply point the character towards the next waypoint (using the counter as the index of the array). Then move the character one step towards this next waypoint. Now you have to check if the character PASSED BY this next waypoint. You can do this with relative ease:

Store the characters position before the step, then perform the step and then compare the new position to the old position. Now, if one of the following conditions is true then the character used to pass by the waypoint:

old x is lower than waypoint x and new x is higher than waypoint x.
or
old x is higher than waypoint x and new x is lower than waypoint x.
The same goes for Z.

If your world isn't entirely flat, but includes hills or stairs then you may also do the same with the Y axis.


So now you have determined if the charcter used to PASS BY. If so, all you have to do is incrementing the counter, so the character will point and walk towards the next waypoint next time. (of course make sure to check the array boundaries).


Saving waypoints is very easy. Personally I prefere to walk around in a map in first person persepctive and frequently save the camera xyz position to a list, there it is, the waypoint list.


nerdy_kid(Posted 2008) [#8]
Thanks, I think I'll try jfk EO-11110's idea with the first person shooter walking in the map to store the waypoints.
I thought there were two ways of doing it and the hollow coridor must have been the other. Thanks again!