hierarchical pathfinding implementation - artificial-intelligence

I want to divide my map in clusters and implement HPA*. Where do I start, each time I try this I run into problems. I need to implement this on a random and dynamically changing map.
I am unsure how to write an algorithm that places these "nodes", to connect the parts, in between sections/clusters of the map and update them. I guess every time open tiles lie in between closed tiles on the edge of a cluster/section there should be a node since inside the cluster it could be that multiple openings into the cluster do not connect to each other within this section.
Normally I would just have a big Tile[,] map. I guess I could just leave this be and create a cluster/section class that holds all the paths and nodes. And have a node class/struct that holds the 2 tiles that are connected between sections. I have read several articles about HPA* but I just can not wrap my head around implementing this correctly on a random and dynamical map. I hope to get some good pointers here although the question is not very clear.
-edit-
What i am trying to do is making cluster class that holds 10x10 tiles/nodes with on each side an entry point (several if there is a obstruction on the edge). The entries link to the next cluster.

Related

Swift 3 - Function to create n number of sprites with random x/y coordinates

I am trying to create multiple SKSpriteNodes that each have their own independent variables that I can change/modify. I would like to be able to run a function when the app starts, for example "createSprites(5)" which would create 5 sprites with the image/texture "shape.png" at random x and y coordinates and add all 5 Sprites to an array that I can access and edit different Sprite's positioning based on the index value. I would then like to be able to have another function "addSprite()" which, each time it is called, create a new Sprite with the same "shape.png" texture, place it at another random X and Y coordinate and also add it to the array of all Sprites to, again, be able to access later and change coordinates etc.
I have been looking through so many other Stack Overflow pages and can not seem to find a solution. My ideal solution would simply be the two functions I stated earlier. One to create an "n" number of Sprites and another function to create and add one more sprite to the array each time it is called.
Hope that makes sense, I'm fairly new to Swift and all this Sprite stuff, so simple informative answers would be very much appreciated.
You're not going to find an ideal solution from the past because nobody has likely had exactly the same desire with both Swift and SpriteKit. Having said that, there's likely partial answers you can blend together, and get the result you want or, at least, an understanding of how to do it.
Sprite Positioning in SK is probably the first thing to read up on:
https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Sprites/Sprites.html
having gotten that figured out, you can move to random positions.
Random positioning of Sprites:
Duplicate Sprite in Random Positions with SpriteKit
Sprite Kit random positions
Both use earlier versions of randomisation that aren't as powerful as what's available now, in GameplayKit. So... Generating random numbers in Swift with GameplayKit:
https://www.hackingwithswift.com/read/35/overview
It's hard to overstate the importance of understanding the various possibilities of game design implications of varying types of randomisation, so probably wise to read this, from Apple:
https://developer.apple.com/library/content/documentation/General/Conceptual/GameplayKit_Guide/RandomSources.html
After that, it's a case of needing to determine what constitutes a time or event at which to create more sprites at more random positions, and how fussy you want to be about proximity to other sprites, and overlaps.

Tracking moving Objects using classes

This is a Theoretical question about the use of classes, something which I haven't yet had too much experience. I just want some opinions about whether this would be the best way to go about programming for my task.
The goal of my project is to track objects that move across the frame of the camera, I have been able to detect the objects and store their centre point for each frame (note that this point is only stored until the next frame is read and new points are stored).
I am thinking of using a class store the previous positions of the objects as well as an ID of some kind so that I can differentiate the objects from each other.
Would this be over complicating things? or would it be better to simply store the history points into a basic array of points?
For some more insight, I will be tracking people from an overhead counter, so realistically there shouldn't be more than 15 objects present in the frame at one time. The images below show the detected heads and their centre points (these are the objects being tracked).

How can I count the number of valid squares in each group?

EDIT:I have a logical error and I don't know how to fix it.This is my code. I think that the problem is in the while function at line 245. It doesn't add the next valid pixel to the queue, so the queue becomes 0 and it exits the WHILE function.
I need help from a veteran here! I have something like a chess table, with equal sized squares, numbered from bottom to top, right to left, but only some of them are valid for me (as shown in the picture I posted a link to). I deleted the non-valid ones from the table.
I want my C program to count the squares in each group. As you can see in the image, a valid group only has directly connected squares, and squares that connect only diagonally are not in the same group. I used colors to evidentiate the valid groups in my picture.
I know the table's width and height and I know how many squares and valid squares it has.
I stored their numbers in a vector, but I can't figure out how to count the squares in each group.
How can I do this?
Here is my picture:
I want to find out a method which works for larger "chess tables", like pictures with known sizes.
What you surely miss is : What group a valid square belongs to ?
You could use graph theory to solve that.
But you can also try some other approach.
For instance, you can use one tag list which will keep track of already visited nodes or not. You can use a vector of vector for managing group nodes.
Browse the table with same direction let's say from top left to bottom right. Check only non visited nodes.
When a valid square is found which is non visited add this node to vector[count++] and flag this node to visited.
Look for this node connected squares in bottom right direction. If one is found then flag it to visited and add the node in the same vector[count] list.
You repeat the same process until you cannot find anymore connected components (with recursion for instance).
If no connected squares are found anymore in the same group continue step 1.
At the end just sum of each of your vector[count] and it should give the expected results (for performance you could do that on the fly while looking connected components).
You should categorize each connected squares into a single component and count the size of each component. For that you can use a nested data structure such as vector to store the connected squares and nest it inside a map to distinguish the other connected squares. like,
map<int squareIndex, vector<squares> >
The basic idea is to have a visited array so that you can ignore the already visited squares. The algorithm follows just like doing Breadth First Search in Graphs. You can use a Queue to store the adjacent connected components of the current square. I would suggest to do the following.
Algo
traverse through the array from 1 to N
If queue is empty then make new index in map and push element into your map
If visited[element] is true then go to step 1, else visited[element]=true and push element into your map.
element = dequeue()
enqueue adjacent squares of the current element and repeat from step 2 for the current element
In this way finally your map will be of size 4 (for the given case) and each map element will contain a vector of the connected squares. Use size property of each vector to count the number of squares in each index.
You are searching for something called connected component.
This post sheds some light on the subject but there's a number of different implementations available on the Web.

Entering / Exiting a NavGraph - Pathfinding

I've got a manually created NavGraph in a 3D environment. I understand (and have implemented previously) an A* routine to find my way through the graph once you've 'got on the graph'.
What I'm interested in, is the most optimal way to get onto and 'off' the Graph.
Ex:
So the routine go's something like this:
Shoot a ray from the source to the destination, if theres nothing in the way, go ahead and just walk it.
if theres something in the way, we need to use the graph, so to get onto the graph, we need to find the closest visible node on the graph. (to do this, I previously sorted the graph based on the distance from the source, then fired rays from closet to furthest till i found one that didn't have an obstacle. )
Then run the standard A*...
Then 'exit' the graph, through the same method as we got on the graph (used to calculate the endpoint for the above A*) so I take and fire rays from the endpoint to the closest navgraph node.
so by the time this is all said and done, unless my navgraph is very dense, I've spent more time getting on/off the graph than I have calculating the path...
There has to be a better/faster way? (is there some kind of spacial subdivision trick?)
You could build a Quadtree of all the nodes, to quickly find the closest node from a given position.
It is very common to have a spatial subdivision of the world. Something like a quadtree or octree is common in 3D worlds, although you could overlay a grid too, or track arbitrary regions, etc. Basically it's a simple data-structures problem of giving yourself some sort of access to N navgraph nodes without needing an O(N) search to find where you are, and your choices tend to come down to some sort of tree or some sort of hash table.

Tile based game theory

I'm looking for articles on tile based games, like the old ultima 6&7, or even puzzle pirates. Specifically:
How they keep track of objects on the map. Objects such as other characters, or trees, or things the character can move.
AI behind the characters. How the game handles character behavior for
characters on the map that are off screen. Especially with very large maps and numerous characters.
I remember checking out Amit's Game Development page back when I wrote some games. He has a great sub-section on tiles that has most of what you want.
You could look through back issues of Game Developer magazine to see if something addresses what you're asking in detail.
For (1) the easiest way of dealing with a tile-based map where each tile can contain multiple objects is to just have a big multidimensional array of structs representing each tile. The struct contains a pointer to the head of a linked list representing all the objects in that tile. This is very memory efficient and lets you quickly find everything in a certain tile while also enumerating them along some other axis (eg, owner, allocation arena, etc).
RogueBasin is devoted to Rogue-like games (e.g. Rogue, NetHack, ). All of those games were based on a simple square grid. The site has an extensive section on developing games like that: http://roguebasin.roguelikedevelopment.org/index.php?title=Articles
You will find both suggestions and code there which could be used to build a game like you describe. After all, the only real difference between Rogue/Larn/NetHack/etc. and Diablo or the Ultima series is using simple text characters to depict the map and gameplay vs. isometric sprites.
In particular you will find information about calculating the area illuminated by a torch or lantern the user is carrying, data structures for storing maps, algorithms for automatic generation of maps, and lots of notes for how different games which have already been written chose to address these problems.
Check out Gamasutra. They have loads of articles for all kinds of game development.
The map would be an array of values. It could be divided into discrete parts. Only parts in range of the player would be loaded and the objects & npc in these parts active.
Since the old hardware had very limited memory and cpu, those games would only be able to load and process parts of the maps.

Resources