Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

How to find the area of an irregular shape in 2D?

Discussion in '2D' started by EveningWolf, Nov 10, 2017.

  1. EveningWolf

    EveningWolf

    Joined:
    Apr 14, 2017
    Posts:
    9
    I am making a fruit ninja clone and want to add some extra functionality. I have an asset that will slice a 2d sprite object with a collider into child objects with mesh renderers and accurate polygon colliders in real time. I was wondering how could I calculate the area of these children and compare them when they are created? I want to be able to ask the player to cut an object into three pieces and then find out if those three pieces are relatively the same size and the if they are award the player more points. Any help for this or better ideas of how to achieve this same goal would be greatly appreciated. Thanks.
     
  2. EveningWolf

    EveningWolf

    Joined:
    Apr 14, 2017
    Posts:
    9
    Anyone have any ideas? Is this not possible?
     
  3. ELeigh

    ELeigh

    Joined:
    Dec 26, 2014
    Posts:
    2
  4. EveningWolf

    EveningWolf

    Joined:
    Apr 14, 2017
    Posts:
    9
    Thanks. I've tried that one before without it working. I used the https://docs.unity3d.com/ScriptReference/Mesh-vertices.html and fed those into a list then tried to use that list in the equation in your link. The major issue I believe is that equation is getting the surface of a 3d polygon and I'm working with 2D objects.
     
  5. EveningWolf

    EveningWolf

    Joined:
    Apr 14, 2017
    Posts:
    9
    I'm also pretty new to Unity and my math knowledge is lacking so I could be doing it wrong.
     
  6. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    2,030
    2D objects are still 3D objects. They have a mesh and you can calculate the surface area in the same way.
     
    Hyblademin likes this.
  7. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    647
    This is a pretty well-known formula, and it will work for you, we just need to troubleshoot.

    The author of the code snippet for finding the area specialized their function for flat polygons in the x-z plane. I'm assuming you're using the x-y plane, and you need to change all the transform.position.z parts to transform.position.y parts. If this is correct and you haven't already done this, try it and tell us if that works.

    If not, could you elaborate on what it is or isn't doing?