简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM