Learning about physics

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:

Learning about physics

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).

Note

One thing to remember is that all physics objects are affected by gravity by default; this means that all physics objects will go down unless something stops them.

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.

Tip

Physics objects only interact with other physics objects. To make physics objects interact with sceneries, we need to add Physics behavior to them.

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:

Learning about physics

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:

  • If you want the players to interact with the object later in the game, use immovable physics objects. For example, you can have a box that initially can't be moved, but after the player obtains a special glove, they can move the box. In this case, you can use immovable physics objects and then set the immovable property to No in the code.
  • If the objects only act as an obstacle, then it's better to use solid objects.
  • If players can only interact with the objects in one way, then use solid objects, for example, a switch that is placed only for the player to shoot it to open a door.

Forces, impulses, torques, and joints

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:

  • Force: A force is an outside influence that changes an object's speed or direction. Forces are applied over time, so it's best to use forces on events that have the value of true for a long time, such as an Is key down keyboard event.
  • Impulses: Impulses are similar to forces, but instead of being applied over time, it is a one sudden change. When you apply a force to an object, its speed and direction will change over time, but when you apply an impulse to an object, you'll change its direction in an instant. Impulses are best used on events that have the value of true in an instant, such as triggers.
  • Torque: A torque is a force that's applied to rotations; it will change the speed of rotation rather than moving an object.
  • Joints: Joints connect two objects together. They can still rotate independently, but their positions are always related to one another; if you move one object, then the joint one also moves. For example, this is used to create some kind of a device or machine that follows the rule of physics. They can be destroyed realistically.

Adding a puzzle element

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:

Adding a puzzle element

Moving our character with the Physics behavior

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.

Moving our character with the Physics behavior

Note

When adding a Physics behavior to the main character, don't forget to set the Prevent rotation property to Yes, unless you want the main character to roll (for example, if the character is a ball or an armadillo).

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.

Moving our character with the Physics behavior

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:

  • Moving right: When we press the right arrow key, we will apply the force at a positive value on the x axis to make it move to the right
  • Moving left: When we press the left arrow key to move to the left, we will apply the force at a negative value on the x axis
  • Jump: When we jump, we will apply an impulse at a certain degree so that it moves up

The code will look as follows for the right arrow pressed event:

Moving our character with the Physics behavior

Similarly, for the left arrow pressed event, the code is as follows:

Moving our character with the Physics behavior

Finally, for the up arrow pressed event, we have the following code:

Moving our character with the Physics behavior

Changing the properties of the rock

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:

  • Friction: This defines how easy it is for objects to move against each other while touching; it is the friction you learned back in your physics class. You can set the value between 0 and 1, with 0 being no friction at all and 1 being maximum friction.
  • Linear damping: This is the slowdown rate of a moving object, with a value of 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.

Selecting an object via its UID

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.

Selecting an object via its UID

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.

Selecting an object via its UID

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:

Selecting an object via its UID

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:

Selecting an object via its UID

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset