So far, i got the algorithm to generate the mesh as intended. But i’m having trouble with the next step : collisions.
As far as i can tell, if i use a mesh collider, it is considered 3D, so anything that will need to collide with it would need a 3D collider too (ie. the player sprite). After trying out a bit, i know that 2D and 3D colliders, coupled with rigidbodies do not collide, it’s either 2 2D objects or 2 3D object that can collide.
I added functions to create a mesh collider based on the resulting mesh. And the collisions work with 3D colliders, but not with 2D colliders.
The problem i’m having is that the game i’m making is supposed to be 2D. Many parts, such as fluids, would be much easier if i could use 2D physics.
Is there a way to achieve this or do i have to think about working in 3D from now on? Or maybe is there a way to mix both?
That would not work as the generated mesh has an irregular shape. This might be a full block, or a block with empty spaces, or any other shape that tile based world can have.
Hello, i just finished trying out PolygonCollider2D.
It’s either too slow, or too limited, or too inefficient. i used the same worst-case map i was testing other colliders on and, 30-40 minutes after game launch, it was still calculating, or instantiating. Because Unity was stuck at showing the Play button pressed but nothing happening.
In the image in the attached files you can see what my worst-case scenario map looks like :
The chunk is a 64x64 tiles map, but there is a need for collision detection for each non-empty tile.
That makes it 2048 tiles total.
My SpriteTile has the option to use polygon colliders and can easily have thousands of tiles with arbitrary shapes created in a fraction of a second, so I assume your code has major optimization problems.
Well, as i said, it’s not individual tiles that i make, but one big Mesh for a map of 64x64 tiles. From what you said i understood that you are making individual tiles.
Also, here is what my collision generator looks like for the polygon collider :
PolygonCollider2D polcol = null;
private void GenCollision()
{
if (polcol == null)
{
polcol = gameObject.GetComponent<PolygonCollider2D>();
if (polcol == null)
{
polcol = gameObject.AddComponent<PolygonCollider2D>();
}
}
polcol.pathCount = colSquareCount;
colSquareCount = 0;
for (int i = 0; i < _chunk.Area.Height; i++)
{
for (int j = 0; j < _chunk.Area.Width; j++)
{
ITile tile = _chunk[j, i];
if (tile != null)
{
Vector2[] points = new Vector2[4];
points[0] = new Vector2(j, i);
points[1] = new Vector2(j + 1, i);
points[2] = new Vector2(j + 1, i + 1);
points[3] = new Vector2(j, i + 1);
polcol.SetPath(colSquareCount, points);
colSquareCount++;
}
}
}
}
I’m not sure what i might be doing wrong as the code is based on the Mesh collider generation algorithm.
No, not individual tiles. Blocks of combined tiles, where the block size is definable. It looks like above a certain number the generation gets disproportionately slow, so I’d suggest 16 chunks of 16x16 tiles rather than a single 64x64 chunk. (Which would be a lot better for the physics engine anyway, as far as speed goes.) Actually 64 chunks of 8x8 would be better still.
I see, i intended to separate the map chunks from the unity chunks (data / display). But haven’t got around to it yet since i was trying to find out how to make collisions work first.
Another point i didn’t mention but might be of consequence is that the game area that would need to be displayed is quite big : around 200x200 tiles. If i use the block size you suggested, that would make me generate around 169 blocks of 16x16 tiles. Would that have any hit on performance?
so i’ve been trying to apply what you told me about creating the PolygonCollider2D. I reduced the size of the collider to 16 tiles, removed the mesh generation, left only the collider generation, checked that the points in the paths are correct, and i still can’t have Unity not freeze up when launched.
It’s obvious i’m doing something wrong. However, after searching trough the net, i couldn’t find any useful information on how to properly use/generate PolygonCollider2D, and Unity’s documentation being what it is, i’m losing hope i could make use of the 2D physics in the game.
Would you be so kind to help me here by either pointing me to a tutorial, or a page that explains the proper way to use the PolygonCollider2D programmatically, or tell me what i did wrong in the function i posted previously?
I tried your code with an array of bools (since I don’t know what your ITile is), and while doing one chunk took an excessive amount of time, breaking it up into 64 chunks of 8x8 took less than half a second. But otherwise I just copied your code, so that part by itself is OK.
So 16x16 is too big too…will try smaller chunks, but then i have to try the 200x200 chunk made of 8x8 chunks…hope that works too.
Edit : found the problem : when modifying the code to make smaller chunks, used wrong if statement for looping over tiles, so it always went for the 64x64 size instead of the smaller one i set as public so it’s editable from editor. works like a charm now. Still have to do some test to see if it works well on a big size map.
Hello.
I finally had some time to test out configurations and even with 8x8, for a map of 200x200 it takes 40 seconds to only generate the polygons for collisions. For something marketed as a game engine, i would think there would be something to ease the generation of 2D colliders, but it seems there isn’t. In any case, i will begin looking into integrating 3D physics into the 2D world. Your answer is technically correct but it’s unsatisfying for my need. In any case thanks for your help.