This commit is contained in:
Syntriax 2023-12-06 13:50:39 +03:00
parent fa7d92ce88
commit 6ceea7d84e
3 changed files with 14 additions and 8 deletions

View File

@ -64,8 +64,9 @@ 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<MovementBallBehaviour>(new Vector2(.1f, .1f), 500f); gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .01f), 500f);
gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall); gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
gameObjectBall.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One * 512f, Vector2.One * 512f);
IGameObject gameObjectLeft = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectLeft = gameManager.InstantiateGameObject<GameObject>();
gameObjectLeft.Name = "Left"; gameObjectLeft.Name = "Left";
@ -74,6 +75,7 @@ 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<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One * .5f, Vector2.One * .5f);
engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject gameObjectRight = gameManager.InstantiateGameObject<GameObject>(); IGameObject gameObjectRight = gameManager.InstantiateGameObject<GameObject>();
@ -83,6 +85,7 @@ 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<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One * .5f, Vector2.One * .5f);
engine.AddRigidBody(gameObjectRight.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(gameObjectRight.BehaviourController.AddBehaviour<RigidBody2D>());
@ -90,26 +93,26 @@ 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<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One, Vector2.One); goPlayAreaTop.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One * .5f, Vector2.One * .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<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One, Vector2.One); goPlayAreaBottom.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One * .5f, Vector2.One * .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<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One, Vector2.One); goPlayAreaRight.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One * .5f, Vector2.One * .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<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One, Vector2.One); goPlayAreaLeft.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One * .5f, Vector2.One * .5f);
engine.AddRigidBody(goPlayAreaLeft.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(goPlayAreaLeft.BehaviourController.AddBehaviour<RigidBody2D>());
// IGameObject goPlayAreaCenter = gameManager.InstantiateGameObject<GameObject>(); // IGameObject goPlayAreaCenter = gameManager.InstantiateGameObject<GameObject>();
@ -152,7 +155,7 @@ public class Game1 : Game
if (Keyboard.GetState().IsKeyDown(Keys.E)) if (Keyboard.GetState().IsKeyDown(Keys.E))
gameManager.Camera.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f; gameManager.Camera.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
Console.WriteLine(gameObjectBall.Transform.Position);
// if (Keyboard.GetState().IsKeyDown(Keys.N)) // if (Keyboard.GetState().IsKeyDown(Keys.N))
// seconds = 39.12f; // seconds = 39.12f;
// if (Keyboard.GetState().IsKeyDown(Keys.Space)) // if (Keyboard.GetState().IsKeyDown(Keys.Space))

View File

@ -45,8 +45,8 @@ public class Collider2DAABBBehaviour : BehaviourOverride, ICollider2D
public void Recalculate() public void Recalculate()
{ {
AABBWorld = new AABB( AABBWorld = new AABB(
AABBLocal.LowerBoundary + Transform.Position, AABBLocal.LowerBoundary.Scale(Transform.Scale) + Transform.Position,
AABBLocal.UpperBoundary + Transform.Position AABBLocal.UpperBoundary.Scale(Transform.Scale) + Transform.Position
); );
vertices.Clear(); vertices.Clear();

View File

@ -14,6 +14,9 @@ public record AABB(Vector2 LowerBoundary, Vector2 UpperBoundary);
public static class PhysicsMath public static class PhysicsMath
{ {
public static Vector2 Scale(this Vector2 original, Vector2 scale)
=> new Vector2(original.X * scale.X, original.Y * scale.Y);
public static Vector2 ClosestPointTo(this Line line, Vector2 point) public static Vector2 ClosestPointTo(this Line line, Vector2 point)
{ {
// Convert edge points to vectors // Convert edge points to vectors