Removed Old Collider

This commit is contained in:
Syntriax 2023-12-05 17:10:03 +03:00
parent 40a06fae4d
commit a417d2d753
2 changed files with 3 additions and 193 deletions

View File

@ -1,8 +1,11 @@
using System; using System;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using Pong.Behaviours; using Pong.Behaviours;
using Syntriax.Engine.Core; using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Graphics.TwoDimensional; using Syntriax.Engine.Graphics.TwoDimensional;
@ -61,7 +64,6 @@ public class Game1 : Game
gameObjectBall.Transform.Position = Vector2.Zero; gameObjectBall.Transform.Position = Vector2.Zero;
gameObjectBall.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f); gameObjectBall.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f);
engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>());
gameObjectBall.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]).OffsetScale = Vector2.One * 51.2f;
gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .1f), 500f); gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .1f), 500f);
gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall); gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
@ -72,7 +74,6 @@ public class Game1 : Game
gameObjectLeft.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>(); gameObjectLeft.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectLeft.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f); gameObjectLeft.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
gameObjectLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); gameObjectLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
gameObjectLeft.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject gameObjectRight = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectRight = gameManager.InstantiateGameObject<GameObject>();
@ -82,7 +83,6 @@ public class Game1 : Game
gameObjectRight.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>(); gameObjectRight.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectRight.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.Up, Keys.Down, 268f, -268f, 400f); gameObjectRight.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.Up, Keys.Down, 268f, -268f, 400f);
gameObjectRight.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); gameObjectRight.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
gameObjectRight.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
engine.AddRigidBody(gameObjectRight.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(gameObjectRight.BehaviourController.AddBehaviour<RigidBody2D>());
@ -90,33 +90,28 @@ public class Game1 : Game
goPlayAreaTop.Transform.Position = new Vector2(0f, 288f + 20f); goPlayAreaTop.Transform.Position = new Vector2(0f, 288f + 20f);
goPlayAreaTop.Transform.Scale = new Vector2(10240f, 40f); goPlayAreaTop.Transform.Scale = new Vector2(10240f, 40f);
// goPlayAreaTop.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); // goPlayAreaTop.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
goPlayAreaTop.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
engine.AddRigidBody(goPlayAreaTop.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(goPlayAreaTop.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject goPlayAreaBottom = gameManager.InstantiateGameObject<GameObject>(); IGameObject goPlayAreaBottom = gameManager.InstantiateGameObject<GameObject>();
goPlayAreaBottom.Transform.Position = new Vector2(0f, -(288f + 20f)); goPlayAreaBottom.Transform.Position = new Vector2(0f, -(288f + 20f));
goPlayAreaBottom.Transform.Scale = new Vector2(10240f, 40f); goPlayAreaBottom.Transform.Scale = new Vector2(10240f, 40f);
// goPlayAreaBottom.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); // goPlayAreaBottom.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
goPlayAreaBottom.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
engine.AddRigidBody(goPlayAreaBottom.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(goPlayAreaBottom.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject goPlayAreaRight = gameManager.InstantiateGameObject<GameObject>(); IGameObject goPlayAreaRight = gameManager.InstantiateGameObject<GameObject>();
goPlayAreaRight.Transform.Position = new Vector2(512f + 20f, 0f); goPlayAreaRight.Transform.Position = new Vector2(512f + 20f, 0f);
goPlayAreaRight.Transform.Scale = new Vector2(40f, 5760f); goPlayAreaRight.Transform.Scale = new Vector2(40f, 5760f);
// goPlayAreaRight.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); // goPlayAreaRight.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
goPlayAreaRight.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
engine.AddRigidBody(goPlayAreaRight.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(goPlayAreaRight.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject goPlayAreaLeft = gameManager.InstantiateGameObject<GameObject>(); IGameObject goPlayAreaLeft = gameManager.InstantiateGameObject<GameObject>();
goPlayAreaLeft.Transform.Position = new Vector2(-(512f + 20f), 0f); goPlayAreaLeft.Transform.Position = new Vector2(-(512f + 20f), 0f);
goPlayAreaLeft.Transform.Scale = new Vector2(40f, 5760f); goPlayAreaLeft.Transform.Scale = new Vector2(40f, 5760f);
// goPlayAreaLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); // goPlayAreaLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
goPlayAreaLeft.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
engine.AddRigidBody(goPlayAreaLeft.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(goPlayAreaLeft.BehaviourController.AddBehaviour<RigidBody2D>());
// IGameObject goPlayAreaCenter = gameManager.InstantiateGameObject<GameObject>(); // IGameObject goPlayAreaCenter = gameManager.InstantiateGameObject<GameObject>();
// goPlayAreaCenter.Transform.Position = new Vector2(100f, 100f); // goPlayAreaCenter.Transform.Position = new Vector2(100f, 100f);
// goPlayAreaCenter.Transform.Scale = new Vector2(40f, 40f); // goPlayAreaCenter.Transform.Scale = new Vector2(40f, 40f);
// // goPlayAreaCenter.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); // // goPlayAreaCenter.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
// goPlayAreaCenter.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
// engine.AddRigidBody(goPlayAreaCenter.BehaviourController.AddBehaviour<RigidBody2D>()); // engine.AddRigidBody(goPlayAreaCenter.BehaviourController.AddBehaviour<RigidBody2D>());
// TODO: use this.Content to load your game content here // TODO: use this.Content to load your game content here
} }

View File

@ -1,185 +0,0 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
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;
public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, ICollider2D
{
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, out CollisionInformation collisionInformation)
{
collisionInformation = new CollisionInformation(Vector2.Zero, Vector2.Zero);
foreach (var triangle in triangles)
{
if (!PhysicsMath.IsInTriangle(point, triangle))
continue;
OnCollision?.Invoke(this, otherCollider);
Line main = new(otherCollider.Transform.Position, point);
foreach (var line in PhysicsMath.GetLines([triangle.A, triangle.B, triangle.C]))
{
if (!PhysicsMath.DoIntersect(main, line))
continue;
Vector2 contactPoint = PhysicsMath.ClosestPointOnLine(point, line);
Vector2 normal = contactPoint - point;
if (normal.LengthSquared() < 0.001f)
normal = new Vector2(0f, 1f);
normal.Normalize();
collisionInformation = new CollisionInformation(normal, contactPoint);
break;
}
return true;
}
return false;
}
public void RecalculateVertices()
{
triangles.Clear();
_vertices.Clear();
foreach (var vertex in verticesOriginal)
{
Vector2 scaledPosition = new Vector2(vertex.X * Transform.Scale.X * OffsetScale.X, vertex.Y * Transform.Scale.Y * OffsetScale.Y);
_vertices.Add(scaledPosition + Transform.Position);
}
Triangle superTriangle = PhysicsMath.GetSuperTriangle(_vertices);
triangles.Add(superTriangle);
List<Triangle> badTriangles = new(32);
List<Line> polygon = new(32);
foreach (var vertex in _vertices)
{
badTriangles.Clear();
polygon.Clear();
foreach (var triangle in triangles)
{
Circle circle = PhysicsMath.GetCircumCircle(triangle);
if (Vector2.DistanceSquared(circle.Center, vertex) <= circle.Radius * circle.Radius)
badTriangles.Add(triangle);
}
foreach (var triangle in badTriangles)
foreach (var line in PhysicsMath.GetLines([triangle.A, triangle.B, triangle.C]))
if (PhysicsMath.DoesLineExistInVertices(line, [triangle.A, triangle.B, triangle.C]))
polygon.Add(line);
foreach (var triangle in badTriangles)
triangles.Remove(triangle);
foreach (var line in polygon)
triangles.Add(new(line.From, line.To, vertex));
}
for (int i = triangles.Count - 1; i >= 0; i--)
{
Triangle triangle = triangles[i];
if (
triangle.A == superTriangle.A || triangle.A == superTriangle.B || triangle.A == superTriangle.C ||
triangle.B == superTriangle.A || triangle.B == superTriangle.B || triangle.B == superTriangle.C ||
triangle.C == superTriangle.A || triangle.C == superTriangle.B || triangle.C == superTriangle.C
)
triangles.RemoveAt(i);
}
for (int i = gameObjects.Count - 1; i >= 0; i--)
{
IGameObject gameObject = gameObjects[i];
Game1.gameManager.RemoveGameObject(gameObject);
gameObjects.RemoveAt(i);
}
for (int i = 0; i < triangles.Count; i++)
{
Triangle triangle = triangles[i];
foreach (var line in PhysicsMath.GetLines([triangle.A, triangle.B, triangle.C]))
{
GameObject gameObject = Game1.gameManager.InstantiateGameObject<GameObject>();
DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>();
displayableSpriteBehaviour.Color = Color.Aqua;
displayableSpriteBehaviour.Origin = new(0.5f, 1f);
displayableSpriteBehaviour.Assign(Game1.spriteBox);
gameObject.Transform.Position = line.From;
Vector2 vector2 = line.To - line.From;
gameObject.Transform.Scale = new Vector2(2f, .0f) + Vector2.UnitY * vector2.Length();
gameObject.Transform.Rotation = (float)Math.Atan2(vector2.X, vector2.Y);
gameObjects.Add(gameObject);
gameObject = Game1.gameManager.InstantiateGameObject<GameObject>();
displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>();
displayableSpriteBehaviour.Color = Color.Crimson;
displayableSpriteBehaviour.Assign(Game1.spriteBox);
gameObject.Transform.Position = line.To;
gameObject.Transform.Scale = new Vector2(4f, 4f);
gameObjects.Add(gameObject);
}
}
foreach (var vertex in Vertices)
{
GameObject gameObject = Game1.gameManager.InstantiateGameObject<GameObject>();
DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>();
displayableSpriteBehaviour.Color = Color.GreenYellow;
displayableSpriteBehaviour.Assign(Game1.spriteBox);
gameObject.Transform.Position = vertex;
gameObject.Transform.Scale = new Vector2(2f, 2f);
gameObjects.Add(gameObject);
}
}
private List<IGameObject> gameObjects = new List<IGameObject>(32);
}