Sign in to follow this  
Wolfsbane

WIP: Panda Pathfinding

Recommended Posts

Hiya guys and girls,

Overview

One of the more useful features to have in a game engine is path finding. Think games from Pac-man to Age of Empires to Tower Defenses. have some kind of rudimentary path finding. And these days with game tablets and phones, players want to simply tap some place on the screen, and watch their player scurry over there.

So I thought I'd take a shot at creating a template/example on how you'd implement it. I'm not finding much time to work on it, so I thought I'd post what I had as a WIP:

API

So far: We have

Quote

PathGrid

 init: function(left, top, hcells, vcells, cellwidth, cellheight, diagonals, debug) 

Create a grid at position left/top, with hcells number of cells horizontally, and vcells number vertically. Set diagonals to true if you want to be able to move across grids diagonally, and set debug to true to see the grid in debug mode.

addCollisionGroup: function(world, groupNum) 

Add a CollisionGroup to the grid. When this is called, the grid will check to see if there are any Panda Physics.Bodies that are inside our path grid. If they are, all the grid cells that overlap with the Body will be marked as blocked.

 search: function(start, end) 

This is an A* algorithm based on the code here. The parameters are a starting Cell square and an ending cell square, and it will return a list of Cell objects to form the path.

heuristic: function(pos0, pos1) 

This one is interesting. This helps determine how accurate your path is. Check this link for further reading. 

For now, you can play with these two values:


this.D = 0.1;
this.G = 10;

I.e. if you set them both to 1, you'll see(from debug mode) that it'll find a path quickest, but the path is not the most optimal. 

Example screenshots: Debug mode and no debug mode.

image.png.bf69e6f22428f5960763f39761832bfe.pngimage.png.44c1cf04b73f71392689dbfa09e6eb8a.png 

 

So: All you have to do is

  1. Create a grid over a playing area.
  2. Add a collision group of objects to be avoided. and
  3. return the path. Done!

Links and resources

I'm basing the design on the old GM mp_grid movement API (Docs). It's a really simple, straightforward pathing implementation, that's pretty flexible for most games.

The A* Algorithm is based on this page by Brian Grinstead. Code is just tweaked a bit to suit Panda. Code isn't optimized, and probably well get changed as per progress.

I think EasyStar.js is probably the de facto way to do pathing in html5 games. The smart dev would just take it and adapt it to Panda. (But I never claimed to be smart, and where's the fun in just using someone else's code)

Attachment

pathfinding_02.zip

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.