I know my question seems pretty vague, but I can't think of a better way to put it, so I'll start off by explaining what I'm trying to do.
I'm currently working on a project whereby I've been given a map and I'm coding a 'Critter' that should be able to navigate its way around the map; the critter has various other functions, but those are not relevant to the current question. The whole program and solution is being written in C#.
I can control the speed of the critter, and retrieve its current location on the map by returning its current X and Y position, I can also set its direction when it collides with the terrain that blocks it.
The only problem I have is that I can't think of a way to intelligently navigate my way around the map; so far I've been basing it on what direction the critter is facing when it collides with the terrain, and this is in no way a good way of moving around the map!
I'm not a games programmer, and this is for a software assignment, so I have no clue on AI techniques.
Here's a link to an image of what the maps and critters look like:
Map and Critter image
I'm in no way looking for anyone to give me a full solution, just a push in the general direction on map navigation.
If the only knowledge of the environment that you have is the position of your critter and its velocity the best you can do is a wall following algorithm I think. If you can detect some of the other things in your environment you have many more options.
Some of the more popular algorithm types are...
A* Search (The classic)
Visibility Graphs
Voronoi Diagrams (similar to the above)
Potential Fields
Potential Fields is a fancy way of saying every obstacle or wall has a "repulsive force" while every goal has an "attractive force". The strength of the force is based on the distance from the object and the "severity" of the object. (A pit of lava is much more severe to travel through than a bumpy road) After constructing the force fields the naive algorithm boils down to following the path of least resistance. Better versions can detect local minima and maxima and escape those wells.
Critter
-----\ /-------\
\ / \
\/ \
Local Minima Trap \
\
\
Goal
A* Search
Take a look at the A* pathfinding algorithm. It's essentially the standard approach for stuff like this.
Amit Patel's write up on pathfinding for games has a pretty good introduction to A* as well as popular variants of the algorithm.
You'll find a C# implementation here, and here
Dynamic A*
Let's say the terrain you'll be searching is not known ahead of time, but rather is discovered as the agent explores its environment. If your agent comes across a previously unknown obstacle, you could just update the agent's map of the terrain and then re-run A* to find a new path to the goal that routes around the obstruction.
While a workable solution, rerunning the planning algorithm from scratch every time you find a new obstacle results in a sizable amount of redundant computation. For example, once you're around the obstacle, it might be that the most efficient route to the goal follows the one you were planning on taking before you discovered the obstacle. By just rerunning A*, you'll need to recompute this section of the previous path.
You can avoid this by using Dynamic A* (D*). Since it keeps track of previously computed paths, when the agent finds a new obstacle, the system only needs to compute new routes in the area around the obstacle. After that, it can just reuse existing paths.
I would use a goal-oriented approach. Your question states the goal is than explore the map and avoid obstacles, so that's what we make our goal. But how do we explore the whole map? We explore what is unexplored.
From the outset you have only one unexplored area, the square you are on. The rest of the map is marked as unexplored. You choose an unexplored location, and make it your goal to explore it. But how do you get there? You create a subgoal to explore the location next to it. And how do you do that - explore the square next to that, and so on, until your original goal is broken down into a sequence of explorations, starting from your current square and navigating to the target square.
As you hit obstacles and discover features of the map, some of the subgoals may need to be changed. E.g. when you hit a wall, the subgoal to explore that square has to be scrubbed and you create a new plan to find an alternate route. This is known as backtracking.
That's basically it for the high level description. I hope it helps!
I seem to be late at the party. If your critter have a GPS and the full map at hand, the right thing to do is definitely A*, and if the map is small enough a simple BFS would do as well if you don't feel like coding up A* (A* has quite a few corner cases you want to handle right).
However a different question is what if your critter only knows the direction of the goal and can only observe locally what is around it? What if your critter does not know the full map?
In this case you would want to implement the "bug algorithm" for navigation. Link: http://www.cs.cmu.edu/~./motionplanning/lecture/Chap2-Bug-Alg_howie.pdf
It's a cute piece of algorithm that works for all unknown maps, you would have a blast coding it I'm sure.
Related
I have a 3D voxel game and I'm trying to find best fitting pathfinding algorithm to use. I've been wondering if the A* algorithm is capable of handling multiple levels, for example a multi-story building and find routes through staircases or ladders.
Is this possible with A* or should I use something else?
Thanks in advance.
Any path-finding algorithms(uniform or non-uniform) can be used as long as you use them right. You have to decide first on what kind of path-finding you want to use depending on what you want to achieve.
Uniform or graph algorithms can have a lot of overhead for a multi-story building due to excessive nodes. (assuming that you are building the whole building all at once)
Non-uniforms are way slower than graph algorithms but doesn't have much of a overhead.
It really depends on what you want your characters to do, and how you will optimize it.
A* is fast, but you might want to check out some iterations of A* like jump point system.
Try using the NavMesh component based workflow first. If there is a nice staircase behaviour that can be efficiently and automatically used, NavMesh will find it.
Otherwise, you will need to use something called hierarchical search. Read an example here.
I have a program for transportation company where optimal route computed by Dijkstra . Cities as vertexes and routes as edges . To find weight of edge . I connected cities in map with line and measure it . Then I accept it as weight of edge . But in real life routes aren't straight . So How can I fix it ? enter image description here
In my project I must solve logistic problem with creating Software . Can anyone give me idea what to solve ?
As you already found out, problem is not so simple as it might seem.
First of all, connecting only major cities is a bad idea, as they're probably not connected directly with a highways (if it's not U.S. or something).
That's your current idea:
What I would propose is to try to get every minor city on every way that makes sense and add it as a vertex to your Dijkstra:
Now, we come to the point where we can see which way actually exists in real world. From just looking on our graph, we might presume that going with bottom path should be more efficient. But what if we found out this:
We can easily come to conclusion now that upper path is actually much better, because you can achieve twice the speed of the bottom one.
Is that very precise classification? No, it's not.
We might want to think of what traffic is on each way and change weights of edges dynamically. But that's probably too much for your basic implementation.
What I would do in the end is think of what data can I gather almost alone or with little help.
So I can definitely:
somehow scrap some data about actual ways of reaching point B from point A; good references are either Google Maps API or Bing Maps API;
gather minor cities along the way with finding real world ways from point A to B;
try finding out what speed limit is where (if there is any database for it)
Actually, you might want to go for full into either Google Maps or Bing Maps and just let them provide you with best road possible.
They both have quite actual data of any road you need.
There is no way you can gather as much data as they do.
You have everything on a plate, if you feel that's the way you can do.
If not, I would go hybrid way - get some vital data from any maps API, then use it for my Dijkstra algorithm, then use this data to write a simple algorithm for measuring actual weight of each edge based on possible modifiers (speed limit, traffic if API provides it and so on).
TL;DR
Given a 2-dimensional plane (say, 800x600 pixels or even 4000x4000) that can contain obstacles (static objects) or entities (like tanks, or vehicles), how can computer-controlled tanks navigate the map without colliding with static objects while pursuing other tanks? Please note that every static object or entity has the ability to freely rotate at 360 degrees and has an arbitrary size.
Context
What I am really trying to do is to develop a game with tanks. It initially started as a modern alternative to an old arcade game called "Battle City". At first, it might have been easy to develop an AI, considering the 13x13 grid, fixed sizes and no rotation. But now, with free rotation and arbitrary sizes, I am unable to find a way of replicating such behavior in these circumstances.
The computer-controlled tanks must be able to navigate a map full of obstacles and pursue the player. I think that the main problem is generating all the possibilities for a tank to go to; the collision system is already implemented and awaiting to be used. For example, tanks might be able to fit through tight spaces (which can be diagonal, for instance) just by adjusting its angle of rotation.
Although I have quite some experience in programming, this is way beyond my reach. Even though I would prefer a broader answer regarding the implementationn of tank's artificial intelligence, a method for generating the said paths might suffice.
I initially though about using graphs, but I do not know how to apply them considering different tanks have different sizes and the rotation thing gives me a headache. Then again, if I would be using graphs, what will a node represent? A pixel? 16,000,000 nodes would be quite a large number.
What I am using
C# as the main programming language;
MonoGame (XNA Framework alternative) for rendering;
RotatedRectangle class (http://www.xnadevelopment.com/tutorials/rotatedrectanglecollisions/rotatedrectanglecollisions.shtml).
I am looking forward for your guidelines. Thank you for your time!
I've been working on a project of crowd simulation that included path finding and obstacles/other people avoidance.
We've used the Recast Navigation, a all-in-one library which implements state-of-the-art navigation mesh algorithms.
You can get more info here : https://github.com/memononen/recastnavigation
In our project, it has proven to be reliable and very configurable. Even if it's written in C++, you can easily find/make a wrapper (in our case, we've been using it wrapped in Javascript on a Nodejs server!)
If you don't want to use this library, you can still take a look at Navigation Meshes, which is the underlying theory behind Recast.
I hope it will help!
Navigation Mesh, that's what ur looking for. To explain a bit, it's in theory really easy. U build ur World (2D/3D) and after creation u generate a new mesh, that tells entities where they are allowed to move, without colliding with the surroundings. They then move on this mesh. Next is the path generation algorithm which is basically nothing else then checking in any mathematically form how to get on this mesh to it's target. On an actual navigation mesh, this get's rather complicated but easy if u think of a grid where u check which fields to move to get the shortest way.
So short answered, u need any type of additional layer of ur world, that tells the AI where it is allowed to move, and any kind of algorithm that fits ur type of layer to calculate the path.
As a hint, for unity as an example, there are many free good build solutions. Also u will find a bunch of good libraries to achieve this without a game engine like unity.
I have a set of points, the coordinates are not predetermined and I can set them when I create them, but their links are predetermined. A point can have one or more links, but not zero.
I want to be able to generate a visual representation of these points at positions where these link lines between them will not intersect. From what I've learned in researching so far, I believe this will be somewhat similar to a planar graph, however there will be points with only one link, and I'm not sure planar graphs are able to represent these.
I'm not sure if there is a good way of doing what I am trying to do or not, but I'll admit that maths is not my strong suite. My 'best' idea so far is to somehow detect these intersections and then move points in a direction that somehow takes the intersection location into account to reposition them so that that particular intersection does not occur....and to loop and do this for every point until no more intersections are detected. However there could well be some sort of more efficient mathematical algorithm that I could use instead that I am simply unaware of.
I'm interested in all advice here, whether it is efficient or not.
This is not an easy problem. Here are the Google search results for “algorithm to draw a planar graph”. The Boost C++ Library has some support for drawing planar embeddings including an example. These of course use C++, not the C# you tagged the problem with.
I'm wondering what way would be best to render a 2D map for a shooter (these will be static maps) similar to Soldat. Multiple options I've considered are a tile based map (stored in txt files), or just creating different classes for the different terrains I plan to use and creating a data structure to read/store them in a file. (I want to also be able to include things like jumping/running on walls, sliding down walls/slopes ect)
I feel like there must be a better way than either of these, but haven't been able to find definitive information :/
Thanks :)
Soldat used a polygon based format to render and represent it's levels. The editor would have allowed the user to plot points and connections between them to make walls and structures when were then given textures and properties (such as collide = death). Items and images where then overlayed over this.
The soldat rendering engine would then use these structures to perform collision detection and to build up the polygon (triangles that are drawn) representation of the world.
I was going to go with the "just use a tile map," but then I looked at Soldat and it's a bit more complex.
I don't know what they're using, but I suspect they're using polygons and probably colliding against polygons as well. The levels are likely generated with the same sort of primitives you'd see in 3D games, but all in a plane. E.g. texture mapped triangles and collision against the edges of the surfaces rather than against the surface itself.
In my experience, "terrain" is usually an object in itself and the collision and visual representations are data inside that object (aggregated into that object, typically). The different pieces of terrain don't necessarily need to be different types of thing, indeed in all of the games I've made the entire world was generally a single object of type "Terrain" or "World" or something and it managed the doings of the various visible pieces coming into and out of view.
This doesn't feel like a great answer, but I thought I'd try to give you something.
Examples of platform games doing what you want are Braid and Aquaria. You can get Aquaria for 2 pennies litteraly, with the Humble Indie Bundle.
Here's the Aquaria's editor in action. I could not find a decent video of the Braid editor.
I found this other video about IndieLib, and a level editor that they have created which is similar to Aquaria's (Disclaimer: I haven't tried it myself).
Appart from that, googling for "Braid and Aquaria platform engine" should give you more results.
I would recommend reading the book Building XNA 2.0 Games: A Practical Guide for Independent Game Development. It walks you through the development of a 2D side-scoller game named Zombie Smashers (based on the author's award-winning The Dishwasher: Dead Samurai).
Chapter 4: The Map Editor is what you are looking for.
It will help you even if you don't plan on developing in XNA.