Search code examples
c#unity-game-engineconvex-hullrigid-bodies

Unity3D - Order list by angle to point


I am trying to make a 2D game in unity. I want to create a convex hull around a group of rigidbody2Ds that I have in a List. To do this, I need to order the List by the angle that each rigidbody2D makes with the start position. Anyone know how I could do this?


Solution

  • You can use LINQ to sort a list by a computed value.

            return list.OrderBy(c =>
            {
                return /*ANGLE COMPUTATION*/;
            }).ToList();
    

    and that angle computation could be the dot product between the (normalized) vectors. https://docs.unity3d.com/ScriptReference/Vector3.Dot.html

    https://en.wikipedia.org/wiki/Dot_product

    There are readily available implementations for convex hulls algorithms though, such as this one: https://github.com/masphei/ConvexHull