This commit is contained in:
Syntriax 2023-12-06 14:14:55 +03:00
parent 6ceea7d84e
commit a7b1c7cb4c
3 changed files with 23 additions and 12 deletions

View File

@ -66,7 +66,7 @@ public class Game1 : Game
engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>());
gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .01f), 500f);
gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
gameObjectBall.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One * 512f, Vector2.One * 512f);
gameObjectBall.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One * 512f * .5f, Vector2.One * 512f * .5f);
IGameObject gameObjectLeft = gameManager.InstantiateGameObject<GameObject>();
gameObjectLeft.Name = "Left";

View File

@ -51,9 +51,9 @@ public class Collider2DAABBBehaviour : BehaviourOverride, ICollider2D
vertices.Clear();
vertices.Add(AABBWorld.LowerBoundary);
vertices.Add(new(AABBWorld.LowerBoundary.X, AABBWorld.UpperBoundary.Y));
vertices.Add(new Vector2(AABBWorld.LowerBoundary.X, AABBWorld.UpperBoundary.Y));
vertices.Add(AABBWorld.UpperBoundary);
vertices.Add(new(AABBWorld.LowerBoundary.Y, AABBWorld.UpperBoundary.X));
vertices.Add(new Vector2(AABBWorld.UpperBoundary.X, AABBWorld.LowerBoundary.Y));
}
public Collider2DAABBBehaviour(Vector2 lowerBoundary, Vector2 upperBoundary)

View File

@ -3,11 +3,7 @@ using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Pong;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Graphics.TwoDimensional;
using Syntriax.Engine.Physics2D.Abstract;
namespace Syntriax.Engine.Physics2D;
@ -41,6 +37,8 @@ public class PhysicsEngine2D : IPhysicsEngine2D
rigidBodies.Remove(rigidBody);
}
// List<IGameObject> gameObjects = new List<IGameObject>();
public void Step(float deltaTime)
{
float intervalDeltaTime = deltaTime / IterationCount;
@ -54,25 +52,38 @@ public class PhysicsEngine2D : IPhysicsEngine2D
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;
if (c2.RigidBody2D is IRigidBody2D c2RigidBody) c2RigidBody.Velocity = -c2RigidBody.Velocity;
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 = collider2Ds.Count - 1; i >= 0; i--)
for (int i = 0; i < collider2Ds.Count; i++)
{
ICollider2D collider2DItem = collider2Ds[i];
if (collider2DItem == collider2D)
continue;
foreach (var vertex in collider2DItem.Vertices)
if (collider2D.CheckCollision(vertex))
for (int y = 0; y < collider2DItem.Vertices.Count; y++)
if (collider2D.CheckCollision(collider2DItem.Vertices[y]))
OnCollisionDetectedAction?.Invoke(collider2D, collider2DItem);
}
}