Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / 3D Pathfinding snippet

Author
Message
JohnStabler
AGK Bronze Backer
11
Years of Service
User Offline
Joined: 16th Aug 2013
Location: Cardiff, Wales, UK
Posted: 4th Aug 2016 21:55 Edited at: 4th Aug 2016 21:57
Some people may find this 3D pathfinding code useful.

It uses waypoints, which can be manually specified or calculated based on level geometry, to create paths around a 3D world for AI to follow.

How it works

Load your level, add waypoint data and then tell it to calculate. It uses some maths and temporary objects & raycasting to work out which waypoints are visible to each other. Once the precalculations are done you can call the GetRoute(startWP, endWP) function at any time in your game loop to generate a path between two way points.

Iterate backwards over the route array to get from the start to the finish. Here's an example of the code in use (assuming a big cube is placed at [0, 0, 0]:





More complex levels just require more well place waypoints:



In my current project I generate levels in code using primitives, so it is really easy to simultaneously generate the required waypoints. I use the "level" property so waypoints on higher/lowe levels cannot be "seen" by waypoints on other, unaccessible, levels. Waypoints can only see other waypoints on the same level of one level above or below.

Performance is good, even with a couple of hundred waypoints. I dare say there could be further optimizations.

Pathfinding code:

Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 4th Aug 2016 22:03
Not bad, I created a similar system a while ago that I've always wanted to go back and finish.

Check out my youtube videos of it in action.
JohnStabler
AGK Bronze Backer
11
Years of Service
User Offline
Joined: 16th Aug 2013
Location: Cardiff, Wales, UK
Posted: 4th Aug 2016 22:09
I've currently got a level with around 1500 waypoints. Precalculation takes a couple of seconds, which is fine with a loading screen; but a call to the GetRoute() function takes around 25ms. I'm looking to improve on that.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Aug 2016 23:21
Not to hijack but to expand on the post a little, awhile back I converted IanM's A* library to AGK.
http://www.zimnox.com/resources/agk/libraries/

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
JohnStabler
AGK Bronze Backer
11
Years of Service
User Offline
Joined: 16th Aug 2013
Location: Cardiff, Wales, UK
Posted: 8th Aug 2016 10:40
@Phaelax

While that library is handy, it seems it is limited to tile-based levels. The approach I've used is better suited to 3D geometry and non-uniform path nodes. It's closer to the navigation mesh method than tiles.

Login to post a reply

Server time is: 2024-11-25 04:31:40
Your offset time is: 2024-11-25 04:31:40