2019-05-29 04:05:36 +00:00
---
title: "Collision Dispatch"
date: 2019-05-28T19:06:03-07:00
weight: 700
---
2020-07-14 23:02:50 +00:00
Let's make the CollisionEngine. All it needs to do is read the CollisionMessages, determine what, if any, kind of collision occurred, and create more specific collision messages from them.
Right now the only collision responses we care about are bounces.
Let's create our Actor, **PongFE/Components/CanCauseBounceComponent.cs** :
```cs
using Encompass;
namespace PongFE.Components
{
public struct CanCauseBounceComponent : IComponent { }
}
```
And our Receiver, **PongFE/Components/CanBeBouncedComponent.cs** :
```cs
using Encompass;
namespace PongFE.Components
{
public struct CanBeBouncedComponent : IComponent { }
}
```
2020-07-21 22:30:21 +00:00
We'll also need **PongFE/Messages/BounceMessage.cs** :
```cs
using Encompass;
using PongFE.Components;
namespace PongFE.Messages
{
public struct BounceMessage : IMessage, IHasEntity
{
public Entity Entity { get; }
public HitOrientation HitOrientation { get; }
public BounceMessage(Entity entity, HitOrientation hitOrientation)
{
Entity = entity;
HitOrientation = hitOrientation;
}
}
}
```
2020-07-14 23:02:50 +00:00
In **PongFE/Engines/CollisionEngine.cs** :
```cs
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))
{
SendMessage(new BounceMessage(b, hitOrientation));
2019-05-29 04:05:36 +00:00
}
}
}
}
}
```
2019-06-12 01:15:57 +00:00
Now we are emitting proper collision messages every time an entity collides with another.
2019-05-29 04:24:48 +00:00
2020-07-14 23:02:50 +00:00
Don't forget to add our new engine in **PongFEGame.cs**
2019-05-29 04:24:48 +00:00
2020-07-14 23:02:50 +00:00
```cs
WorldBuilder.AddEngine(new CollisionEngine());
2019-05-29 04:24:48 +00:00
```
2019-05-29 04:05:36 +00:00
2020-07-14 23:02:50 +00:00
Next, we'll make our game actually do something in response to the Bounce message.