Physics behaviors and the collision polygon

The Physics behavior provides the ability to create some interesting games.

Turn In: Game file named Physics2.capx

In this tutorial, we will look at the Physics behavior. Start by copying the Physics1.capx to Physics2.capx. If you do not have a Physics1.capx file, here is a summary of what that file should look like: window size of 1024 by 768 and layout size of 1280 by 768. You should have a sprite named ramp whose width is twice its height (make a triangle by drawing a line from the upper left corner to the lower right corner then fill the bottom half of the triangle), a wooden block image, and a rock image.

Add the physics behavior to the ramp, rock, and wooden block then Run layout. Notice what happens ... all of the objects drop off the screen. Why? Gravity is part of physics and as such things fall.

Create a new sprite called base that is a thin line stretching across the bottom of the layout. Add physics to that line. Change the Immovable physics behavior for the base object to Yes. Run layout and notice what happens to the other objects.

If not already, position the ramp and the wooden block on top of the base so that they are not dropping. Duplicate the wooden block so that there are a total of six. Stand up three blocks (longer side up), two blocks in each of the gaps between the three blocks, and one block in the gap between the two blocks. It will look like a pyramid.

Position the rock as high above the wooden blocks as possible then Run layout. Move the rock a little bit to the left or the right and Run layout. Change the size of the rock to bigger and smaller then Run layout.

Position the rock above the left side of the ramp and run the layout. While visually we would expect to see the rock roll down the ramp, the rock just sits on top of the ramp (the white portion of the rectangle).

Collision Polygon

Double-click on the ramp. At the lower left of the editor tools is the "Set collision polygon" button ... click that button. Notice that you have four handles on each corner of the rectangle. Grab the handle on the upper right corner and drag it to the middle of the rectangle in a way that matches the shape of the ramp. Close the editor and Run layout. Notice that the rock now goes down the ramp.

Click on the rock and change the "Collision mask" property to circle. Run layout and notice the difference.

The ramp should be on the left side of the game and the stack of wooden blocks on the right. The rock should roll toward the blocks and may even go far enough to hit the blocks and maybe knock them down. If you do not have things layed out that way, do so now.

One of the physics properties is density. Heavier or more dense objects should have a higher density value while lighter or less dense objects should have a lower value. The default density is 1.

Change the rock to a density of 5 and Run layout. The rock should go farther and knock the blocks further away. Try some higher values for the rock and lower values (i.e. 0.7, 0.5, or 0.2) for the wooden block. Run layout after each change.

The Elasticity property defines how bouncy the objects will be. Change the rock to elasticity of 1 which will make it more like a basketball than a rock. Run layout. Try a few values higher than 1 and between 0.2 and 1 (i.e. 0.5, 0.7). Notice how each change affects the game.

There are some additional physics properties. Use the link below to read through what some of those properties do and try a few of them out.

Construct2 Tutorial - Physics in Construct2: The basics

Copyright © 2021 Eric Schumm. Permission granted to freely use this in your classroom.