- Home /
How to fix a specific colliders count problem
Hello guys,
This question is related to a previous one of mine, that I thought I've found a solution to but that led to a 'bug' of some sort.
Here is the fully detailed problem.
The above image describes my ideal situation. An AI is captured between 4 walls and 4 dots, therefore a 'captured' boolean is triggered.
Unfortunately, I've found another situation:
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.
What is your structure model ?
Does a dot know at what walls it is attached ? Does a wall know what dots it links ? You need to check if you have a closed area, but to do that you need an efficient structure model.
@$$anonymous$$iraSensei, I would love to check if there is a closed area but I do not know how. I asked a question about this a month or so ago and did not find an answer that fit the needs of my game...any tips?
In fact, that is how I started making the game. With that exact idea in $$anonymous$$d. Didn't think that it would be hard to check for an enclosed area around a certain point :)
So you draw walls between points, but a wall is a line in the end right? In that case check if both ends of the wall are inside your box collider?
"The game is supposed to be 2D, but as Unity is using 3D space, I made the best of it"
Just one thing, Eug. Just so you know, basically every 2D computer game in the world now is made with Unity3D. it is the go-to tool for 2D games. (Some are also made in Corona, and a few natively.) Now that even Angry Birds, for goodness sake, uses Unity3D it;'s pretty much the standard.
it is completely normal to make 2D games in 3D engines, it's how it's done. As you say you use an ortho camera (usually) and proceed from there.
"The AI has a box collider ... [and it also has] has two scripts attached to it. One that counts the number of walls and one that counts the number of dots."
How do you do that?
Answer by Fattie · Aug 26, 2013 at 12:20 PM
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.
PS for general discussion on this sort of thing check out books such as http://www.amazon.com/Mathematics-Programming-Computer-Graphics-Edition/dp/1435458869
or http://www.amazon.com/Real-Time-Collision-Detection-Interactive-Technology/dp/1558607323
there are only 3 or 4 "famous" books like that, enjoy em all!
Great answer as always @Fattie. I will definitely check out those books! Thank you a lot!
I am sure I don't fully understand everything you explained here but I get the idea and think I'll be able to implement it in a day or two to check it :) Will post a comment here to let you know when I get it working :D
I really hope it helps. It's just a "basic" thing you do when working with mesh or whatever; again i don't REALLY understand your SPECIFIC situation (are the posts at fixed points -- whatever -- I just don't know) but the above will be of interest maybe anyways.
there are a number of enclosure-like algorithms, you'll find 'em all.
I find in the really world it's a matter of your specific situation - there is always a simpler way in a given SPECIFIC situation. Anyways over to you...
BTW a crappy way to check for enclosure is just send out, oh, 36 casts to the edge of the board (like one every ten degrees) and if one of them makes it to the egde, you are not surrounded. it's just that simple :) that often works in most situations!! :O
Answer by TropicalTrevor · Aug 26, 2013 at 12:29 PM
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?
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Trigger Enter and Exit not working properly? 2 Answers
Generating A Custom Collider For Multiple Meshes - Script 2 Answers
How to check if car entered garage... then if true, do a specified function ? -1 Answers