feat(physics): added area & inertia calculations for the shape and circles
This commit is contained in:
@@ -22,6 +22,31 @@ public class Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
|
||||
private readonly List<Vector2D> _vertices = vertices;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the area of the <see cref="Shape2D"/>.
|
||||
/// </summary>
|
||||
public float Area
|
||||
{
|
||||
get
|
||||
{
|
||||
float area = 0f;
|
||||
|
||||
for (int i = 0; i < _vertices.Count; i++)
|
||||
{
|
||||
Vector2D a = _vertices[i];
|
||||
Vector2D b = _vertices[(i + 1) % _vertices.Count];
|
||||
area += a.Cross(b);
|
||||
}
|
||||
|
||||
return area.Abs() * .5f;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the geometric interia of the <see cref="Shape2D"/>.
|
||||
/// </summary>
|
||||
public float GeometricInertia => GetGeometricInertia(this, Vector2D.Zero);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the vertices of the <see cref="Shape2D"/>.
|
||||
/// </summary>
|
||||
@@ -112,6 +137,40 @@ public class Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
return new Triangle(p1, p2, p3);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the geometric interia of the <see cref="Shape2D"/>.
|
||||
/// </summary>
|
||||
/// <param name="shape">The shape to get the geometrical interia of.</param>
|
||||
/// <param name="centerOfMass">The point in space to calculate the geometrical interia from.</param>
|
||||
/// <returns>The geometrical interia of the <see cref="Shape2D"/>.</returns>
|
||||
public static float GetGeometricInertia(Shape2D shape, Vector2D centerOfMass)
|
||||
{
|
||||
float geometricInertia = 0f;
|
||||
|
||||
for (int i = 0; i < shape._vertices.Count; i++)
|
||||
{
|
||||
Vector2D p1 = centerOfMass.FromTo(shape._vertices[i]);
|
||||
Vector2D p2 = centerOfMass.FromTo(shape._vertices[(i + 1) % shape._vertices.Count]);
|
||||
|
||||
float cross = p1.Cross(p2);
|
||||
float dot = p1.Dot(p1) + p1.Dot(p2) + p2.Dot(p2);
|
||||
|
||||
geometricInertia += cross * dot;
|
||||
}
|
||||
|
||||
return geometricInertia.Abs() / 12f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the interia of the <see cref="Shape2D"/>.
|
||||
/// </summary>
|
||||
/// <param name="shape">The shape to get the interia of.</param>
|
||||
/// <param name="centerOfMass">The point in space to calculate the geometrical interia from.</param>
|
||||
/// <param name="mass">Mass of the shape.</param>
|
||||
/// <returns>The interia of the <see cref="Shape2D"/>.</returns>
|
||||
public static float GetInertia(Shape2D shape, Vector2D centerOfMass, float mass)
|
||||
=> GetGeometricInertia(shape, centerOfMass) * mass;
|
||||
|
||||
/// <summary>
|
||||
/// Triangulates the given convex <see cref="Shape2D"/>.
|
||||
/// </summary>
|
||||
@@ -297,6 +356,12 @@ public static class Shape2DExtensions
|
||||
/// <inheritdoc cref="Shape2D.GetSuperTriangle(Shape2D)" />
|
||||
public static Triangle ToSuperTriangle(this Shape2D shape) => Shape2D.GetSuperTriangle(shape);
|
||||
|
||||
/// <inheritdoc cref="Shape2D.GetGeometricInertia(Shape2D, Vector2D)" />
|
||||
public static float GetGeometricInertia(this Shape2D shape, Vector2D centerOfMass) => Shape2D.GetGeometricInertia(shape, centerOfMass);
|
||||
|
||||
/// <inheritdoc cref="Shape2D.GetInertia(Shape2D, Vector2D, float)" />
|
||||
public static float GetInertia(this Shape2D shape, Vector2D centerOfMass, float mass) => Shape2D.GetInertia(shape, centerOfMass, mass);
|
||||
|
||||
/// <inheritdoc cref="Shape2D.TriangulateConvex(Shape2D, IList{Triangle})" />
|
||||
public static void ToTrianglesConvex(this Shape2D shape, IList<Triangle> triangles) => Shape2D.TriangulateConvex(shape, triangles);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user