Generate Polygons and Colliders Runtime in 2D Game

Basically I was trying to develop Scale game so my initial research was running about this.

Here is the short video about game so you can understand whole game concept:

Now in full game play, step by step polygons get generated as you place cutter shape on polygon object.

So what do you think, the polygon object actually a 2d sprite object or its 3d model object?
As per its look and clarity, its kind of 2d sprite I am thinking.

Through Google, I found these options for polygon generation:
http://wiki.unity3d.com/index.php/Triangulator

https://github.com/UnityPatterns/PolyMesh

And for generating polygon collider, I found this option:

Now please share your side opinion or thinking for generating polygons at runtime.
If you have other simple way exist then please point me that so I can start programming work for Scale kind of game.

Both options should be easy to implement. I would do some benchmarks and take the one that is more performant.

Edit: After looking closer at the principle of the game I would say performance is not so important, so I would go with meshes, as they will be easier to create those polygons with.

1 Like

So as per your saying, shall I require to use both Poly Mesh and Polygon Collider for this game?
Also easy to implement in game.
https://github.com/UnityPatterns/PolyMesh
https://www.assetstore.unity3d.com/en/#!/content/52265

I don’t know these assets, but I would say that this tutorial and a bunch of 2D box colliders is everything you need to build this game. Just add one 2D box collider with every cut and remove those that are not needed anymore.

EDIT: Or you can use something like this, maybe with edge collider, as I am not sure if polygon collider can be used inside out. It is really easy, once you will know the shape.

1 Like

Now I have created Polygon through given points so I move ahead one step.
Second step for me is to create collider around shape and also inside out too. So within shop region ball can move physically.

So what should I have to try for this ??
Whether I have to try with Edge collider or convex mesh collider??

At present I moved ahead so much and I can able to now generate polygon mesh and collider at run time.

I was working on Scale game:

For this purpose, I require to sort points as per clock wise direction and I implemented algorithm for this process. But now this kind of situation occurred for me. I made proper sketch to represent my side error situation.

Above figure I hope explaining my problem exactly.
Basically using algorithm, points get sorted correctly but as per game requirements, I want some tweak in those.

I have used this algorithm for polygon points sorting:
https://stackoverflow.com/questions/6989100/sort-points-in-clockwise-order

So what to do in this case? Please give me some suggestion regarding this.

The algorithm you are using for point sorting is probably useless for concave polygons. I would use some some library like Clipper to calculate difference of two polygons.

How clipping algorithm become useful to me in this case? Basically clipping algorithm do clipping and offsetting of polygons.
I am not clear regarding usage of this algorithm over here so can you please explain me little bit more??

You have two polygons. The one that is displayed on the screen, polygon A, and the other that is implied, your “cutter”, polygon B. Now you just need to perform boolean A not B to get a new cut out polygon and that is what Clipper is for.

I think sir some misconception was running. Basically at present I was getting points sorted in above red polygon way.
I want these points to be like green polygon way.

Rather than sorting if I use Clipper algorithm then polygon A’s points in properly sorted as like above grey polygon.
But my polygon B’s points was creating complex polygon because they were not sorted.

Now what happen if I apply boolean A not B over this! May be some other complex shape get created. I want polygon should be green like.

Grey Color is our polygon A.
For polygon B - I have list of points not sorted. So polygon may be a star.
Target - I want to sort polygon B points so it should become like green polygon not red polygon.

I hope now all matter clear to you.

Your algorithm for sorting will not help you, it is for convex polygons and your polygon is concave. It is like trying to solve connect the dots puzzle without the numbers. Maybe you might make some sorting algorithm based on constraint, that sides of your polygon are perpendicular, but there could be edge case bugs that only your customers will find.

On the other side, using library like Clipper is as easy as

A/ Get your old polygon
B/ Place cutter rectangle over it and perform boolean not
C/ Enjoy your new polygon

1 Like

Hi I want to clipper in unity. But its dealing with integer numbers only. What about floating point? i need float values in clipper to deal with. Is there a way?

It is because of floating point (im)precision. You can use it in Unity just fine, you just have to convert it. Something like

private const long ClipperScale = 10000;

private static Vector2 ConvertToVector2(IntPoint value)
{
    var x = (float) value.X / ClipperScale;
    var y = (float) value.Y / ClipperScale;
    return new Vector2(x, y);
}

private static IntPoint ConvertToIntPoint(Vector2 value)
{
    var x = (long) ((double) value.x * ClipperScale);
    var y = (long) ((double) value.y * ClipperScale);
    return new IntPoint(x, y);
}

Pretty Good. Thank you.