Detecting Adjacent Provinces using Collision
Hello. First time posting so apologies if I'm in the wrong place. So I have a world map with a lot of provinces. Each province is an individual Gameobject with Polygon Collider 2D and Rigidbody 2D (static). Have set all the colliders so they overlap with neighboring provinces like so:
Am storing data in each province Gameobject. Currently am trying to store a Gameobject list of all adjacent provinces in each province (adjacency determined by collision). Code works for storing all other information so far, but the collisions between the provinces aren't being detected. This might be because the provinces are instantiated already overlapping/colliding.
Anyone have any idea how I can get this to work?
Answer by streeetwalker · Sep 06, 2020 at 07:50 PM
@matteosspam, yes, in my experience that is correct. If items are instantiated with overlapping colliders, OnCollisonEnter does not fire. However, OnCollisionStay should.
But why not just create a public array or list of adjacent provinces and author references to them in the inspector of each province? Or, probably even better yet, put references to all your provinces into a array, and reference the indices of each adjacent province in another array that each respective province holds.
It seems you could avoid the hassle using one of those methods, or something similar.
Hi, thanks for your response. Could you give me an example of how OnCollisionStay2D could be used like this? I've been trying to get it to work and haven't been able to figure out how to use it to add to a Gameobject's list.
As for your suggestion, I might be misunderstanding what you're recommending, but there are a couple hundred provinces, so I was looking for a way to $$anonymous$$imize the amount of work I'd have to do by hand, hence the CSVReader and such. Just placing them all by hand was already a pain. At this point though I might just have to suck it up and do it.
@matteosspam, sorry I didn't catch that you were reading province data from a CSV file. I'm not familiar with the code for CSVReader - is this the one from this source? https://bravenewmethod.com/2014/09/13/lightweight-csv-reader-for-unity/
Then potentially your work is easier, because you can author the adjacency lists into the CSV file, and then populate each province's adjacency list from that. I think in the long run this is going to be easier than trying to do what you are doing with colliders.
Are you creating the CSV in some texteditor or Excel or Word and each line in the CSV represents a province?
If so, let each line number represent the index of the province. For example, the data for province 0 is the 1st line in the CSV and so on (or if you have a header line with the dictionary keys as strings, then the first province is the 2nd line). Because, you are reading the data for each province into a list of dictionaries, you can access that data using privinceData[index].
Therefore, you need one field in each line of the CSV to encode an array of the indexes adjacent provinces. You can encode that as a string using some delimiter that is not going to be parsed by CSVReader. You will have to parse it yourself when you want to populate the array that represents the adjacent provinces for any given province.
Or you could modify CSV reader to create the array. Either way, you can also store that in the dictionary, because it looks like your dictionary stores its data as generic objects...
All of this is not difficult to do, but to explain it gets complex because there are several parts to it. Before I try to go further, can you provide a sample of one province's data that you have in your CSV, along with the field names/ids that become the dictionary keys for a given province's data?
Yes, I am using that exact CSVReader. It's currently reading in the values from the CSV and converting them into int, bool, or strings, depending on what they are.
Here's a few lines from the CSV.
Here's an example of what a province's data looks like in the inspector.
@matteosspam - the sample CSV lines I wrote didn't format properly, so I edited it to put a line break after my sample header row.
Your answer
Follow this Question
Related Questions
A box collider 2D (Is Trigger marked) stops my player from moving which has rigidbody 2D 1 Answer
Adding rotate-to-mouse code causes issues with Relative Joint 1 Answer
how avoided this among my player and the wall? 0 Answers
All colliders not longer working 0 Answers
2D Colliders don't actually touch, causing problems 0 Answers