Engine-Pong/Game/Physics2D/PhysicsEngine2D.cs

115 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Abstract;
namespace Syntriax.Engine.Physics2D;
public class PhysicsEngine2D : IPhysicsEngine2D
{
private List<IRigidBody2D> rigidBodies = new List<IRigidBody2D>(32);
private List<ICollider2D> colliders = new List<ICollider2D>(64);
private int _iterationCount = 1;
public int IterationCount { get => _iterationCount; set => _iterationCount = value < 1 ? 1 : value; }
public void AddRigidBody(IRigidBody2D rigidBody)
{
if (rigidBodies.Contains(rigidBody))
return;
rigidBodies.Add(rigidBody);
foreach (var collider2D in rigidBody.BehaviourController.GetBehaviours<ICollider2D>())
colliders.Add(collider2D);
rigidBody.BehaviourController.OnBehaviourAdded += OnBehaviourAdded;
rigidBody.BehaviourController.OnBehaviourRemoved += OnBehaviourRemoved;
}
public void RemoveRigidBody(IRigidBody2D rigidBody)
{
rigidBodies.Remove(rigidBody);
}
// List<IGameObject> gameObjects = new List<IGameObject>();
public void Step(float deltaTime)
{
float intervalDeltaTime = deltaTime / IterationCount;
for (int iterationIndex = 0; iterationIndex < IterationCount; iterationIndex++)
{
foreach (var rigidBody in rigidBodies)
StepRigidBody(rigidBody, intervalDeltaTime);
foreach (var collider in colliders)
collider.Recalculate();
// foreach (var gameObject in gameObjects)
// Game1.gameManager.RemoveGameObject(gameObject);
// gameObjects.Clear();
// foreach (var collider in colliders)
// foreach (var vertex in collider.Vertices)
// {
// GameObject gameObject = Game1.gameManager.InstantiateGameObject<GameObject>();
// gameObject.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(Game1.spriteBox);
// gameObject.Transform.Position = vertex;
// gameObjects.Add(gameObject);
// }
for (int i = colliders.Count - 1; i >= 0; i--)
CheckCollisions(colliders[i], colliders, (c1, c2) =>
{
if (c1.RigidBody2D is IRigidBody2D c1RigidBody) { c1RigidBody.Velocity = -c1RigidBody.Velocity; StepRigidBody(c1RigidBody, intervalDeltaTime); c1.Recalculate(); }
if (c2.RigidBody2D is IRigidBody2D c2RigidBody) { c2RigidBody.Velocity = -c2RigidBody.Velocity; StepRigidBody(c2RigidBody, intervalDeltaTime); c2.Recalculate(); }
});
}
}
private void CheckCollisions(ICollider2D collider2D, List<ICollider2D> collider2Ds, Action<ICollider2D, ICollider2D> OnCollisionDetectedAction)
{
for (int i = 0; i < collider2Ds.Count; i++)
{
ICollider2D collider2DItem = collider2Ds[i];
if (collider2DItem == collider2D)
continue;
for (int y = 0; y < collider2DItem.Vertices.Count; y++)
if (collider2D.CheckCollision(collider2DItem.Vertices[y]))
OnCollisionDetectedAction?.Invoke(collider2D, collider2DItem);
}
}
private void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)
{
Vector2 nextPosition = rigidBody.Transform.Position;
nextPosition += rigidBody.Velocity * intervalDeltaTime;
rigidBody.Transform.Position = nextPosition;
}
private void OnBehaviourAdded(IBehaviourController controller, IBehaviour behaviour)
{
if (behaviour is not ICollider2D collider2D)
return;
colliders.Add(collider2D);
}
private void OnBehaviourRemoved(IBehaviourController controller, IBehaviour behaviour)
{
if (behaviour is not ICollider2D collider2D)
return;
colliders.Remove(collider2D);
}
}