Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Generate Polygons and Colliders Runtime in 2D Game

Discussion in 'Scripting' started by siddharth3322, Nov 18, 2017.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    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://answers.unity.com/questions/464446/how-to-draw-2d-polygon.html
    https://github.com/UnityPatterns/PolyMesh

    And for generating polygon collider, I found this option:
    https://www.assetstore.unity3d.com/en/#!/content/52265

    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.
     
  2. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    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.
     
    Last edited: Nov 18, 2017
    siddharth3322 likes this.
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    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
     
  4. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    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.
     
    Last edited: Nov 19, 2017
    siddharth3322 likes this.
  5. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    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??
     
  6. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    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:
    https://play.google.com/store/apps/details?id=com.gmail.bulupe.Scale

    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.

    Polygon Problem.png

    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.
     
  7. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    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.
     
  8. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    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??
     
  9. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    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.
     
  10. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    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.
     
  11. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    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

    bitmap.png
     
    siddharth3322 likes this.
  12. sara_unityads

    sara_unityads

    Joined:
    Feb 7, 2015
    Posts:
    26

    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?
     
  13. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    It is because of floating point (im)precision. You can use it in Unity just fine, you just have to convert it. Something like
    Code (CSharp):
    1. private const long ClipperScale = 10000;
    2.  
    3. private static Vector2 ConvertToVector2(IntPoint value)
    4. {
    5.     var x = (float) value.X / ClipperScale;
    6.     var y = (float) value.Y / ClipperScale;
    7.     return new Vector2(x, y);
    8. }
    9.  
    10. private static IntPoint ConvertToIntPoint(Vector2 value)
    11. {
    12.     var x = (long) ((double) value.x * ClipperScale);
    13.     var y = (long) ((double) value.y * ClipperScale);
    14.     return new IntPoint(x, y);
    15. }
     
  14. sara_unityads

    sara_unityads

    Joined:
    Feb 7, 2015
    Posts:
    26
    Pretty Good. Thank you.