05.Creating the Screen Collision

Introduction

So far we have a very simple project with a PlayerBall (which appears as a Circle) which can be moved with a Xbox 360 game pad or the keyboard. This tutorial will cover creating collision for the game. We’ll be using a new FlatRedBall type called AxisAlignedRectangle which can be created through Glue just like Circle objects. We’ll use multiple AxisAlignedRectangles to assemble our level, and we’ll be organizing these in a list object.

Creating the Walls PositionedObjectList

In Glue, Lists are created by creating an object of type PositionedObjectList<T>. PositionedObjectList<T> instances must be defined as containing a specific type of object. These lists can contain collision objects (such as Circle or AxisAlignedRectangle), visual objects (such as Sprite or Text), or entities.

We’ll be creating a list of AxisAlignedRectangles which will act as the walls in our GameScreen. To do this:

  1. In Glue, expand GameScreen
  2. Right-click on Objects
  3. Select Add Object
  4. Verify that FlatRedBall or Custom Type is selected
  5. Click on PositionedObjectList<T>
  6. Set the List Type as FlatRedBall.Math.Geometry.AxisAlignedRectangle
  7. Name the list Walls
  8. Click OK

BeefballAddWalls.gif

Adding Wall AxisAlignedRectangles

Now that we have a PositionedObjectList for our walls, we will add the individual walls. For this step we recommend keeping Glue Preview open and using it to help position the AxisAlignedRectangles.

To add the first wall AxisAlignedRectangle:

  1. Right-click on the Walls item.
  2. Select Add Object
  3. Enter the name Wall1 and click OK. We are adding this new object to a PositionedObjectList of AxisAlignedRectangles, so Glue doesn’t ask us for the object type – it knows to make this new object an AxisAlignedRectangle.

AddWallToListBeefball2.gif

Now we can modify the properties of this wall with the help of GlueView. Select “Wall1” and change the values as follows:

CreateRectangle2.gif

  • Y = 300
  • Width = 800
  • Height = 30

Doing so will move the rectangle to the top of the screen.

Notice that objects can also be moved in the Glue Preview window with the mouse, but typing in values is often useful for fine-tuning positions.

Now that we’ve created a single wall, we can duplicate it by right-clicking on it and selecting the “Duplicate” command:

DuplicateCommand.png

Create 5 duplicates, so that the Walls list holds a total of 6 AxisAlignedRectangle instances:

SixWalls.PNG

Next set the X, Y, Width, and Height variables for for the newly-created walls as follows:

Wall2

  • X = 0
  • Y = -300
  • Width = 800
  • Height = 30

Wall 3

  • X = -400
  • Y = 200
  • Width = 30
  • Height = 200

Wall 4

  • X = 400
  • Y = 200
  • Width = 30
  • Height = 200

Wall 5

  • X = -400
  • Y = -200
  • Width = 30
  • Height = 200

Wall 6

  • X = 400
  • Y = -200
  • Width = 30
  • Height = 200

When finished, the game should have walls made of AxisAlignedRectangles with gaps on the left and right sides for goals:

WallsAndGoals.PNG

Implementing Collision

Although we’ll be implementing more advanced collision in the following tutorials, let’s add some quick collision to the game so we can see it in action. Since the PlayerBall entity implements the ICollidable interface (we checked this value when we first created the PlayerBall entity), it has built-in collision functions. In this case, we’ll use the CollideAgainstMove  method. To add collision:

  1. Open your GameScreen’s custom code file in Visual Studio (GameScreen.cs)
  2. Modify your code so that the CollisionActivity is called inside your GameScreen’s CustomActivity, and add the CollisionActivity method after CustomActivity. Your code should look as follows:

The CollideAgainstMove  collision method will both tell you if a collision has happened (by its return value) and will reposition the colliding objects to prevent overlap. The PlayerBallInstance  has a 0 mass, therefore it should always be the object to be moved when collisions occur, keeping the wall stationary. For more information on CollideAgainstMove , see this page.

Conclusion

Even though the game isn’t really playable yet, we’re definitely starting to see something come together. We have a movable object and game boundaries which have solid collision. The next tutorial will dive deeper into the control of our PlayerBall Entity.

<- Controlling an EntityAdvanced PlayerBall Controls ->