Using Farseer in a Glue Project


This walkthrough shows how to add Farseer to a simple Glue project. We will create a diagonal stack of blocks which will fall and collide against a static surface.

This tutorial uses the FlatRedBall desktop engine, which means it uses proper XNA (as opposed to MonoGame). MonoGame projects must use the MonoGame version of the Farseer .dll.

Creating the Block Entity

We will begin with an empty Glue project:

Add a new Block entity:

  1. Right-click on Entities
  2. Select Add Entity
  3. Enter the name Block
  4. Click OK

Next, we’ll add a Sprite to the Block so we can see it in-game:

  1. Expand the Block entity
  2. Right-click on Objects
  3. Select Add Object
  4. Select Sprite

  5. Click OK

The Sprite needs to be modified so it will show up in game:

  1. Change the Sprite’s Color Operation to Color. This allows us to use solid colors to draw the sprite instead of a texture.
  2. Set the Red value to 1
  3. Delete the Texture Scale value
  4. Enter a Width of 32
  5. Enter a Height of 32

Creating the GameScreen

Next we’ll create a screen to hold our Block instances and the Farseer logic:

  1. Right-click on Screens
  2. Select Add Screen
  3. Enter the name Game Screen
  4. Click OK

GameScreen needs a Block list so that we can construct the blocks in code and have them be automatically managed. To do this:

  1. Push and hold the right mouse button on the Block entity
  2. Drag the entity onto GameScreen
  3. Release the mouse button
  4. Select Add Entity List

Adding Farseer to the Visual Studio Project

Now that our Glue project has been created, we’ll add Farseer to the Visual Studio project:

  1. Download the Farseer precompiled .dll:
  2. Open the game project in Visual Studio
  3. Right-click on References in Visual Studio under your game project
  4. Select Add Reference
  5. Click the Browse category
  6. Click the Browse… button
  7. Navigate to where you saved the Farseer dll file
  8. Select the file and click Add

Preparing the Block Entity for Farseer

We’ll add the code to create a Farseer Body  instance in the Block.cs  file. Open the Block file and modify the class so it appears as shown in the following code:

Adding code to GameScreen

Finally we’ll add code to our GameScreen . Modify your GameScreen.cs  file so it looks like the following code:

If we run the game we’ll see our blocks falling and colliding: