HOME | DD
Published: 2013-12-04 20:47:18 +0000 UTC; Views: 4052; Favourites: 21; Downloads: 164
Redirect to original
Description
~Game Path Solver~**This is not a game - this demonstrates the A* method used in games to find the best path to reach a location in a map.
Have you ever wondered how game characters find their way to locations? If they just went in a straight line, they would eventually get stuck on walls. Well, actually in many games they do just that! LOL
But in computer science, nearly everything has already been solved and there is a best solution available. When it comes to finding paths through obstacles, the solution is a method called A* (A-Star). Not only A* always finds a path, but assuming a valid one exists, it will also find the shortest one.
In this simulation, the character is the red circle, and you can click to tell it to go anywhere in the white area. Black blocks are walls. The character will use A* to find the shortest possible way, and also show you how many steps it took to get there. If the area cannot be reached, it will not go.
Every time you run this it will create a new random map, but you can SHIFT-CLICK to edit it anyway you like.
You may be asking: if A* is the best path solver, then why do my game characters sometimes end up stuck on walls? The answer to that is performance. Real time 3D games use so much computer resources just to display the 3D world that it may not be able to afford the additional processing power and memory required by A* for every single character walking in the game.
They will then use "cheaper" path finding solutions for the sake of performance, that are not as smart as A*, but will give faster (yet inaccurate) results using less processor and memory. That's how we end up with stupid game characters getting stuck running against walls! You can try all you want here - for as long as there is a viable way, it finds the best path instantly and will *NEVER* get stuck on walls. Some of the best RTS games use A* to move the troops in the game maps. ^_______^
A* finds the shortest path as fast as you can click the map, and it's flexible enough to allow you to change your mind in the middle of a path. It will then find a new path starting from where you are at the moment.
I am creating a new tank game and will use A* to drive the enemy tanks, so if they can see you, they will chase your quite efficiently. I have also used A* to drive the artificial intelligence of the monsters in my "Monster Maze" game (below), where there are as many as 50 monsters using A* at the same time in the highest level.
This program was written in ActionScript 3.0 using freeware FlashDevelp. Hope you like it and thanks for coming by!
Related content
Comments: 111
ken1171 In reply to ??? [2015-08-18 23:52:56 +0000 UTC]
It's fun trying to block the way and see how it tries different paths. ^^
👍: 0 ⏩: 1
ken1171 In reply to MagicMaster390 [2015-08-19 00:13:04 +0000 UTC]
I have used this method in my Monster Maze game. ^^
👍: 0 ⏩: 1
Graenfur [2013-12-18 11:31:02 +0000 UTC]
Hey, about your tank collision problem: I'm not any kind of expert here and I don't know how exactly things are done in your game, but you could "record" each tanks movement in an array or something.. So when tank runs into problem like being stuck (which of course would need to be defined what is and isn't stuck) then it travels back in the recorded path until it is unstuck again..
[or the problem is exactly the detecting weather it is or isn't stuck?]
Anyway.. I dunno.. just a suggestion.
👍: 0 ⏩: 1
ken1171 In reply to Graenfur [2013-12-18 18:18:19 +0000 UTC]
Hey, that idea might actually work! The way I am doing it now is to only record the last known position before a collision has happened, and then return the tank there. But 1-step recovery is not being enough. If instead I keep an array of previous positions before a collision, there will be a better chance of recovering from getting stuck on the walls. I can keep only, say, the last 5 positions, and then try them in reverse order until there is no more collision. I will see if that works! ^_____^
👍: 0 ⏩: 1
Graenfur In reply to ken1171 [2013-12-20 18:35:25 +0000 UTC]
I remember working with box2d and there was this 'bullet' property which could be assigned to fast objects so they would checked for collisions more often than others. While my idea is not the same, it made me figure that more detailed info about latest positions may help here.
Let me know how it works out.
Also looking forward to that game. [:
👍: 0 ⏩: 1
ken1171 In reply to Graenfur [2013-12-20 20:00:16 +0000 UTC]
I actually solved 95% of the wall-sticking collisions with a rather simple method - just undo the last action before the collision. Simple is always better! Designing levels for the game now. ^^
👍: 0 ⏩: 1
Graenfur In reply to ken1171 [2013-12-21 11:30:17 +0000 UTC]
Hmm. Isn't going backwards through recorded positions the same undoing?
👍: 0 ⏩: 1
ken1171 In reply to Graenfur [2013-12-21 20:18:13 +0000 UTC]
Not really, because I only need to remember the last action before testing for collision, and if the vehicle hits something, all I have to do is to revert that action. I still have rare cases of getting stuck on the walls, but I am happy enough it's rare now.
The tanks now have better graphics, increased rate of fire, and yesterday I have completed 5 levels for the game. Just have to decide what happens when we win.
👍: 0 ⏩: 0
Ratchet27 [2013-12-09 01:16:55 +0000 UTC]
Epic man!
Good ol' pathfinding. I really wish I had the time to work on programming. I feel like I could be really good at game making if I just has an extra hour each day... lol
👍: 0 ⏩: 1
ken1171 In reply to Ratchet27 [2013-12-09 05:26:27 +0000 UTC]
Thank you! I am still trying to find a way to avoid getting my tank stuck on the walls because of poor collision detection. I try to pull it back to where it was right before collision, but that doesn't seem to be enough... >___<
👍: 0 ⏩: 1
Ratchet27 In reply to ken1171 [2013-12-09 20:49:55 +0000 UTC]
Oh! I've ran into that one before!
It was a while back, and I don't think I solved it...
👍: 0 ⏩: 1
ken1171 In reply to Ratchet27 [2013-12-09 22:14:56 +0000 UTC]
This is a pretty common thing, so I am sure there is a solution somewhere. The problem is that my tank not only moves, but it also turns around like a real tank would move. So collision can happen even when it is just turning in place.
👍: 0 ⏩: 1
Ratchet27 In reply to ken1171 [2013-12-10 19:54:21 +0000 UTC]
Hmmm... That does seem a bit tricky. That would mean that a potential bounding box would have to rotate with the tank as well. Not only that, rotary and linear velocities must be handled by the collision detecting. I've seen this solved before via "bouncing" the tank, but that means that it literally throws the tank from the wall... I'm assuming that you are wanting nonelastic collisions...
👍: 0 ⏩: 1
ken1171 In reply to Ratchet27 [2013-12-10 20:14:44 +0000 UTC]
I have already done all that, and the tank already "bounces" off the walls after collisions, but there are certain collision angles where this causes the tank to glue to the wall instead of bouncing off. I have to look into this, but 1st, I want to finish the game. Creating the 2nd level map now. Each level has flags to capture, and after getting them all, starts appear on the map. Capturing the stars win the level.
Also have to implement player lives now. As it is now, getting killed restarts the current level indefinitely. I think I will give the players 3 lives, and maybe they can get more when winning levels.
👍: 0 ⏩: 1
Ratchet27 In reply to ken1171 [2013-12-13 00:48:25 +0000 UTC]
Ah. I see. I had a similar problem in a python pong game. If the "ball" caught the edge of the paddle, it bounced back and forth inside of the paddle. Still haven't fixed it because I built the collision methods from scratch.
That makes sense. As opposed to fixing every problem immediately and never finishing... Not that I've EVER done that... lol
👍: 0 ⏩: 1
ken1171 In reply to Ratchet27 [2013-12-13 03:58:05 +0000 UTC]
Darn, this new product I have sent to the store came back from beta testing with issues I need to fix. I guess the game will have to wait a little longer.
👍: 0 ⏩: 1
Ratchet27 In reply to ken1171 [2013-12-18 02:42:34 +0000 UTC]
Hmm... That sucks. What product is it?
👍: 0 ⏩: 1
ken1171 In reply to Ratchet27 [2013-12-18 05:22:17 +0000 UTC]
Today I have finished rebuilding the whole thing from scratch, and from this I have learned valuable new skills that will be profitable from now on. ^^ It's about this product:
👍: 0 ⏩: 1
Ratchet27 In reply to ken1171 [2013-12-19 17:23:02 +0000 UTC]
If I might ask, what exactly is the product? The clothing rendering, the clothing itself, the picture???
👍: 0 ⏩: 1
ken1171 In reply to Ratchet27 [2013-12-20 03:11:02 +0000 UTC]
The clothing. It's a 7 pieces set with dress, stockings, boots, belt, earrings and 2 bracelets for both Poser and DS4 platforms. The expansion pack includes 78 materials also made for both platforms.
👍: 0 ⏩: 1
Ratchet27 In reply to ken1171 [2013-12-20 18:42:20 +0000 UTC]
Ah. Makes sense now. Pretty awesome!
👍: 0 ⏩: 1
ken1171 In reply to Ratchet27 [2013-12-20 19:58:40 +0000 UTC]
Thanks! I have solved 95% of the wall collision issues with the tank game, and now it happens "kinda rarely".
👍: 0 ⏩: 1
Ratchet27 In reply to ken1171 [2013-12-23 21:49:23 +0000 UTC]
Oh! Awesome! Kinda rarely works for Bethesda.
👍: 0 ⏩: 1
ken1171 In reply to Ratchet27 [2013-12-24 02:23:52 +0000 UTC]
The problem is not the collision detection, but instead the way tanks move, by rotating to change direction. Tonight I am making the 3 music tracks for the game, which by now already has a few voice acting and sound effects. Five levels have been created, and I still don't know what kind of intro and winning screens I will make.
👍: 0 ⏩: 1
Ratchet27 In reply to ken1171 [2013-12-26 02:08:52 +0000 UTC]
Ooh! Music = awesome! Sounds about beta app worthy.
👍: 0 ⏩: 1
ken1171 In reply to Ratchet27 [2013-12-26 03:20:04 +0000 UTC]
The game has been posted today. ^^
👍: 0 ⏩: 0
ken1171 In reply to Phyl-CGI [2013-12-07 20:33:58 +0000 UTC]
Thank you! I am already integrating this into my tank game. It will drive the AI for simultaneous multiple enemies in the map. ^^
👍: 0 ⏩: 1
ken1171 In reply to Phyl-CGI [2013-12-08 02:30:54 +0000 UTC]
I am currently being massacred when playing catch the flag against 2 tanks.
👍: 0 ⏩: 1
ken1171 In reply to Schieben [2013-12-05 18:35:56 +0000 UTC]
Thanks! I am now integrating this into my tank game to drive the enemy units. ^^
👍: 0 ⏩: 0
ken1171 In reply to melficexd [2013-12-04 23:40:10 +0000 UTC]
Thanks! My new tank game will use A* on the enemy tank AI. ^___^
👍: 0 ⏩: 0
ken1171 In reply to kaleidobot [2013-12-04 23:39:40 +0000 UTC]
Probably is on this 30x30 grid!
Did you edit the map to make it THAT hard to find a path?
👍: 0 ⏩: 1
kaleidobot In reply to ken1171 [2013-12-04 23:52:58 +0000 UTC]
Yeah a nifty zig-zag pattern.
I think it should be possible to score even higher, but I am not a mathematician
👍: 0 ⏩: 1
ken1171 In reply to kaleidobot [2013-12-05 00:01:39 +0000 UTC]
I can think of a number of games that can be build over this grid - either to help it reach the other side, or to keep it from doing it. For example, "Pipe Dream" can easily be adapted into a grid to make the water find a pipe path to the other side. That would be fairly simple! Every time the player rotates a pipe segment, I can adjust the grid map to present the water-passable ways. Now add a timer and you have a game!
👍: 0 ⏩: 1
kaleidobot In reply to ken1171 [2013-12-05 12:00:41 +0000 UTC]
I agree, it's bath time for one of your characters
👍: 0 ⏩: 1
ken1171 In reply to kaleidobot [2013-12-05 18:44:30 +0000 UTC]
Hmmm.... more ideas are cooking now...
👍: 0 ⏩: 0
GoodKittyNyanchan [2013-12-04 22:45:18 +0000 UTC]
This was quite a fascinyating lesson-nya.
👍: 0 ⏩: 1
ken1171 In reply to GoodKittyNyanchan [2013-12-04 23:51:48 +0000 UTC]
Thank you! I was always amused by how my troops moved in C&C Red Alert. I just told them to move to a certain place in the map, and no matter how cluttered the map was, they always got there. Now it doesn't look like magic anymore. LOL
👍: 0 ⏩: 1
nathaniel2k In reply to ken1171 [2013-12-11 15:48:26 +0000 UTC]
magic doesnt stop being magic just cause you know how its done
👍: 0 ⏩: 1
ken1171 In reply to nathaniel2k [2013-12-11 20:00:01 +0000 UTC]
I have put like 2, 4 and then 6 AI tanks in a level (A* driven), and it's interesting how they appear to work as a group when chasing me. When separated by a wall, one goes around from the top, and another decides to go down, leaving me no way to escape without confrontation with at least one of them. In reality the path they take is purely based on which one is the shortest, they are not really trying to surround me - but sometimes it appears that way. ^^
I have added AI behavior to give up chasing if I get far enough from them, where the distance is configurable. In that case they just go back to random patrols across the map. Conversely, if I get close enough to a tank, they will "see" me and start chasing again. I guess we can compare this to getting into their "visual range".
When a tank is hit, it is not destroyed. Instead, it stops moving and gets into automatic repair mode, where a red bar appears on top of it. Until repaired, it cannot move or shoot. I think this sounds more realistic than the traditional kill-disappear-respawn. As a side effect, if a tank is damaged on top of a flag you want to capture, you will have to wait until the tank starts moving again, which adds to the strategy part. ^^
👍: 0 ⏩: 1
nathaniel2k In reply to ken1171 [2013-12-11 20:45:29 +0000 UTC]
naturally you dont want to shut them down when theyre in youre way either
👍: 0 ⏩: 1
| Next =>























