Compare commits
16 Commits
main
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
| 5cd3b938f7 | |||
| f81bd61aa1 | |||
| 6d8ba5c80c | |||
| b713fe4f12 | |||
| 4b3a0fdde0 | |||
| 44ff916afe | |||
| 497eedab72 | |||
| 3893a1d249 | |||
| 4255cc4893 | |||
| 37f4f56cd6 | |||
| 629d758dbc | |||
| 7fb6821a83 | |||
| af2eed2200 | |||
| 6db427f39b | |||
| 53b342da46 | |||
| 4c13578125 |
@@ -30,6 +30,16 @@ public static class Math
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public const float DegreeToRadian = Pi / 180f;
|
public const float DegreeToRadian = Pi / 180f;
|
||||||
|
|
||||||
|
/// <inheritdoc cref="Invert{T}(T)" />
|
||||||
|
public static T OneOver<T>(T value) where T : INumber<T> => T.One / value;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets 1 / value of given <see cref="T"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value <see cref="T"/>.</param>
|
||||||
|
/// <returns>1 / value of given <see cref="T"/>.</returns>
|
||||||
|
public static T Invert<T>(T value) where T : INumber<T> => T.One / value;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets one minus of given <see cref="T"/>.
|
/// Gets one minus of given <see cref="T"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -8,6 +8,12 @@ public static class MathExtensions
|
|||||||
/// <inheritdoc cref="Math.OneMinus{T}(T)" />
|
/// <inheritdoc cref="Math.OneMinus{T}(T)" />
|
||||||
public static T OneMinus<T>(this T value) where T : INumber<T> => Math.OneMinus(value);
|
public static T OneMinus<T>(this T value) where T : INumber<T> => Math.OneMinus(value);
|
||||||
|
|
||||||
|
/// <inheritdoc cref="Math.OneOver{T}(T)" />
|
||||||
|
public static T OneOver<T>(this T value) where T : INumber<T> => Math.OneOver(value);
|
||||||
|
|
||||||
|
/// <inheritdoc cref="Math.Invert{T}(T)" />
|
||||||
|
public static T Invert<T>(this T value) where T : INumber<T> => Math.OneMinus(value);
|
||||||
|
|
||||||
/// <inheritdoc cref="Math.Add{T}(T, T)" />
|
/// <inheritdoc cref="Math.Add{T}(T, T)" />
|
||||||
public static T Add<T>(this T left, T value) where T : INumber<T> => Math.Add(left, value);
|
public static T Add<T>(this T left, T value) where T : INumber<T> => Math.Add(left, value);
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,16 @@ public readonly struct Circle(Vector2D center, float radius) : IEquatable<Circle
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly float Diameter => 2f * Radius;
|
public readonly float Diameter => 2f * Radius;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the area of the <see cref="Circle"/>.
|
||||||
|
/// </summary>
|
||||||
|
public readonly float Area => Math.Pi * RadiusSquared;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the geometric interia of the <see cref="Circle"/>.
|
||||||
|
/// </summary>
|
||||||
|
public readonly float GeometricInertia => .5f * RadiusSquared;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A predefined unit <see cref="Circle"/> with a center at the origin and a radius of 1.
|
/// A predefined unit <see cref="Circle"/> with a center at the origin and a radius of 1.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -22,6 +22,31 @@ public class Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
|||||||
|
|
||||||
private readonly List<Vector2D> _vertices = vertices;
|
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>
|
/// <summary>
|
||||||
/// Gets the vertices of the <see cref="Shape2D"/>.
|
/// Gets the vertices of the <see cref="Shape2D"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -112,6 +137,40 @@ public class Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
|||||||
return new Triangle(p1, p2, p3);
|
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>
|
/// <summary>
|
||||||
/// Triangulates the given convex <see cref="Shape2D"/>.
|
/// Triangulates the given convex <see cref="Shape2D"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -297,6 +356,12 @@ public static class Shape2DExtensions
|
|||||||
/// <inheritdoc cref="Shape2D.GetSuperTriangle(Shape2D)" />
|
/// <inheritdoc cref="Shape2D.GetSuperTriangle(Shape2D)" />
|
||||||
public static Triangle ToSuperTriangle(this Shape2D shape) => Shape2D.GetSuperTriangle(shape);
|
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})" />
|
/// <inheritdoc cref="Shape2D.TriangulateConvex(Shape2D, IList{Triangle})" />
|
||||||
public static void ToTrianglesConvex(this Shape2D shape, IList<Triangle> triangles) => Shape2D.TriangulateConvex(shape, triangles);
|
public static void ToTrianglesConvex(this Shape2D shape, IList<Triangle> triangles) => Shape2D.TriangulateConvex(shape, triangles);
|
||||||
|
|
||||||
|
|||||||
9
Engine.Core/Systems/Abstract/ICoroutineManager.cs
Normal file
9
Engine.Core/Systems/Abstract/ICoroutineManager.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace Engine.Core;
|
||||||
|
|
||||||
|
public interface ICoroutineManager
|
||||||
|
{
|
||||||
|
IEnumerator StartCoroutine(IEnumerator enumerator);
|
||||||
|
void StopCoroutine(IEnumerator enumerator);
|
||||||
|
}
|
||||||
80
Engine.Core/Systems/NestedCoroutineManager.cs
Normal file
80
Engine.Core/Systems/NestedCoroutineManager.cs
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Engine.Core;
|
||||||
|
|
||||||
|
public class NestedCoroutineManager : Behaviour, IUpdate, ICoroutineManager
|
||||||
|
{
|
||||||
|
private readonly List<CoroutineStack> stacks = [];
|
||||||
|
private readonly Pool<CoroutineStack> pool = new(() => new());
|
||||||
|
|
||||||
|
public IEnumerator StartCoroutine(IEnumerator enumerator)
|
||||||
|
{
|
||||||
|
CoroutineStack stack = pool.Get();
|
||||||
|
stack.EntryPoint = enumerator;
|
||||||
|
stack.Stack.Push(enumerator);
|
||||||
|
|
||||||
|
stacks.Add(stack);
|
||||||
|
|
||||||
|
return enumerator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StopCoroutine(IEnumerator enumerator)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < stacks.Count; i++)
|
||||||
|
if (stacks[i].EntryPoint == enumerator)
|
||||||
|
{
|
||||||
|
RemoveCoroutineAt(i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RemoveCoroutineAt(int i)
|
||||||
|
{
|
||||||
|
stacks[i].Reset();
|
||||||
|
stacks.RemoveAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IUpdate.Update()
|
||||||
|
{
|
||||||
|
for (int i = stacks.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
Stack<IEnumerator> stack = stacks[i].Stack;
|
||||||
|
|
||||||
|
if (stack.Count == 0)
|
||||||
|
{
|
||||||
|
RemoveCoroutineAt(i);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator top = stack.Peek();
|
||||||
|
|
||||||
|
if (top.Current is ICoroutineYield coroutineYield && coroutineYield.Yield())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (top.Current is IEnumerator nested)
|
||||||
|
{
|
||||||
|
stack.Push(nested);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!top.MoveNext())
|
||||||
|
{
|
||||||
|
stack.Pop();
|
||||||
|
if (stack.Count != 0)
|
||||||
|
stack.Peek().MoveNext();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class CoroutineStack
|
||||||
|
{
|
||||||
|
public IEnumerator EntryPoint = null!;
|
||||||
|
public Stack<IEnumerator> Stack = new();
|
||||||
|
|
||||||
|
public void Reset() { EntryPoint = null!; Stack.Clear(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public NestedCoroutineManager() => Priority = int.MinValue;
|
||||||
|
}
|
||||||
@@ -4,32 +4,32 @@ using static Engine.Core.WaitForTaskYield;
|
|||||||
|
|
||||||
namespace Engine.Core;
|
namespace Engine.Core;
|
||||||
|
|
||||||
public class WaitForTaskYield(Task task, TaskCompletionStatus completionStatus = TaskCompletionStatus.Either) : ICoroutineYield
|
public class WaitForTaskYield(Task task, TaskCompletionStatus completionStatus = TaskCompletionStatus.Any) : ICoroutineYield
|
||||||
{
|
{
|
||||||
public bool Yield()
|
public bool Yield()
|
||||||
{
|
{
|
||||||
switch (completionStatus)
|
switch (completionStatus)
|
||||||
{
|
{
|
||||||
case TaskCompletionStatus.Successful:
|
case TaskCompletionStatus.Success:
|
||||||
if (task.IsCanceled)
|
if (task.IsCanceled)
|
||||||
throw new("Task has been canceled.");
|
throw new("Task has been canceled.");
|
||||||
if (task.IsFaulted)
|
if (task.IsFaulted)
|
||||||
throw new("Task has faulted.");
|
throw task.Exception ?? new("Task has faulted.");
|
||||||
return task.IsCompletedSuccessfully;
|
return !task.IsCompletedSuccessfully;
|
||||||
|
|
||||||
case TaskCompletionStatus.Failed:
|
case TaskCompletionStatus.Fail:
|
||||||
if (task.IsCompletedSuccessfully)
|
if (task.IsCompletedSuccessfully)
|
||||||
throw new("Task was completed successfully.");
|
throw new("Task was completed successfully.");
|
||||||
return task.IsFaulted;
|
return !task.IsFaulted;
|
||||||
}
|
}
|
||||||
|
|
||||||
return task.IsCompleted;
|
return !task.IsCompleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum TaskCompletionStatus
|
public enum TaskCompletionStatus
|
||||||
{
|
{
|
||||||
Either,
|
Any,
|
||||||
Successful,
|
Success,
|
||||||
Failed
|
Fail
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
Engine.Core/Systems/Yields/YieldExtensions.cs
Normal file
10
Engine.Core/Systems/Yields/YieldExtensions.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using static Engine.Core.WaitForTaskYield;
|
||||||
|
|
||||||
|
namespace Engine.Core;
|
||||||
|
|
||||||
|
public static class YieldExtensions
|
||||||
|
{
|
||||||
|
public static WaitForTaskYield ToYield(this Task task, TaskCompletionStatus completionStatus = TaskCompletionStatus.Any) => new(task, completionStatus);
|
||||||
|
}
|
||||||
@@ -37,17 +37,25 @@ public class SerializedClassConverter : EngineTypeYamlConverterBase<SerializedCl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach ((string key, object @object) in publicDictionary)
|
||||||
|
if (@object is IDictionary<object, object> dictionary && dictionary.TryGetValue(nameof(TypeContainer.Type), out object? typeField) && dictionary.TryGetValue(nameof(TypeContainer.Value), out object? valueField))
|
||||||
|
publicDictionary[key] = new TypeContainer() { Type = typeField!.ToString()!, Value = valueField };
|
||||||
|
|
||||||
|
foreach ((string key, object @object) in privateDictionary)
|
||||||
|
if (@object is IDictionary<object, object> dictionary && dictionary.TryGetValue(nameof(TypeContainer.Type), out object? typeField) && dictionary.TryGetValue(nameof(TypeContainer.Value), out object? valueField))
|
||||||
|
privateDictionary[key] = new TypeContainer() { Type = typeField!.ToString()!, Value = valueField };
|
||||||
|
|
||||||
Type classType = TypeFactory.GetType(serializedClass.Type);
|
Type classType = TypeFactory.GetType(serializedClass.Type);
|
||||||
|
|
||||||
foreach ((string key, object @object) in publicDictionary)
|
foreach ((string key, object @object) in publicDictionary)
|
||||||
if (@object is TypeContainer typeContainer)
|
if (@object is TypeContainer typeContainer)
|
||||||
serializedClass.Public.Add(key, Serializer.InternalDeserialize(typeContainer.Value!.ToString()!, TypeFactory.GetType(typeContainer.Type)));
|
serializedClass.Public.Add(key, Serializer.InternalDeserialize(Serializer.Serialize(typeContainer.Value!), TypeFactory.GetType(typeContainer.Type)));
|
||||||
else
|
else
|
||||||
serializedClass.Public.Add(key, Serializer.InternalDeserialize(@object.ToString()!, (classType.GetProperty(key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)?.PropertyType ?? classType.GetField(key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)?.FieldType)!));
|
serializedClass.Public.Add(key, Serializer.InternalDeserialize(@object.ToString()!, (classType.GetProperty(key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)?.PropertyType ?? classType.GetField(key, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)?.FieldType)!));
|
||||||
|
|
||||||
foreach ((string key, object @object) in privateDictionary)
|
foreach ((string key, object @object) in privateDictionary)
|
||||||
if (@object is TypeContainer typeContainer)
|
if (@object is TypeContainer typeContainer)
|
||||||
serializedClass.Private.Add(key, Serializer.InternalDeserialize(typeContainer.Value!.ToString()!, TypeFactory.GetType(typeContainer.Type)));
|
serializedClass.Private.Add(key, Serializer.InternalDeserialize(Serializer.Serialize(typeContainer.Value!), TypeFactory.GetType(typeContainer.Type)));
|
||||||
else
|
else
|
||||||
serializedClass.Private.Add(key, Serializer.InternalDeserialize(@object.ToString()!, (classType.GetProperty(key, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Default)?.PropertyType ?? classType.GetField(key, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Default)?.FieldType)!));
|
serializedClass.Private.Add(key, Serializer.InternalDeserialize(@object.ToString()!, (classType.GetProperty(key, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Default)?.PropertyType ?? classType.GetField(key, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Default)?.FieldType)!));
|
||||||
|
|
||||||
@@ -65,14 +73,14 @@ public class SerializedClassConverter : EngineTypeYamlConverterBase<SerializedCl
|
|||||||
Dictionary<string, object> publics = [];
|
Dictionary<string, object> publics = [];
|
||||||
Dictionary<string, object> privates = [];
|
Dictionary<string, object> privates = [];
|
||||||
|
|
||||||
foreach ((string key, object? @object) in serializedClass.Public.Where(v => !v.GetType().HasAttribute<IgnoreSerializationAttribute>()))
|
foreach ((string key, object? @object) in serializedClass.Public)
|
||||||
if (@object?.GetType().IsClass == false)
|
if (@object?.GetType().IsClass == false || @object is string)
|
||||||
publics.Add(key, @object!);
|
publics.Add(key, @object!);
|
||||||
else
|
else
|
||||||
publics.Add(key, new TypeContainer(@object));
|
publics.Add(key, new TypeContainer(@object));
|
||||||
|
|
||||||
foreach ((string key, object? @object) in serializedClass.Private.Where(v => !v.GetType().HasAttribute<IgnoreSerializationAttribute>()))
|
foreach ((string key, object? @object) in serializedClass.Private)
|
||||||
if (@object?.GetType().IsClass == false)
|
if (@object?.GetType().IsClass == false || @object is string)
|
||||||
privates.Add(key, @object!);
|
privates.Add(key, @object!);
|
||||||
else
|
else
|
||||||
privates.Add(key, new TypeContainer(@object));
|
privates.Add(key, new TypeContainer(@object));
|
||||||
|
|||||||
@@ -7,6 +7,11 @@ namespace Engine.Physics2D;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface ICollider2D : IBehaviour
|
public interface ICollider2D : IBehaviour
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Event triggered when the collider is recalculated.
|
||||||
|
/// </summary>
|
||||||
|
Event<ICollider2D> OnRecalculated { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event triggered when a collision is detected.
|
/// Event triggered when a collision is detected.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -30,6 +35,16 @@ public interface ICollider2D : IBehaviour
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
IRigidBody2D? RigidBody2D { get; }
|
IRigidBody2D? RigidBody2D { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The area of the <see cref="ICollider2D"/>.
|
||||||
|
/// </summary>
|
||||||
|
float Area { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The geometric inertia of the <see cref="ICollider2D"/>.
|
||||||
|
/// </summary>
|
||||||
|
float GeometricInertia { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The value indicating whether the <see cref="ICollider2D"/> is a trigger.
|
/// The value indicating whether the <see cref="ICollider2D"/> is a trigger.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -27,6 +27,21 @@ public interface IRigidBody2D : IBehaviour2D
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
float Mass { get; set; }
|
float Mass { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The inverse mass (1 / Mass) of the <see cref="IRigidBody2D"/>.
|
||||||
|
/// </summary>
|
||||||
|
float InverseMass { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The Invertia of the <see cref="IRigidBody2D"/>.
|
||||||
|
/// </summary>
|
||||||
|
float Inertia { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The inverse Invertia (1 / Invertia) of the <see cref="IRigidBody2D"/>.
|
||||||
|
/// </summary>
|
||||||
|
float InverseInertia { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The value indicating whether the <see cref="IRigidBody2D"/> is static/immovable.
|
/// The value indicating whether the <see cref="IRigidBody2D"/> is static/immovable.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace Engine.Physics2D;
|
|||||||
|
|
||||||
public abstract class Collider2DBase : Behaviour2D, ICollider2D
|
public abstract class Collider2DBase : Behaviour2D, ICollider2D
|
||||||
{
|
{
|
||||||
|
public Event<ICollider2D> OnRecalculated { get; } = new();
|
||||||
public Event<ICollider2D, CollisionDetectionInformation> OnCollisionDetected { get; } = new();
|
public Event<ICollider2D, CollisionDetectionInformation> OnCollisionDetected { get; } = new();
|
||||||
public Event<ICollider2D, CollisionDetectionInformation> OnCollisionResolved { get; } = new();
|
public Event<ICollider2D, CollisionDetectionInformation> OnCollisionResolved { get; } = new();
|
||||||
public Event<ICollider2D, ICollider2D> OnTriggered { get; } = new();
|
public Event<ICollider2D, ICollider2D> OnTriggered { get; } = new();
|
||||||
@@ -20,6 +21,9 @@ public abstract class Collider2DBase : Behaviour2D, ICollider2D
|
|||||||
public IRigidBody2D? RigidBody2D { get; protected set; } = null;
|
public IRigidBody2D? RigidBody2D { get; protected set; } = null;
|
||||||
public bool IsTrigger { get; set; } = false;
|
public bool IsTrigger { get; set; } = false;
|
||||||
|
|
||||||
|
public abstract float Area { get; }
|
||||||
|
public abstract float GeometricInertia { get; }
|
||||||
|
|
||||||
public void Recalculate()
|
public void Recalculate()
|
||||||
{
|
{
|
||||||
if (!NeedsRecalculation)
|
if (!NeedsRecalculation)
|
||||||
@@ -27,6 +31,7 @@ public abstract class Collider2DBase : Behaviour2D, ICollider2D
|
|||||||
|
|
||||||
CalculateCollider();
|
CalculateCollider();
|
||||||
NeedsRecalculation = false;
|
NeedsRecalculation = false;
|
||||||
|
OnRecalculated.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void CalculateCollider();
|
public abstract void CalculateCollider();
|
||||||
|
|||||||
@@ -15,7 +15,15 @@ public class Collider2DCircle : Collider2DBase, ICircleCollider2D
|
|||||||
}
|
}
|
||||||
} = Circle.UnitCircle;
|
} = Circle.UnitCircle;
|
||||||
|
|
||||||
public override void CalculateCollider() => CircleWorld = Transform.Transform(CircleLocal);
|
private float area = 0f; public override float Area => area;
|
||||||
|
private float geometricInertia = 0f; public override float GeometricInertia => geometricInertia;
|
||||||
|
|
||||||
|
public override void CalculateCollider()
|
||||||
|
{
|
||||||
|
CircleWorld = Transform.Transform(CircleLocal);
|
||||||
|
area = CircleWorld.Area;
|
||||||
|
geometricInertia = CircleWorld.GeometricInertia;
|
||||||
|
}
|
||||||
|
|
||||||
public Collider2DCircle() { }
|
public Collider2DCircle() { }
|
||||||
public Collider2DCircle(Circle circle) => CircleLocal = circle;
|
public Collider2DCircle(Circle circle) => CircleLocal = circle;
|
||||||
|
|||||||
@@ -15,7 +15,16 @@ public class Collider2DShape : Collider2DBase, IShapeCollider2D
|
|||||||
}
|
}
|
||||||
} = Shape2D.Square;
|
} = Shape2D.Square;
|
||||||
|
|
||||||
public override void CalculateCollider() => ShapeLocal.Transform(Transform, ShapeWorld);
|
private float area = 0f; public override float Area => area;
|
||||||
|
private float geometricInertia = 0f; public override float GeometricInertia => geometricInertia;
|
||||||
|
|
||||||
|
public override void CalculateCollider()
|
||||||
|
{
|
||||||
|
ShapeLocal.Transform(Transform, ShapeWorld);
|
||||||
|
|
||||||
|
area = ShapeWorld.Area;
|
||||||
|
geometricInertia = ShapeWorld.GetGeometricInertia(Transform.Position);
|
||||||
|
}
|
||||||
|
|
||||||
public Collider2DShape() { }
|
public Collider2DShape() { }
|
||||||
public Collider2DShape(Shape2D shape) { ShapeLocal = shape; }
|
public Collider2DShape(Shape2D shape) { ShapeLocal = shape; }
|
||||||
|
|||||||
@@ -7,14 +7,16 @@ public readonly struct CollisionDetectionInformation
|
|||||||
(
|
(
|
||||||
ICollider2D Detector,
|
ICollider2D Detector,
|
||||||
ICollider2D Detected,
|
ICollider2D Detected,
|
||||||
|
Vector2D ContactPoint,
|
||||||
Vector2D Normal,
|
Vector2D Normal,
|
||||||
float Penetration
|
float Penetration
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
public ICollider2D Detector { get; init; } = Detector;
|
public ICollider2D Detector { get; init; } = Detector;
|
||||||
public ICollider2D Detected { get; init; } = Detected;
|
public ICollider2D Detected { get; init; } = Detected;
|
||||||
|
public Vector2D ContactPoint { get; init; } = ContactPoint;
|
||||||
public Vector2D Normal { get; init; } = Normal;
|
public Vector2D Normal { get; init; } = Normal;
|
||||||
public float Penetration { get; init; } = Penetration;
|
public float Penetration { get; init; } = Penetration;
|
||||||
|
|
||||||
public CollisionDetectionInformation Reverse() => new(Detected, Detector, -Normal, Penetration);
|
public CollisionDetectionInformation Reverse() => new(Detected, Detector, ContactPoint, -Normal, Penetration);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using Engine.Core;
|
using Engine.Core;
|
||||||
using Engine.Physics2D;
|
using Engine.Physics2D;
|
||||||
|
|
||||||
@@ -41,12 +43,13 @@ public class CollisionDetector2D : ICollisionDetector2D
|
|||||||
|
|
||||||
private static bool DetectShapeShapeOneWay(IShapeCollider2D left, IShapeCollider2D right, ref CollisionDetectionInformation collisionInformation)
|
private static bool DetectShapeShapeOneWay(IShapeCollider2D left, IShapeCollider2D right, ref CollisionDetectionInformation collisionInformation)
|
||||||
{
|
{
|
||||||
System.Collections.Generic.IReadOnlyList<Vector2D> vertices = left.ShapeWorld.Vertices;
|
IReadOnlyList<Vector2D> vertices = left.ShapeWorld.Vertices;
|
||||||
int count = vertices.Count;
|
int count = vertices.Count;
|
||||||
|
|
||||||
for (int indexProjection = 0; indexProjection < count; indexProjection++)
|
for (int indexProjection = 0; indexProjection < count; indexProjection++)
|
||||||
{
|
{
|
||||||
Vector2D projectionVector = vertices[indexProjection].FromTo(vertices[(indexProjection + 1) % count]).Perpendicular().Normalized;
|
Vector2D leftEdge = vertices[indexProjection].FromTo(vertices[(indexProjection + 1) % count]);
|
||||||
|
Vector2D projectionVector = leftEdge.Perpendicular().Normalized;
|
||||||
|
|
||||||
Projection1D leftProjection = left.ShapeWorld.ToProjection(projectionVector);
|
Projection1D leftProjection = left.ShapeWorld.ToProjection(projectionVector);
|
||||||
Projection1D rightProjection = right.ShapeWorld.ToProjection(projectionVector);
|
Projection1D rightProjection = right.ShapeWorld.ToProjection(projectionVector);
|
||||||
@@ -55,17 +58,61 @@ public class CollisionDetector2D : ICollisionDetector2D
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth))
|
if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth))
|
||||||
collisionInformation = new(left, right, projectionVector, depth);
|
{
|
||||||
|
Vector2D contactPoint = FindShapeToShapeContactPoint(left, right, projectionVector);
|
||||||
|
collisionInformation = new(left, right, contactPoint, projectionVector, depth);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Vector2D FindShapeToShapeContactPoint(IShapeCollider2D left, IShapeCollider2D right, Vector2D contactProjectionVector)
|
||||||
|
{
|
||||||
|
IReadOnlyList<Vector2D> leftVertices = left.ShapeWorld.Vertices;
|
||||||
|
IReadOnlyList<Vector2D> rightVertices = right.ShapeWorld.Vertices;
|
||||||
|
|
||||||
|
Line2D leftSupportLine = GetSupportLine(leftVertices, contactProjectionVector);
|
||||||
|
Line2D rightSupportLine = GetSupportLine(rightVertices, -contactProjectionVector);
|
||||||
|
|
||||||
|
if (leftSupportLine.Direction.Dot(rightSupportLine.Direction).Abs() > .99f)
|
||||||
|
return (leftSupportLine.From + leftSupportLine.To + rightSupportLine.From + rightSupportLine.To) / 4f;
|
||||||
|
|
||||||
|
return leftSupportLine.IntersectionPoint(rightSupportLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Line2D GetSupportLine(IReadOnlyList<Vector2D> vertices, Vector2D contactProjectionVector)
|
||||||
|
{
|
||||||
|
System.Span<Vector2D> points = stackalloc Vector2D[2];
|
||||||
|
System.Span<float> distances = stackalloc float[2] { float.MaxValue, float.MaxValue };
|
||||||
|
for (int i = 0; i < vertices.Count; i++)
|
||||||
|
{
|
||||||
|
Vector2D point = vertices[i];
|
||||||
|
float distance = contactProjectionVector.Dot(point);
|
||||||
|
|
||||||
|
if (distance < distances[0])
|
||||||
|
{
|
||||||
|
points[1] = points[0];
|
||||||
|
distances[1] = distances[0];
|
||||||
|
|
||||||
|
points[0] = point;
|
||||||
|
distances[0] = distance;
|
||||||
|
}
|
||||||
|
else if (distance < distances[1])
|
||||||
|
{
|
||||||
|
points[1] = point;
|
||||||
|
distances[1] = distance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new(points[0], points[1]);
|
||||||
|
}
|
||||||
|
|
||||||
private static bool DetectShapeCircle(IShapeCollider2D shapeCollider, ICircleCollider2D circleCollider, out CollisionDetectionInformation collisionInformation)
|
private static bool DetectShapeCircle(IShapeCollider2D shapeCollider, ICircleCollider2D circleCollider, out CollisionDetectionInformation collisionInformation)
|
||||||
{
|
{
|
||||||
collisionInformation = default;
|
collisionInformation = default;
|
||||||
|
|
||||||
System.Collections.Generic.IReadOnlyList<Vector2D> vertices = shapeCollider.ShapeWorld.Vertices;
|
IReadOnlyList<Vector2D> vertices = shapeCollider.ShapeWorld.Vertices;
|
||||||
int count = vertices.Count;
|
int count = vertices.Count;
|
||||||
|
|
||||||
for (int indexProjection = 0; indexProjection < count; indexProjection++)
|
for (int indexProjection = 0; indexProjection < count; indexProjection++)
|
||||||
@@ -78,8 +125,10 @@ public class CollisionDetector2D : ICollisionDetector2D
|
|||||||
if (!shapeProjection.Overlaps(circleProjection, out float depth))
|
if (!shapeProjection.Overlaps(circleProjection, out float depth))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
Vector2D contactPoint = circleCollider.CircleWorld.Center + projectionVector * circleCollider.CircleWorld.Radius;
|
||||||
|
|
||||||
if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth))
|
if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth))
|
||||||
collisionInformation = new(shapeCollider, circleCollider, projectionVector, depth);
|
collisionInformation = new(shapeCollider, circleCollider, contactPoint, projectionVector, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -91,8 +140,10 @@ public class CollisionDetector2D : ICollisionDetector2D
|
|||||||
if (!shapeProjection.Overlaps(circleProjection, out float depth))
|
if (!shapeProjection.Overlaps(circleProjection, out float depth))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
Vector2D contactPoint = circleCollider.CircleWorld.Center + shapeToCircleProjectionVector * circleCollider.CircleWorld.Radius;
|
||||||
|
|
||||||
if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth))
|
if (collisionInformation.Detector is null || Math.Abs(collisionInformation.Penetration) > Math.Abs(depth))
|
||||||
collisionInformation = new(shapeCollider, circleCollider, shapeToCircleProjectionVector, depth);
|
collisionInformation = new(shapeCollider, circleCollider, contactPoint, shapeToCircleProjectionVector, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -110,7 +161,10 @@ public class CollisionDetector2D : ICollisionDetector2D
|
|||||||
bool collision = leftProjection.Overlaps(rightProjection, out float depth);
|
bool collision = leftProjection.Overlaps(rightProjection, out float depth);
|
||||||
|
|
||||||
if (collision)
|
if (collision)
|
||||||
collisionInformation = new(left, right, leftToRightCenterProjectionVector, depth);
|
{
|
||||||
|
Vector2D contactPoint = left.CircleWorld.Center + leftToRightCenterProjectionVector * left.CircleWorld.Radius;
|
||||||
|
collisionInformation = new(left, right, contactPoint, leftToRightCenterProjectionVector, depth);
|
||||||
|
}
|
||||||
|
|
||||||
return collision;
|
return collision;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ public class CollisionResolver2D : ICollisionResolver2D
|
|||||||
{
|
{
|
||||||
public void Resolve(CollisionDetectionInformation collisionInformation)
|
public void Resolve(CollisionDetectionInformation collisionInformation)
|
||||||
{
|
{
|
||||||
Vector2D displacementVector = collisionInformation.Normal * collisionInformation.Penetration;
|
|
||||||
|
|
||||||
ICollider2D left = collisionInformation.Detector;
|
ICollider2D left = collisionInformation.Detector;
|
||||||
ICollider2D right = collisionInformation.Detected;
|
ICollider2D right = collisionInformation.Detected;
|
||||||
|
|
||||||
@@ -17,6 +15,20 @@ public class CollisionResolver2D : ICollisionResolver2D
|
|||||||
if (isLeftStatic && isRightStatic)
|
if (isLeftStatic && isRightStatic)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
Displace(collisionInformation, left, right, isLeftStatic, isRightStatic);
|
||||||
|
Bounce(collisionInformation, left, right, isLeftStatic, isRightStatic);
|
||||||
|
|
||||||
|
left.Recalculate();
|
||||||
|
right.Recalculate();
|
||||||
|
|
||||||
|
left.Resolve(collisionInformation);
|
||||||
|
right.Resolve(collisionInformation);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Displace(CollisionDetectionInformation collisionInformation, ICollider2D left, ICollider2D right, bool isLeftStatic, bool isRightStatic)
|
||||||
|
{
|
||||||
|
Vector2D displacementVector = collisionInformation.Normal * collisionInformation.Penetration;
|
||||||
|
|
||||||
if (isLeftStatic)
|
if (isLeftStatic)
|
||||||
right.Transform.Position += displacementVector;
|
right.Transform.Position += displacementVector;
|
||||||
else if (isRightStatic)
|
else if (isRightStatic)
|
||||||
@@ -33,11 +45,32 @@ public class CollisionResolver2D : ICollisionResolver2D
|
|||||||
right.Transform.Position += leftMomentumPercentage * displacementVector;
|
right.Transform.Position += leftMomentumPercentage * displacementVector;
|
||||||
left.Transform.Position -= rightMomentumPercentage * displacementVector;
|
left.Transform.Position -= rightMomentumPercentage * displacementVector;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
left.Recalculate();
|
private static void Bounce(CollisionDetectionInformation collisionInformation, ICollider2D left, ICollider2D right, bool isLeftStatic, bool isRightStatic)
|
||||||
right.Recalculate();
|
{
|
||||||
|
Vector2D leftVelocity = left.RigidBody2D?.Velocity ?? Vector2D.Zero;
|
||||||
|
Vector2D rightVelocity = right.RigidBody2D?.Velocity ?? Vector2D.Zero;
|
||||||
|
|
||||||
left.Resolve(collisionInformation);
|
Vector2D relativeVelocity = leftVelocity - rightVelocity;
|
||||||
right.Resolve(collisionInformation);
|
float velocityAlongNormal = Vector2D.Dot(relativeVelocity, collisionInformation.Normal);
|
||||||
|
|
||||||
|
if (velocityAlongNormal > 0)
|
||||||
|
{
|
||||||
|
collisionInformation = collisionInformation.Reverse();
|
||||||
|
velocityAlongNormal = -velocityAlongNormal;
|
||||||
|
}
|
||||||
|
|
||||||
|
float e = (left.RigidBody2D?.Material.Restitution ?? 0f).Add(right.RigidBody2D?.Material.Restitution ?? 0f).Divide(2f);
|
||||||
|
|
||||||
|
float leftMassEffective = isLeftStatic ? float.PositiveInfinity : left.RigidBody2D?.Mass ?? float.Epsilon;
|
||||||
|
float rightMassEffective = isRightStatic ? float.PositiveInfinity : right.RigidBody2D?.Mass ?? float.Epsilon;
|
||||||
|
float impulse = -(1f + e) * velocityAlongNormal / ((1f / leftMassEffective) + (1f / rightMassEffective));
|
||||||
|
|
||||||
|
if (!isLeftStatic)
|
||||||
|
left.RigidBody2D?.Velocity += impulse / leftMassEffective * collisionInformation.Normal;
|
||||||
|
|
||||||
|
if (!isRightStatic)
|
||||||
|
right.RigidBody2D?.Velocity -= impulse / rightMassEffective * collisionInformation.Normal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
namespace Engine.Physics2D;
|
namespace Engine.Physics2D;
|
||||||
|
|
||||||
public readonly struct PhysicsMaterial2D(float Friction, float Restitution) : IPhysicsMaterial2D
|
public class PhysicsMaterial2D(float Friction, float Restitution) : IPhysicsMaterial2D
|
||||||
{
|
{
|
||||||
public readonly float Friction { get; init; } = Friction;
|
public float Friction { get; set; } = Friction;
|
||||||
public readonly float Restitution { get; init; } = Restitution;
|
public float Restitution { get; set; } = Restitution;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
namespace Engine.Physics2D;
|
|
||||||
|
|
||||||
public readonly struct PhysicsMaterial2DDefault : IPhysicsMaterial2D
|
|
||||||
{
|
|
||||||
public readonly float Friction => .1f;
|
|
||||||
|
|
||||||
public readonly float Restitution => .1f;
|
|
||||||
}
|
|
||||||
9
Engine.Physics2D/ReadOnlyPhysicsMaterial2D.cs
Normal file
9
Engine.Physics2D/ReadOnlyPhysicsMaterial2D.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Engine.Physics2D;
|
||||||
|
|
||||||
|
public readonly struct ReadOnlyPhysicsMaterial2D(float Friction, float Restitution) : IPhysicsMaterial2D
|
||||||
|
{
|
||||||
|
public readonly float Friction { get; } = Friction;
|
||||||
|
public readonly float Restitution { get; } = Restitution;
|
||||||
|
|
||||||
|
public readonly static ReadOnlyPhysicsMaterial2D Default = new(.1f, .1f);
|
||||||
|
}
|
||||||
@@ -1,16 +1,75 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using Engine.Core;
|
using Engine.Core;
|
||||||
|
|
||||||
namespace Engine.Physics2D;
|
namespace Engine.Physics2D;
|
||||||
|
|
||||||
public class RigidBody2D : Behaviour2D, IRigidBody2D
|
public class RigidBody2D : Behaviour2D, IRigidBody2D, IFirstFrameUpdate, ILastFrameUpdate
|
||||||
{
|
{
|
||||||
private const float LOWEST_ALLOWED_MASS = 0.00001f;
|
private const float LOWEST_ALLOWED_MASS = 0.00001f;
|
||||||
|
|
||||||
public IPhysicsMaterial2D Material { get; set; } = new PhysicsMaterial2DDefault();
|
public IPhysicsMaterial2D Material { get; set; } = ReadOnlyPhysicsMaterial2D.Default;
|
||||||
|
|
||||||
public Vector2D Velocity { get; set; } = Vector2D.Zero;
|
public Vector2D Velocity { get; set; } = Vector2D.Zero;
|
||||||
public float AngularVelocity { get; set; } = 0f;
|
public float AngularVelocity { get; set; } = 0f;
|
||||||
public bool IsStatic { get; set; } = false;
|
public bool IsStatic { get; set; } = false;
|
||||||
|
|
||||||
public float Mass { get; set => field = Math.Max(value, LOWEST_ALLOWED_MASS); } = 1f;
|
public float Mass { get; set { field = Math.Max(value, LOWEST_ALLOWED_MASS); } } = 1f;
|
||||||
|
public float InverseMass { get; protected set; } = 1f;
|
||||||
|
|
||||||
|
public float Inertia { get; protected set; } = 1f;
|
||||||
|
public float InverseInertia { get; protected set; } = 1f;
|
||||||
|
|
||||||
|
private readonly List<ICollider2D> childColliders = [];
|
||||||
|
|
||||||
|
public void LastActiveFrame() => DisconnectColliders();
|
||||||
|
public void FirstActiveFrame()
|
||||||
|
{
|
||||||
|
ReconnectColliders();
|
||||||
|
UpdateValues();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ReconnectColliders()
|
||||||
|
{
|
||||||
|
DisconnectColliders();
|
||||||
|
|
||||||
|
BehaviourController.GetBehavioursInChildren(childColliders);
|
||||||
|
|
||||||
|
foreach (ICollider2D collider in childColliders)
|
||||||
|
collider.OnRecalculated.AddListener(RecalculateCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DisconnectColliders()
|
||||||
|
{
|
||||||
|
foreach (ICollider2D collider in childColliders)
|
||||||
|
collider.OnRecalculated.RemoveListener(RecalculateCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RecalculateCallback(ICollider2D _) => UpdateValues();
|
||||||
|
private void UpdateValues()
|
||||||
|
{
|
||||||
|
InverseMass = Mass.OneOver();
|
||||||
|
|
||||||
|
Vector2D center = Transform.Position;
|
||||||
|
Inertia = 0f;
|
||||||
|
|
||||||
|
float totalColliderArea = 0f;
|
||||||
|
|
||||||
|
foreach (ICollider2D collider in childColliders)
|
||||||
|
totalColliderArea += collider.Area;
|
||||||
|
|
||||||
|
foreach (ICollider2D collider in childColliders)
|
||||||
|
{
|
||||||
|
float colliderMass = Mass * (collider.Area / totalColliderArea);
|
||||||
|
float colliderInertia = collider.GeometricInertia * colliderMass;
|
||||||
|
float distanceSquared = center.FromTo(collider.Transform.Position).MagnitudeSquared;
|
||||||
|
|
||||||
|
Inertia += colliderInertia + colliderMass * distanceSquared;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (childColliders.Count == 0)
|
||||||
|
Inertia = 1f;
|
||||||
|
|
||||||
|
InverseInertia = Inertia.OneOver();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace Engine.Systems.Tween;
|
|||||||
|
|
||||||
public class TweenManager : Behaviour, IEnterUniverse, IExitUniverse, ITweenManager
|
public class TweenManager : Behaviour, IEnterUniverse, IExitUniverse, ITweenManager
|
||||||
{
|
{
|
||||||
private CoroutineManager coroutineManager = null!;
|
private ICoroutineManager coroutineManager = null!;
|
||||||
|
|
||||||
private readonly Dictionary<ITween, IEnumerator> runningCoroutines = [];
|
private readonly Dictionary<ITween, IEnumerator> runningCoroutines = [];
|
||||||
private readonly Queue<Tween> pool = new();
|
private readonly Queue<Tween> pool = new();
|
||||||
@@ -75,7 +75,7 @@ public class TweenManager : Behaviour, IEnterUniverse, IExitUniverse, ITweenMana
|
|||||||
|
|
||||||
public void EnterUniverse(IUniverse universe)
|
public void EnterUniverse(IUniverse universe)
|
||||||
{
|
{
|
||||||
coroutineManager = universe.FindRequiredBehaviour<CoroutineManager>();
|
coroutineManager = universe.FindRequiredBehaviour<ICoroutineManager>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ExitUniverse(IUniverse universe)
|
public void ExitUniverse(IUniverse universe)
|
||||||
|
|||||||
Reference in New Issue
Block a user