07. Creating the Puck Entity


Now that we have our PlayerBall movement working, we’ll add a Puck Entity which the user can hit around. You’ll find that the Puck is very similar to the PlayerBall. In a full game you may consider using inheritance to standardize your code; however, we’ll simply make a new Entity in this tutorial to avoid introducing too many new concepts.

Creating a Puck Entity

To create a Puck Entity:

  1. Right-click on your Entities tree item in Glue
  2. Select Add Entity
  3. Name the Entity Puck
  4. Check the Circle check box under Collisions
  5. Verify that ICollidable is checked (it should be checked automatically when Circle is checked)

  6. Click OK

For more information on how to perform the above steps, you may want to review the tutorial which created the PlayerBall Entity here.

Differentiating the Puck

Currently our Puck and PlayerBall both have Circle bodies, and by default the Circles will have the same radii and color. To differentiate the Puck from the PlayerBall:

  1. Expand the Puck entity
  2. Click on the CircleInstance object under the Puck Entity
  3. Click the Variables tab
  4. Find the Color variable
  5. Change the value to Red using the drop-down
  6. Change the Radius value to 6

Computer settings matter: If your computer is set up so the decimal separator is the comma ‘,’ instead of the period ‘.’ then you should enter values using the ‘,’ character. Unlike C# code, Glue obeys your computer’s language settings of your computer.

The Puck should appear as follows in GlueView:

Adding a Puck to the GameScreen

Now that we have a Puck Entity, we’ll want to add it to the GameScreen. In a previous tutorial we did this by right-clicking on the Objects folder. Let’s look at another way to create objects:

  1. Click + hold on the Puck tree item in Glue
  2. Drag down to the GameScreen tree item in Glue
  3. Release
  4. You will now have an instance called PuckInstance in your Screen

Positioning your objects

If you run your game or view it in GlueView you’ll notice that the PlayerBallInstance and PuckInstance are both at the center of the Screen. Let’s reposition the PlayerBallInstance:

  1. Select the PlayerBallInstance object under your GameScreen
  2. Change the X value to 180

You should see changes to variables reflect in GlueView

Puck Collision

Next we’ll add collision logic for the new PuckInstance in our GameScreen. We will add two types of collision:

  1. Puck vs. Walls
  2. Puck vs. PlayerBallInstance

To add this collision, modify the CollisionActivity method as follows:

Notice that the mass variables for PlayerInstance vs. PuckInstance differ compared to wall collision. The PuckInstance is given a mass of .3 relative to a mass of 1 for the PuckInstance, resulting in the PuckInstance behaving as if it has 30% of the mass of the PlayerInstance.

If you run the game, you should be able to hit the Puck around the level.

Adding Drag

Currently the Puck will move indefinitely after being hit. We’ll assign the Drag value to the Puck just like we did to PlayerBall:

  1. Expand the Puck Entity in Glue
  2. Right-click on the Variables item
  3. Select “Add Variable”
  4. Select “Expose an existing variable”
  5. Select “Drag” as the Variable
  6. Change “Drag” to .4

Now the Puck will slow down over time just like the PlayerBall.

<- Advanced PlayerBall ControlsAdding multiple players ->