![]() |
findPath()
1 Attachment(s)
I never planned on sharing this with anyone when I made it a while back, but since you asked....
I strongly suggest you read this tutorial which helped me understand A* pathfinding. I must have read it at least 5 times myself while coding this. There's also several suggestions on how you can help speed up your pathfinding. findPath(cx, cy, tx, ty, cutCorners, fullPath); -- returns an array of nodes (tiles) from one tile to another
Source: Attachment 49606 It's not commented very well...I added what I could quickly before posting. It actually wasn't commented at all until today, so if you have any questions just ask. For example, PHP Code:
Important note: This function assumes that the object you are finding a path for is only 1 tile big (16x16 pixels). However, it shouldn't be hard to modify the onwall checks for bigger objects such as a player. I might add another parameter for the size of the object in the future, but I don't have the time right now. Tips for improving speed:
|
You so pro !
|
I was waiting for you to click on an area that was inaccessible, to see it explode.
|
Quote:
Here's a little level sweepage, 3015 valid locations. As you can see, the area inside the table was not highlighted because it is not valid. |
Quote:
|
wow nice
|
1 Attachment(s)
I edited your script so that use takes an offset x/y and width/height for wall checking.
The function parameters now goes as this: findPath(cx, cy, offSet, blockSize, tx, ty, cutCorners, fullPath) Where offSet and blockSize are two-member arrays. It's not perfect, but it does work pretty well. |
I would much prefer if a path is not found the characters head exploded.
|
Oh, I also forgot to mention that I changed the h-score equation to a revised manhattan equation that takes diagonal movement into account. The equation favors diagonal movement.
|
Quote:
On another random note, I found a* algorithms start to go real bad once you're above the 64*64 tile range.... implement a method to work with a node based system? =D (i'm way too lazy) |
Quote:
Quote:
Diagonal movement "costs" 14 and horizontal/vertical was 10 (1,1,sqrt(2) triangle)...and with A* that would make moving to a diaganol node less favorable because it's considered to be a longer distance. However, if taking a diaganol path will get you to the final location quicker, it still will to a certain extent. Keep in mind this searches tile-by tile, and doesn't take into account that a straight line to the target might be the quickest approach (like if there's no walls)...might be something else you could add. |
Quote:
|
Quote:
|
Bumping this thread a bit...
I've tried several times to have a larger wall checking area than 1x1 and have absolutely no luck. I'm wondering if you've had any luck doing this, Salesman? |
Quote:
I'm supposed to be cramming for an exam right now...serious ADD...but I'll try to maybe show an example of larger nodes tomorrow or this weekend. |
I think I was just going about the problem wrong before, I wasn't adjusting the grid, instead I was adjusting the onwall detection, when I should be doing both.
Honestly I'm not sure, I haven't used this in awhile, but I seem to remember my edit that allowed variable size nodes not working properly. |
4 Attachment(s)
Quote:
Edit: Here ya go. It seems to work, but I kind of rushed through it so let me know if you run into any problems. Attachment 50640 Added two more parameters.
findPath(cx, cy, tx, ty, nodeSize, moveByNode, cutCorners, fullPath) Example of usage: PHP Code:
Attachment 50642 It also allows you to search for nodes smaller than 1 tile, but I can't really see a reason why you'd ever want to do that: Attachment 50641 Ideally, you probably want to set moveByNode to false. Otherwise you might not find a path that actually exists. Here's what I mean: Attachment 50643 In this picture, the gray lines represent tiles. The black lines represent what your grid will look like with moveByNode set to true. The green squares are starting and finishing nodes. The red is a wall. The pink is a spot that should be a perfectly fine spot for a 2x2 object to sit, but would not be considered fine if moveByNode is true. With moveByNode set to true, the path from one green to the other is impossible because it searches by node (black squares). When false, the path is found easily. An example of when you might use moveByNode is for a turn-based system where NPCs can only move a certain number of nodes and/or maps are designed based on the node/object-size. |
sorry for double posting, but I forgot editing the last post doesn't bump the thread and I want coreys to know that I updated the script. :blush:
|
;)
Thanks. This will come in handy. However, moving this script to serverside I've noticed it takes quite a bit of CPU usage (at least serverside, it does). Also, you may want to add some sort of offset option, for cases like a normal player character type NPC where the top of the head doesn't need to be counted as a blocking area. Additionally, this paper might be interesting to you! It's basically on how to efficiently find paths with variable size entities; it also has a lot on dealing with multiple types of terrain where entities have certain types of terrain they can clear but that's probably not that important (unless you want it to be!). |
All times are GMT +2. The time now is 09:41 AM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.