We will now discuss physics in Construct 2. To apply physics to an object, we only need to add a Physics behavior to it. All objects with the Physics behavior are referred to as physics objects. Physics is a complex subject, so I will only explain it briefly here to cover what you need to know to start experimenting with it.
If you still remember your physics class back in school, then this will be really useful here because the concepts are the same. We will create a third layout to demonstrate our physics object, so create a new layout and make it look like this:
As you can see, we used two solid objects that we used to build up our levels earlier. However, we will add one new sprite object here called rock
; the sprite for the rock
object can be found under Base packItems
. The rock doesn't do anything for now, but we'll use it as a physics object, so let's add a Physics behavior to it (the Physics behavior can be found in the movement section when you add a behavior).
If we test Layout 3
, it is expected that the rock will fall down and stop when hitting the floating platform. Unfortunately, this is not the case. The rock keeps falling down through the floating platform and the ground. Why? It's simply because physics objects only interact with other physics objects; they won't interact with objects that have Solid or Jump-thru behaviors.
Why is this? Why does Construct 2 not make the solid objects interactable with physics objects? The answer is that although the physics objects can be treated as solid objects, solid objects can't be treated as physics objects. If you still remember, Newton's third law of motion states that every action has an equal and opposite reaction. In our game, this means that when a physics object collides with scenery, such as our platforms, the platforms will be pushed back a bit. Solid objects won't be able to calculate this collision, so physics objects ignore them. To make our scenery interact with a physics object, we will have to make our scenery a physics object as well.
But wait a minute; if we add a Physics behavior to the floating platform, then won't the platform fall as well? Did I say that all physics objects are affected by gravity? Yes, except for physics objects that are immovable. Immovable physics objects don't move at all, either by gravity or by collision from other objects; their only purpose is to act as an obstacle to other physics objects.
To make a physics object immovable, we simply need to change the value of its Immovable property. So now, let's add a Physics behavior to the boxAlt
object and change its Immovable property to Yes, as shown in the following screenshot:
Test the game again, and the falling rock will collide with the platform as we want it to.
By now, maybe you're wondering when to use solid objects and when to use immovable physics objects. Here are the simple rules:
No
in the code.As we're going to use the Physics behavior, we need to get familiar with the terms in physics, especially if they're also used in Construct 2. The terms are as follows:
true
for a long time, such as an Is key down
keyboard event.true
in an instant, such as triggers.Now that we know the terms used in physics, let's put them to use by adding another feature to our game: the puzzle element. Puzzles are common in platformer games; they're usually the hurdles you have to overcome in order to reach new areas to complete the level.
So, let's create a simple one for now. We'll have our character to push the rock to hit a button on the ground below; this will open a door. We'll learn how to apply a force to a physics object with the character pushing the rock. Continuing from layout 3
, let's add a few more sprite objects to it. We'll add the obstacle door that stops us from advancing forward; you can get the sprite for it under the Tiles
folder. After that, we'll add the button. There are two animations for it: Default
and Pressed
. You can get the sprites for both of them under the Items
folder. Add a Physics behavior to both of them, set the Immovable property to Yes (do the same to the ground object), and then place them as follows:
We also added our alien to Layout 3 to push the rock with. As with any other object in this layout, we'll add a Physics behavior to the alien, but there's one important thing to know when adding a Physics behavior to the character that the players control. When moving, physics objects don't just slide on a surface, but they'll also rotate based on a lot of things applied to them (for example, forces, impulses, and collisions with other objects). We surely don't want our character to rotate, so we'll prevent it by setting the Prevent rotation property to Yes in the Properties bar of alienGreen
.
There's another thing to keep in mind: when moving a physics object, we must only use physics actions and expressions. This is to ensure that Construct 2 does the physics calculations correctly. If we move a physics object using other behavior, such as the Platform behavior, then we might end up doing something that shouldn't be done, for example, walking through an immovable scenery object. So first, we will disable the Platform behavior by setting its property to Disabled.
Then, we will move the character by applying forces and impulses to it. To do so, switch over to the layout 3
event sheet. There are three situations in which we want to move the character:
The code will look as follows for the right arrow pressed event:
Similarly, for the left arrow pressed event, the code is as follows:
Finally, for the up arrow pressed event, we have the following code:
By now, you can try testing the game and move around in the layout. You will find that the only object the character can interact with is the rock, because it's the only object that is not immovable. However, the rock is too easily moved; it's as if it's too light for a rock. We're going to make it harder to move.
To do so, we only need to change the value of it properties, which are friction and linear damping:
0
and 1
, with 0
being no friction at all and 1
being maximum friction.0
that means no slowdown at all and 1
that means maximum slowdown. Set the value of the friction and linear damping properties to 1
, and we're done making the rock heavier to push.Now that everything is set and ready, we can start making the buttons react when collided with the rock, and then, we'll destroy the blue locks. The way we destroy them is not just by making them disappear with the destroy action, but we'll use impulse to throw them at an angle and then destroy them when they're outside of the layout. However, first, I want to explain what a UID is.
UIDs are unique identification numbers that are assigned to every object once they are created (you can't change them), whether in the layout or by code. No two objects have the same UID, and as these numbers are unique to each object, we can select an object by its UID. If you followed my example correctly, the UIDs for the lock objects must be between 98 and 103, but if this not the case, you can check the value of UID yourself by clicking on the object and looking at the top part of the Properties bar.
Now, we'll use the UID in the event sheet to select the locks individually. What we want to do is when the rock collides with the button, we want change the button's animation to Pressed and afterwards apply an impulse to the boxes. We can just apply the same impulses to all the boxes, making them move in the same direction, but instead, we will make them move in different directions similar to an explosion effect (by picking each one of them). Remember to make the lockBlue
objects movable, or we won't be able to move them around.
Your UID of the lockBlue
object might be different; check them first before you write the code. We also want to destroy the locks after they're blasted or, to be more precise, we want to destroy them when they get out of the layout. Thankfully, there's a behavior for this. The behavior is simply named Destroy Outside Layout and will destroy the object if it goes outside of the layout. Add it to the lockBlue
object.
One very important thing to do before testing your game is to check the collision polygons of your animation, especially the pressed animation of the button. If you just load the pressed button image and let Construct 2 guess the collision polygons, you would get something like this:
This is not good because as you can see, there are two lines that intersect each other. This can ruin the Physics behavior and give rise to a game-breaking bug, because the Physics behavior won't be able to correctly calculate the collision area of this object. To fix this, change the collision polygon to be as follows:
This will prevent the game-breaking bug from affecting our game and make our game run as we want it to. Test the game now and push the rock to the button; you'll see the locks being blown away. Save your project for now before you lose all the changes you made until now, and let's move on.