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?
Asked
Active
Viewed 276 times
1 Answers
1
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

Brice V.
- 861
- 4
- 7
-
Thanks! I thought LINQ was the answer, but couldn't quite get my head around it. – flibit Mar 31 '20 at 21:09