add collision system

main
Evan Hemsley 2020-07-13 11:49:20 -07:00
parent 684b1f83fc
commit 83754fbe44
20 changed files with 404 additions and 20 deletions

View File

@ -0,0 +1,6 @@
using Encompass;
namespace PongFE.Components
{
public struct BounceResponseComponent : IComponent { }
}

View File

@ -0,0 +1,6 @@
using Encompass;
namespace PongFE.Components
{
public struct CanBeBouncedComponent : IComponent { }
}

View File

@ -0,0 +1,6 @@
using Encompass;
namespace PongFE.Components
{
public struct CanCauseBounceComponent : IComponent { }
}

View File

@ -0,0 +1,15 @@
using Encompass;
using MoonTools.Bonk;
namespace PongFE.Components
{
public struct CollisionComponent : IComponent
{
public Rectangle Rectangle { get; }
public CollisionComponent(Rectangle rectangle)
{
Rectangle = rectangle;
}
}
}

View File

@ -0,0 +1,15 @@
using System.Numerics;
using Encompass;
namespace PongFE.Components
{
public struct VelocityComponent : IComponent
{
public Vector2 Velocity { get; }
public VelocityComponent(Vector2 velocity)
{
Velocity = velocity;
}
}
}

View File

@ -0,0 +1,42 @@
using System.Numerics;
using Encompass;
using PongFE.Components;
using PongFE.Messages;
namespace PongFE.Engines
{
[Reads(
typeof(BounceResponseComponent),
typeof(VelocityComponent)
)]
[Receives(typeof(BounceMessage))]
[Sends(typeof(UpdateVelocityMessage))]
public class BounceEngine : Engine
{
public override void Update(double dt)
{
foreach (ref readonly var message in ReadMessages<BounceMessage>())
{
if (HasComponent<BounceResponseComponent>(message.Entity) && HasComponent<VelocityComponent>(message.Entity))
{
System.Console.WriteLine("hello bounce!");
ref readonly var velocityComponent = ref GetComponent<VelocityComponent>(message.Entity);
Vector2 newVelocity;
if (message.HitOrientation == HitOrientation.Horizontal)
{
newVelocity =
new Vector2(-velocityComponent.Velocity.X, velocityComponent.Velocity.Y);
}
else
{
newVelocity =
new Vector2(velocityComponent.Velocity.X, -velocityComponent.Velocity.Y);
}
SendMessage(new UpdateVelocityMessage(message.Entity, newVelocity));
}
}
}
}
}

View File

@ -0,0 +1,36 @@
using Encompass;
using PongFE.Components;
using PongFE.Messages;
namespace PongFE.Engines
{
[Reads(
typeof(CanCauseBounceComponent),
typeof(CanBeBouncedComponent)
)]
[Receives(typeof(CollisionMessage))]
[Sends(typeof(BounceMessage))]
public class CollisionEngine : Engine
{
public override void Update(double dt)
{
foreach (ref readonly var message in ReadMessages<CollisionMessage>())
{
CheckBounce(message.EntityA, message.EntityB, message.HitOrientation);
CheckBounce(message.EntityB, message.EntityA, message.HitOrientation);
}
}
private void CheckBounce(Entity a, Entity b, HitOrientation hitOrientation)
{
if (HasComponent<CanCauseBounceComponent>(a))
{
if (HasComponent<CanBeBouncedComponent>(b))
{
System.Console.WriteLine("bounce");
SendMessage(new BounceMessage(b, hitOrientation));
}
}
}
}
}

View File

@ -1,25 +1,125 @@
using System.Collections.Generic;
using System.Numerics;
using Encompass;
using MoonTools.Bonk;
using MoonTools.Structs;
using PongFE.Components;
using PongFE.Messages;
namespace PongFE.Engines
{
[Reads(typeof(PositionComponent))]
[Reads(
typeof(PositionComponent),
typeof(CollisionComponent)
)]
[Receives(typeof(MotionMessage))]
[Writes(typeof(PositionComponent))]
[Sends(
typeof(UpdatePositionMessage),
typeof(CollisionMessage)
)]
public class MotionEngine : Engine
{
private readonly SpatialHash<Entity> _spatialHash = new SpatialHash<Entity>(32);
private readonly Dictionary<Entity, Vector2> _moveAmounts = new Dictionary<Entity, Vector2>();
private readonly Dictionary<Entity, Position2D> _finalPositions = new Dictionary<Entity, Position2D>();
public override void Update(double dt)
{
foreach (ref readonly var motionMessage in ReadMessages<MotionMessage>())
_spatialHash.Clear();
_moveAmounts.Clear();
_finalPositions.Clear();
foreach (ref readonly var entity in ReadEntities<CollisionComponent>())
{
if (HasComponent<PositionComponent>(motionMessage.Entity))
ref readonly var collisionComponent = ref GetComponent<CollisionComponent>(entity);
if (HasComponent<PositionComponent>(entity))
{
ref readonly var positionComponent = ref GetComponent<PositionComponent>(motionMessage.Entity);
var newPosition = positionComponent.Position + motionMessage.Movement;
SetComponent(motionMessage.Entity, new PositionComponent(newPosition));
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
_spatialHash.Insert(entity, collisionComponent.Rectangle, new Transform2D(positionComponent.Position));
}
}
foreach (ref readonly var entity in ReadEntities<PositionComponent>())
{
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
_finalPositions[entity] = positionComponent.Position;
_moveAmounts[entity] = Vector2.Zero;
foreach (var motionMessage in ReadMessagesWithEntity<MotionMessage>(entity))
{
_moveAmounts[entity] += motionMessage.Movement;
}
}
foreach (var pair in _moveAmounts)
{
var entity = pair.Key;
var moveAmount = pair.Value;
ref readonly var positionComponent = ref GetComponent<PositionComponent>(entity);
var projectedPosition = positionComponent.Position + moveAmount;
if (!HasComponent<CollisionComponent>(entity))
{
SendMessage(new UpdatePositionMessage(entity, projectedPosition));
}
else
{
ref readonly var collisionComponent = ref GetComponent<CollisionComponent>(entity);
var rectangle = collisionComponent.Rectangle;
var (xHit, yHit, newPosition, collisionEntity) = SolidCollisionPosition(rectangle, positionComponent.Position, projectedPosition);
if (xHit || yHit)
{
projectedPosition = newPosition;
if (xHit)
{
SendMessage(new CollisionMessage(entity, collisionEntity, HitOrientation.Horizontal));
}
else
{
SendMessage(new CollisionMessage(entity, collisionEntity, HitOrientation.Vertical));
}
}
}
SendMessage(new UpdatePositionMessage(entity, projectedPosition));
}
}
private (bool, bool, Position2D, Entity) SolidCollisionPosition(Rectangle rectangle, Position2D startPosition, Position2D endPosition)
{
var startX = startPosition.X;
var endX = endPosition.X;
var startY = startPosition.Y;
var endY = endPosition.Y;
bool xHit, yHit;
int xPosition, yPosition;
Entity xCollisionEntity, yCollisionEntity;
(xHit, xPosition, xCollisionEntity) = SweepX(_spatialHash, rectangle, Position2D.Zero, new Position2D(startX, startY), endX - startX);
if (!xHit) { xPosition = endX; }
(yHit, yPosition, yCollisionEntity) = SweepY(_spatialHash, rectangle, Position2D.Zero, new Position2D(xPosition, startY), endY - startY);
return (xHit, yHit, new Position2D(xPosition, yPosition), xHit ? xCollisionEntity : yCollisionEntity);
}
private (bool, int, Entity) SweepX(SpatialHash<Entity> solidSpatialHash, Rectangle rectangle, Position2D offset, Position2D startPosition, int horizontalMovement)
{
var sweepResult = SweepTest.Test(solidSpatialHash, rectangle, new Transform2D(offset + startPosition), new Vector2(horizontalMovement, 0));
return (sweepResult.Hit, startPosition.X + (int)sweepResult.Motion.X, sweepResult.ID);
}
public static (bool, int, Entity) SweepY(SpatialHash<Entity> solidSpatialHash, Rectangle rectangle, Position2D offset, Position2D startPosition, int verticalMovement)
{
var sweepResult = SweepTest.Test(solidSpatialHash, rectangle, new Transform2D(offset + startPosition), new Vector2(0, verticalMovement));
return (sweepResult.Hit, startPosition.Y + (int)sweepResult.Motion.Y, sweepResult.ID);
}
}
}

View File

@ -17,8 +17,12 @@ namespace PongFE.Spawners
protected override void Spawn(BallSpawnMessage message)
{
var ball = CreateEntity();
AddComponent(ball, new PositionComponent(new MoonTools.Structs.Position2D(640, 360)));
AddComponent(ball, new PositionComponent(message.Position));
AddComponent(ball, new VelocityComponent(message.Velocity));
AddComponent(ball, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, 16, 16)));
AddComponent(ball, new Texture2DComponent(BallTexture, 0));
AddComponent(ball, new CanBeBouncedComponent());
AddComponent(ball, new BounceResponseComponent());
}
}
}

View File

@ -0,0 +1,19 @@
using Encompass;
using PongFE.Components;
using PongFE.Messages;
namespace PongFE.Engines
{
[Receives(typeof(UpdatePositionMessage))]
[Writes(typeof(PositionComponent))]
public class UpdatePositionEngine : Engine
{
public override void Update(double dt)
{
foreach (ref readonly var message in ReadMessages<UpdatePositionMessage>())
{
SetComponent(message.Entity, new PositionComponent(message.Position));
}
}
}
}

View File

@ -0,0 +1,19 @@
using Encompass;
using PongFE.Components;
using PongFE.Messages;
namespace PongFE.Engines
{
[Receives(typeof(UpdateVelocityMessage))]
[Writes(typeof(VelocityComponent))]
public class UpdateVelocityEngine : Engine
{
public override void Update(double dt)
{
foreach (ref readonly var message in ReadMessages<UpdateVelocityMessage>())
{
SetComponent(message.Entity, new VelocityComponent(message.Velocity));
}
}
}
}

View File

@ -0,0 +1,20 @@
using Encompass;
using PongFE.Components;
using PongFE.Messages;
namespace PongFE.Engines
{
[Sends(typeof(MotionMessage))]
[QueryWith(typeof(PositionComponent), typeof(VelocityComponent))]
public class VelocityEngine : Engine
{
public override void Update(double dt)
{
foreach (var entity in TrackedEntities)
{
ref readonly var velocityComponent = ref GetComponent<VelocityComponent>(entity);
SendMessage(new MotionMessage(entity, velocityComponent.Velocity * (float)dt));
}
}
}
}

View File

@ -1,3 +1,4 @@
using System.Numerics;
using Encompass;
using MoonTools.Structs;
@ -6,10 +7,12 @@ namespace PongFE.Messages
public struct BallSpawnMessage : IMessage
{
public Position2D Position { get; }
public Vector2 Velocity { get; }
public BallSpawnMessage(Position2D position)
public BallSpawnMessage(Position2D position, Vector2 velocity)
{
Position = position;
Velocity = velocity;
}
}
}

View File

@ -0,0 +1,16 @@
using Encompass;
namespace PongFE.Messages
{
public struct BounceMessage : IMessage
{
public Entity Entity { get; }
public HitOrientation HitOrientation { get; }
public BounceMessage(Entity entity, HitOrientation hitOrientation)
{
Entity = entity;
HitOrientation = hitOrientation;
}
}
}

View File

@ -0,0 +1,24 @@
using Encompass;
namespace PongFE.Messages
{
public enum HitOrientation
{
Horizontal,
Vertical
}
public struct CollisionMessage : IMessage
{
public Entity EntityA { get; }
public Entity EntityB { get; }
public HitOrientation HitOrientation;
public CollisionMessage(Entity a, Entity b, HitOrientation hitOrientation)
{
EntityA = a;
EntityB = b;
HitOrientation = hitOrientation;
}
}
}

View File

@ -0,0 +1,17 @@
using Encompass;
using MoonTools.Structs;
namespace PongFE.Messages
{
public struct UpdatePositionMessage : IMessage, IHasEntity
{
public Entity Entity { get; }
public Position2D Position { get; }
public UpdatePositionMessage(Entity entity, Position2D position)
{
Entity = entity;
Position = position;
}
}
}

View File

@ -0,0 +1,17 @@
using System.Numerics;
using Encompass;
namespace PongFE.Messages
{
public struct UpdateVelocityMessage : IMessage, IHasEntity
{
public Entity Entity { get; }
public Vector2 Velocity { get; }
public UpdateVelocityMessage(Entity entity, Vector2 velocity)
{
Entity = entity;
Velocity = velocity;
}
}
}

View File

@ -25,7 +25,10 @@
<ItemGroup>
<ProjectReference Include="..\FNA\FNA.Core.csproj"/>
<ProjectReference Include="..\encompass-cs\encompass-cs\encompass-cs.csproj" />
<ProjectReference Include="..\MoonTools.Structs\Structs\Structs.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MoonTools.Structs" Version="3.0.1"/>
<PackageReference Include="MoonTools.Bonk" Version="8.0.0"/>
</ItemGroup>
<Import Project="..\build\CopyFNALibs.targets"/>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />

View File

@ -1,8 +1,7 @@
<Project>
<PropertyGroup>
<BaseIntermediateOutputPath>obj\$(MSBuildPongFE)</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.props" />
<PropertyGroup>
<BaseIntermediateOutputPath>obj\$(MSBuildPongFE)</BaseIntermediateOutputPath>
</PropertyGroup>
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net461</TargetFramework>
@ -20,6 +19,9 @@
<PropertyGroup>
<DefaultItemExcludes>$(DefaultItemExcludes);DllMap.cs</DefaultItemExcludes>
</PropertyGroup>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.props"/>
<Import Project="..\build\CopyFNALibs.targets"/>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets"/>
<ItemGroup>
<Content Include="Content\**\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
@ -27,9 +29,10 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FNA\FNA.csproj"/>
<ProjectReference Include="..\encompass-cs\encompass-cs\encompass-cs.csproj" />
<ProjectReference Include="..\MoonTools.Structs\Structs\Structs.csproj" />
<ProjectReference Include="..\encompass-cs\encompass-cs\encompass-cs.csproj"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="MoonTools.Structs" Version="3.0.1"/>
<PackageReference Include="MoonTools.Bonk" Version="8.0.0"/>
</ItemGroup>
<Import Project="..\build\CopyFNALibs.targets"/>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />
</Project>

View File

@ -56,17 +56,31 @@ namespace PongFE
WorldBuilder.AddEngine(new InputEngine());
WorldBuilder.AddEngine(new PaddleMovementEngine());
WorldBuilder.AddEngine(new VelocityEngine());
WorldBuilder.AddEngine(new MotionEngine());
WorldBuilder.AddEngine(new CollisionEngine());
WorldBuilder.AddEngine(new BounceEngine());
WorldBuilder.AddEngine(new UpdatePositionEngine());
WorldBuilder.AddEngine(new UpdateVelocityEngine());
WorldBuilder.AddEngine(new BallSpawner(BallTexture));
WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch));
var paddle = WorldBuilder.CreateEntity();
WorldBuilder.SetComponent(paddle, new PlayerInputComponent(PongFE.Components.PlayerIndex.One));
WorldBuilder.SetComponent(paddle, new PaddleMoveSpeedComponent(400));
WorldBuilder.SetComponent(paddle, new PositionComponent(new MoonTools.Structs.Position2D(5, 5)));
WorldBuilder.SetComponent(paddle, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, 20, 80)));
WorldBuilder.SetComponent(paddle, new CanCauseBounceComponent());
WorldBuilder.SetComponent(paddle, new Texture2DComponent(PaddleTexture, 0));
WorldBuilder.SendMessage(new BallSpawnMessage(new MoonTools.Structs.Position2D(640, 360)));
WorldBuilder.SendMessage(
new BallSpawnMessage(
new MoonTools.Structs.Position2D(640, 360),
new System.Numerics.Vector2(-100, -20)
)
);
World = WorldBuilder.Build();
}
@ -78,7 +92,6 @@ namespace PongFE
protected override void Update(GameTime gameTime)
{
System.Console.WriteLine(1 / gameTime.ElapsedGameTime.TotalSeconds);
World.Update(gameTime.ElapsedGameTime.TotalSeconds);
base.Update(gameTime);