Exemplo n.º 1
0
        /// <summary>
        /// Calculates the rotational inertia for a polygon with a given mass
        /// Passed in polygon does not need to be in local space
        /// </summary>
        /// <param name="polygon"></param>
        /// <param name="mass"></param>
        /// <returns></returns>
        public static float GetInertiaForPolygon(gxtPolygon polygon, float mass)
        {
            gxtDebug.Assert(polygon.NumVertices >= 3);
            
            // perform a deep copy so we don't corrupt the values of the polygon
            Vector2[] vertices = new Vector2[polygon.NumVertices];
            for (int i = 0; i < vertices.Length; ++i)
            {
                vertices[i] = polygon.v[i];
            }

            Vector2 centroid = polygon.GetCentroid();
            if (centroid != Vector2.Zero)
            {
                for (int i = 0; i < vertices.Length; ++i)
                {
                    vertices[i] -= centroid;
                }
                    //polygon.Translate(-centroid);
            }
            
            float denom = 0.0f;
            float numer = 0.0f;
            for (int j = vertices.Length - 1, i = 0; i < vertices.Length; j = i, i++)
            {
                float a = vertices[i].LengthSquared();
                float b = Vector2.Dot(vertices[i], vertices[j]);
                float c = vertices[j].LengthSquared();
                float d = gxtMath.Abs(gxtMath.Cross2D(vertices[j], vertices[i]));

                numer += d;
                denom += (a + b + c) * d;
            }
            return denom / (numer * 6.0f);
        }