This commit is contained in:
Syntriax 2023-12-04 13:14:23 +03:00
parent 6dbd2b1407
commit 682bd62ffc
5 changed files with 55 additions and 11 deletions

View File

@ -69,15 +69,23 @@ public class Game1 : Game
gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
IGameObject gameObjectBall2 = gameManager.InstantiateGameObject<GameObject>();
gameObjectBall2.Name = "Ball2";
gameObjectBall2.Transform.Position = Vector2.UnitY * -40;
gameObjectBall2.Transform.Position = Vector2.UnitY * -50f;
gameObjectBall2.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f);
RigidBody2D rigidBody = gameObjectBall2.BehaviourController.AddBehaviour<RigidBody2D>();
rigidBody.Velocity = Vector2.UnitY * 10f;
rigidBody.Velocity = Vector2.UnitY * 100f;
engine.AddRigidBody(rigidBody);
gameObjectBall2.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(1, 1) * 5f, new Vector2(-1, 1) * 5f, new Vector2(1, -1) * 5f, new Vector2(-1, -1) * 5f]);
// gameObjectBall2.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .1f), playAreaBehaviour, 100f);
gameObjectBall2.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
IGameObject gameObjectBall3 = gameManager.InstantiateGameObject<GameObject>();
gameObjectBall3.Name = "Ball";
gameObjectBall3.Transform.Position = Vector2.UnitY * -120f + Vector2.UnitX * 10f;
gameObjectBall3.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f);
engine.AddRigidBody(gameObjectBall3.BehaviourController.AddBehaviour<RigidBody2D>());
gameObjectBall3.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(1, 1) * 5f, new Vector2(-1, 1) * 5f, new Vector2(1, -1) * 5f, new Vector2(-1, -1) * 5f]);
// gameObjectBall3.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .1f), playAreaBehaviour, 100f);
gameObjectBall3.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
// IGameObject gameObjectLeft = gameManager.InstantiateGameObject<GameObject>();
// gameObjectLeft.Name = "Left";
// gameObjectLeft.Transform.Position = new Vector2(-452, 0f);

View File

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core.Abstract;
@ -9,6 +9,8 @@ namespace Syntriax.Engine.Physics2D.Abstract;
public interface ICollider2D : IBehaviour, IAssignableTransform
{
IRigidBody2D? RigidBody2D { get; }
Action<ICollider2D, ICollider2D>? OnCollision { get; set; }
Vector2 OffsetPosition { get; set; }
@ -17,7 +19,7 @@ public interface ICollider2D : IBehaviour, IAssignableTransform
IReadOnlyList<Vector2> Vertices { get; }
bool CheckCollision(Vector2 point, ICollider2D otherCollider);
bool CheckCollision(Vector2 point, ICollider2D otherCollider, out Vector2 normal);
void RecalculateVertices();
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core;
@ -13,26 +14,46 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
{
private List<Triangle> triangles = new List<Triangle>(32);
private readonly List<Vector2> _vertices = new List<Vector2>(32);
private IRigidBody2D? _rigidBody2D = null;
public Action<IAssignableTransform>? OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; }
public Action<ICollider2D, ICollider2D>? OnCollision { get; set; } = null;
private IList<Vector2> verticesOriginal { get; } = vertices;
public Vector2 OffsetPosition { get; set; } = Vector2.Zero;
public Vector2 OffsetScale { get; set; } = Vector2.One;
public float OffsetRotation { get; set; } = 0f;
ITransform IAssignableTransform.Transform => Transform;
public IReadOnlyList<Vector2> Vertices => _vertices;
public IRigidBody2D? RigidBody2D
{
get
{
if (_rigidBody2D is null)
BehaviourController.TryGetBehaviour(out _rigidBody2D);
return _rigidBody2D;
}
}
public bool Assign(ITransform transform) => GameObject.Assign(transform);
public bool CheckCollision(Vector2 point, ICollider2D otherCollider)
public bool CheckCollision(Vector2 point, ICollider2D otherCollider, out Vector2 normal)
{
normal = Vector2.Zero;
foreach (var triangle in triangles)
{
if (!isInside(point, triangle))
continue;
OnCollision?.Invoke(this, otherCollider);
normal = Transform.Position - otherCollider.Transform.Position;
return true;
}
@ -44,7 +65,7 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
triangles.Clear();
_vertices.Clear();
foreach (var vertex in VerticesOriginal)
foreach (var vertex in verticesOriginal)
_vertices.Add(vertex + Transform.Position);
Triangle superTriangle = GetSuperTriangle(_vertices);
@ -227,7 +248,4 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
return result;
}
public IList<Vector2> VerticesOriginal { get; } = vertices;
public IReadOnlyList<Vector2> Vertices => _vertices;
}

View File

@ -0,0 +1,12 @@
using Microsoft.Xna.Framework;
using Syntriax.Engine.Physics2D.Abstract;
namespace Syntriax.Engine.Physics2D;
public record CollisionInformation
(
Vector2 Normal,
float Impulse,
IRigidBody2D? RigidBodyX,
IRigidBody2D? RigidBodyY
);

View File

@ -59,10 +59,14 @@ public class PhysicsEngine2D : IPhysicsEngine2D
for (int colliderIY = colliderIX + 1; colliderIY < colliders.Count; colliderIY++)
foreach (var vertex in colliderX.Vertices)
{
if (!colliders[colliderIY].CheckCollision(vertex, colliderX))
if (!colliders[colliderIY].CheckCollision(vertex, colliderX, out var collisionInformation))
continue;
Console.WriteLine($"Collision");
// Console.WriteLine($"Collision");
// if (colliders[colliderIX].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX))
// rigidX.Velocity = -rigidX.Velocity;
// if (colliders[colliderIY].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidY))
// rigidY.Velocity = -rigidY.Velocity;
break;
}
}