The above image represents another way that the dots are connected which also triggers the 'captured' boolean.
The yellow-orange area represents my AI's capture area (represented by a box collider in-game).
What I do to trigger the ‘captured’ boolean is count the number of walls and dots and if the number of walls is >= 4 and the number of dots is also >= 4 then it is captured.
I do understand where the problem is coming from. But I am not sure how I’d be able to fix it. Any advice here would be greatly appreciated.
P.S.: Sorry for the long question.
EDIT:@Fattie, sorry if I wasn’t clear about the problem. More info below.
Neither the dots nor the walls know to whom they are attached as they are created by tap + drag action.
The yellow border represents the AI’s capture area (a box collider).
The problem is that it should only trigger the ‘captured’ boolean when the first image scenario happens, as the second one does not count as a closed area, therefore my AI is not captured.
EDIT2: Here is all the info about the game.
The game is supposed to be 2D, but as Unity is using 3D space, I made the best of it. Ortographic camera and placing the objects at precise positions.
The game starts with an AI wandering around the map.
The purpose of the game is to capture the AI in an enclosed area composed of 4 walls.
Upon tapping once on the background, a dot (or pole) is instantiated from a prefab.
Upon tapping on that dot, 4 slots open (for the four directions). While tapping, if dragging in one direction, a wall will be created and at the end of the wall, another dot will be placed with only the remaining 3 slots available.
The AI has a box collider (shown in the second picture as the yellow border) which has 2 scripts attached to it. One that counts the number of walls and one that counts the number of dots. Also the script has a fail safe to check that there are no duplicates amongs these.
When both scripts’ ints are equal or higher than 4, I consider the AI captured.
I know that my solution is in fact a hackish solution, but it was the best at the time when I couldn’t find any other.
You have to get in to doing paths to check for enclosure.
So from your “AI character” you do this - there are various strategies but here’s one …
shoot north. if there is no wall, you’re free
say there is a wall. (We’ll call it WallAA.) you must follow that wall counterclockwise to it’s end-point.
{NB. to determine which end is CCW, get the two angles from AICharacter and decide.}
at that junction point, you will need a way to find all the other walls connected to that junction point
for each of those other walls-connected-to-the-junction-point, find the clockwise angle from the present line segment. the minimal is the next path following around the paddock. that’s the core of the intelligence.
keep doing this, checking you don’t repeat etc etc
i’m sure you can guess the end - if you come back to the start point (the “other” end in (2)) and you turned 360 degrees LEFT in total, there is a closed loop around your AICharacter
It’s astounding that computers do ALL THIS every time you use the simplest “enclosed” algorithm in photoshop, a game or anything else. that’s how you do it.
as you can see this gets any weird topology, etc
this DOES NOT allow for doubly-enclosed islands etc -but you can work out all that detail. it’s impossible to be more specific unless we knew much more about your game.
It all bottles down to checking more than just how many walls does a dot have, you need to check the connectivity of a wall.
There’s three structural scenarios I can think of:
1)
You’re telling a dot what other dots it’s linked to. So a dot has an array of dots which are the connections or walls.
If this is the case:
For every dot, check how many of the other dots are in it’s list of connections. Each of the 4 dots should have two other dots in it’s connections, assuming it can never connect to itself. If this condition holds up captured == true.
2)
If you have a wall that knows which dots it’s connected to, but the dots don’t know about the walls, you need to check for each of your walls to have both it’s dots occur in your 4 source dots.
A wall is then a struct or class with a Dot start and Dot end (which are both unique).
3)
If a dot knows what walls it has, I’m assuming the walls of a dot are just a list of ints or something, you need to match dots’ walls to see if they overlap. If two dots share a wall ID they are connected. Then if each of your dots shares 2 walls you’ll end up with 8 positive results, which is what you’d requier in this scenario to make sure all dots are connected both ways.
To make this work: when creating a wall, make sure both points involved know this wall. A wall can then even be a completely empty object, or as I described above, a unique int ID.
Does that help you? Or is your code setup completely different?