Tetstests
This commit is contained in:
parent
1e8cc82dc4
commit
b8706e82d8
|
@ -112,6 +112,12 @@ public class Game1 : Game
|
||||||
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)]);
|
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>();
|
||||||
|
// goPlayAreaCenter.Transform.Position = new Vector2(100f, 100f);
|
||||||
|
// goPlayAreaCenter.Transform.Scale = new Vector2(40f, 40f);
|
||||||
|
// // 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>());
|
||||||
// TODO: use this.Content to load your game content here
|
// TODO: use this.Content to load your game content here
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,8 +156,17 @@ public class Game1 : Game
|
||||||
|
|
||||||
if (Keyboard.GetState().IsKeyDown(Keys.Space))
|
if (Keyboard.GetState().IsKeyDown(Keys.Space))
|
||||||
seconds += gameTime.ElapsedGameTime.Milliseconds * .0005f;
|
seconds += gameTime.ElapsedGameTime.Milliseconds * .0005f;
|
||||||
|
if (Keyboard.GetState().IsKeyDown(Keys.B))
|
||||||
|
{
|
||||||
|
seconds -= gameTime.ElapsedGameTime.Milliseconds * .0005f;
|
||||||
|
while (physicsTimer - 0.01f > seconds)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Physics Timer: {physicsTimer}");
|
||||||
|
physicsTimer -= 0.01f;
|
||||||
|
engine.Step(-.01f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Add your update logic here
|
|
||||||
while (physicsTimer + 0.01f < seconds)
|
while (physicsTimer + 0.01f < seconds)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Physics Timer: {physicsTimer}");
|
Console.WriteLine($"Physics Timer: {physicsTimer}");
|
||||||
|
|
|
@ -64,15 +64,12 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
|
||||||
Vector2 contactPoint = ClosestPointOnEdge(point, edge);
|
Vector2 contactPoint = ClosestPointOnEdge(point, edge);
|
||||||
|
|
||||||
Vector2 normal = contactPoint - point;
|
Vector2 normal = contactPoint - point;
|
||||||
|
if (normal.LengthSquared() < 0.001f)
|
||||||
|
normal = new Vector2(0f, 1f);
|
||||||
|
|
||||||
normal.Normalize();
|
normal.Normalize();
|
||||||
|
|
||||||
collisionInformation = new CollisionInformation(normal, contactPoint);
|
collisionInformation = new CollisionInformation(normal, contactPoint);
|
||||||
|
|
||||||
GameObject gameObject = Game1.gameManager.InstantiateGameObject<GameObject>();
|
|
||||||
gameObject.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(Game1.spriteBox);
|
|
||||||
gameObject.Transform.Position = point;
|
|
||||||
gameObject.Transform.Scale = new Vector2(1f, .01f) * 100f;
|
|
||||||
gameObject.Transform.Rotation = (float)Math.Atan2(normal.Y, normal.X);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -142,18 +139,54 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
|
||||||
triangles.RemoveAt(i);
|
triangles.RemoveAt(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
// for (int i = 0; i < triangles.Count; i++)
|
for (int i = gameObjects.Count - 1; i >= 0; i--)
|
||||||
// {
|
{
|
||||||
// Triangle triangle = triangles[i];
|
IGameObject gameObject = gameObjects[i];
|
||||||
|
Game1.gameManager.RemoveGameObject(gameObject);
|
||||||
|
gameObjects.RemoveAt(i);
|
||||||
|
}
|
||||||
|
|
||||||
// triangle.A += Transform.Position;
|
|
||||||
// triangle.B += Transform.Position;
|
|
||||||
// triangle.C += Transform.Position;
|
|
||||||
|
|
||||||
// triangles[i] = triangle;
|
for (int i = 0; i < triangles.Count; i++)
|
||||||
// }
|
{
|
||||||
|
Triangle triangle = triangles[i];
|
||||||
|
foreach (var edge in GetEdges(triangle))
|
||||||
|
{
|
||||||
|
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 = edge.A;
|
||||||
|
Vector2 vector2 = edge.B - edge.A;
|
||||||
|
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 = edge.B;
|
||||||
|
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);
|
||||||
|
|
||||||
private Vector2 ClosestPointOnEdge(Vector2 point, Edge edge)
|
private Vector2 ClosestPointOnEdge(Vector2 point, Edge edge)
|
||||||
{
|
{
|
||||||
// Convert edge points to vectors
|
// Convert edge points to vectors
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
|
using Pong;
|
||||||
|
using Syntriax.Engine.Core;
|
||||||
using Syntriax.Engine.Core.Abstract;
|
using Syntriax.Engine.Core.Abstract;
|
||||||
|
using Syntriax.Engine.Graphics.TwoDimensional;
|
||||||
using Syntriax.Engine.Physics2D.Abstract;
|
using Syntriax.Engine.Physics2D.Abstract;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Physics2D;
|
namespace Syntriax.Engine.Physics2D;
|
||||||
|
@ -54,10 +57,10 @@ public class PhysicsEngine2D : IPhysicsEngine2D
|
||||||
for (int colliderIY = colliderIX + 1; colliderIY < colliders.Count; colliderIY++)
|
for (int colliderIY = colliderIX + 1; colliderIY < colliders.Count; colliderIY++)
|
||||||
for (int verticesIndex = 0; verticesIndex < colliderX.Vertices.Count; verticesIndex++)
|
for (int verticesIndex = 0; verticesIndex < colliderX.Vertices.Count; verticesIndex++)
|
||||||
{
|
{
|
||||||
if (!colliders[colliderIY].CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var collisionInformation))
|
ICollider2D colliderY = colliders[colliderIY];
|
||||||
|
if (!colliderY.CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var collisionInformation))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
ICollider2D colliderY = colliders[colliderIY];
|
|
||||||
if (colliderX.BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX))
|
if (colliderX.BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX))
|
||||||
{
|
{
|
||||||
Vector2 xVertex = colliderX.Vertices[verticesIndex];
|
Vector2 xVertex = colliderX.Vertices[verticesIndex];
|
||||||
|
@ -68,10 +71,45 @@ public class PhysicsEngine2D : IPhysicsEngine2D
|
||||||
rigidX.Velocity = Vector2.Reflect(rigidX.Velocity, collisionInformation.Normal);
|
rigidX.Velocity = Vector2.Reflect(rigidX.Velocity, collisionInformation.Normal);
|
||||||
rigidX.Transform.Position -= xVertex - vertexNewPosition;
|
rigidX.Transform.Position -= xVertex - vertexNewPosition;
|
||||||
|
|
||||||
|
// {
|
||||||
|
// GameObject gameObject = Game1.gameManager.InstantiateGameObject<GameObject>();
|
||||||
|
// DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>();
|
||||||
|
// displayableSpriteBehaviour.Assign(Game1.spriteBox);
|
||||||
|
// gameObject.Transform.Position = collisionInformation.ContactPosition;
|
||||||
|
// gameObject.Transform.Scale = new Vector2(1f, .01f) * 100f;
|
||||||
|
// gameObject.Transform.Rotation = (float)Math.Atan2(collisionInformation.Normal.Y, collisionInformation.Normal.X);
|
||||||
|
// }
|
||||||
|
// {
|
||||||
|
// GameObject gameObject = Game1.gameManager.InstantiateGameObject<GameObject>();
|
||||||
|
// DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>();
|
||||||
|
// displayableSpriteBehaviour.Color = Color.Aqua;
|
||||||
|
// displayableSpriteBehaviour.Assign(Game1.spriteBox);
|
||||||
|
// gameObject.Transform.Position = collisionInformation.ContactPosition;
|
||||||
|
// gameObject.Transform.Scale = new Vector2(1f, .01f) * 100f;
|
||||||
|
// gameObject.Transform.Rotation = (float)Math.Atan2(rigidX.Velocity.Y, rigidX.Velocity.X);
|
||||||
|
// }
|
||||||
|
// {
|
||||||
|
// GameObject gameObject = Game1.gameManager.InstantiateGameObject<GameObject>();
|
||||||
|
// DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>();
|
||||||
|
// displayableSpriteBehaviour.Color = Color.Pink;
|
||||||
|
// displayableSpriteBehaviour.Assign(Game1.spriteBox);
|
||||||
|
// gameObject.Transform.Position = collisionInformation.ContactPosition;
|
||||||
|
// gameObject.Transform.Scale = new Vector2(1f, .01f) * 100f;
|
||||||
|
// gameObject.Transform.Rotation = (float)Math.Atan2(edgeDirection.Y, edgeDirection.X);
|
||||||
|
// }
|
||||||
|
// {
|
||||||
|
// GameObject gameObject = Game1.gameManager.InstantiateGameObject<GameObject>();
|
||||||
|
// DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>();
|
||||||
|
// displayableSpriteBehaviour.Color = Color.DarkGoldenrod;
|
||||||
|
// displayableSpriteBehaviour.Assign(Game1.spriteBox);
|
||||||
|
// gameObject.Transform.Position = collisionInformation.ContactPosition;
|
||||||
|
// gameObject.Transform.Scale = new Vector2(1f, .01f) * 100f;
|
||||||
|
// gameObject.Transform.Rotation = (float)Math.Atan2((xVertex - p1).Y, (xVertex - p1).X);
|
||||||
|
// }
|
||||||
|
|
||||||
StepRigidBody(rigidX, intervalDeltaTime * (1f - t));
|
StepRigidBody(rigidX, intervalDeltaTime * (1f - t));
|
||||||
colliders[colliderIX].RecalculateVertices();
|
colliders[colliderIX].RecalculateVertices();
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine($"/////////////////////////////////////////////");
|
Console.WriteLine($"/////////////////////////////////////////////");
|
||||||
// Console.WriteLine($"Collision");
|
// Console.WriteLine($"Collision");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue