Collision
The MonoGame.Extended.Collisions library contains a 2D grid-based collision system.
Installation
MonoGame.Extended.Collisions is distributed via a NuGet package. You can add the NuGet package to your C# project through your IDE of choice (Visual Studio, Xamarin Studio, Rider, etc) or through the Command Line Interface (CLI) using the dotnet command.
Usage
In this example, we will make a simple sandbox where shapes can move and collide with each other.
We start by defining an IEntity
interface that inherits ICollisionActor
, so we can insert the entities into our CollisionComponent
.
Next, we define our entity classes
The OnCollision
method and the Bounds property come from the ICollisionActor
interface. These will be called by the CollisionComponent
Setting up the game
First, we define our properties and fields
Then we Initialize our game by creating entities and adding them to the CollisionComponent
.
Updating the game
In the Update
method, we update all entities and the CollisionComponent
.