Physics in Construct: The Basics

86

Stats

59,438 visits, 83,998 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

This tutorial is recommended by the Construct team! This means it contains useful, high quality information that will help grow you as a game developer.
Published on 3 Oct, 2017. Last updated 4 Mar, 2019

Construct includes the Physics behavior, powered by the Box2D physics engine. This allows you to have objects moving with real-world physics. Click here to open a demo to see the kind of physics it simulates. Physics can make your games really fun and engaging! Here's an overview of how you can get Physics to work in your game.

If you ever took a Physics class in school, you'll find some of the things you learned applicable to Construct's physics. I'll still explain the basics in brief in case you haven't learnt the concepts before. Interested in some of the theory? You might want to read Wikipedia's article on Newton's laws of motion.

Lots of physics examples come with Construct! Search for Physics in the Start Page to find them. They're well worth having a look. The descriptions here will probably make a lot more sense if you've seen it in action first.

How to add Physics

Select an object you want to add Physics to. In the Properties Bar, click Behaviors. Click Add new behavior and from the dialog pick the Physics behavior. All done!

We'll call any object with the Physics behavior added a "physics object".

Gravity

By default, gravity is present on physics objects, which accelerates all objects downwards. The default gravity is 10 (remember, the Y axis increases downwards in Construct). If you want to turn off gravity, e.g. for a space game, you can use the Set gravity action on any physics object. Note: gravity applies to the whole "world". If you set gravity to 0 on one object, gravity is turned off for all objects.

Turning off Physics gravity

Making the scenery

You don't want your game's floor to fall away off the bottom of the screen with gravity too! Even in "zero gravity" games, a piece of scenery will be slightly pushed back if something collides with it, and possibly start to rotate. (Newton's third law of motion: "every action has an equal and opposite reaction", which means the scenery is pushed back a bit too.)

Most of the time in our games we want to simulate the scenery being rock solid: not falling away with gravity, and not being pushed a little bit back by things hitting it. Enable the physics object's Immovable property to simulate this.

Enabling immovable property on Physics

The object is then simulated as if it has infinite mass. It won't go anywhere!

The other physics properties

Let's briefly go over the other properties in the physics behavior:

Collision mask

This sets the collision shape of the object. By default, it's use collision polygon. If you open up the image editor and click the collision polygon tool, you can change the bits of it that will collide. Be careful not to use too many points, or it could slow down the game.

If set to either bounding box or circle, the collision polygon from the image editor is ignored. Instead it'll either use a rectangle around the object (bounding box), or a circle which is useful for rolling objects like balls.

Prevent rotation

If enabled, the object will never rotate even if struck at a glancing angle. This might be useful if you want to control the angle of the player yourself. For example, in a platform game, you probably don't want your player to trip over and fall on their face every time they try to run.

Density

Density is used to determine the object's mass. Mass defines how hard the object is to move. (Note that "weight" isn't the exactly correct term - weight is dependent on gravity, and objects still have mass in zero gravity. Objects with a large mass are still harder to move in zero gravity.) Your object's mass is determined by its density multiplied by the area of its collision shape. So a really huge object has a much bigger mass than a small object, even if their densities are the same.

If you have a concrete block, you'd want to give it a much higher density than a block of foam!

Friction

Friction affects how much the object is slowed down by sliding against another object. No friction is like skating on ice, and high friction is like dragging a brick along concrete. It's harder work to pull the brick along on concrete than on ice.

Elasticity

The elasticity (or "bounciness" or "restitution") affects how bouncy the object is. An object with high elasticity will bounce high when dropped on to the floor, and an object with no elasticity won't bounce at all.

Linear damping

Objects move at the same speed in the same direction forever, unless something else affects them. Think of throwing a tennis ball in space - off it goes forever. (Newton's first law of motion: "every object in a state of uniform motion tends to remain in that state of motion unless an external force is applied to it".) On Earth, forces like gravity, friction and air resistance tend to make this less noticeable.

In your physics game, you might find you push an object and it carries on forever, in accordance with Newton's third law. You might want to simulate some friction against the floor, or air resistance. Increasing the linear damping makes objects slow down gradually by themselves, ultimately to a stop. Zero linear damping is like in space - objects will carry on forever.

Angular damping

Angular damping is a very similar concept to linear damping, but instead to do with the object's spin. Again, in space, a spinning object will spin at the same speed forever. Increasing the angular damping will make a spinning object gradually slow down until it is no longer spinning. Note the damping occurs regardless of how fast the object is moving.

Bullet

Physics objects that move very quickly, such as projectiles, may need extra processing to ensure their collisions are handled accurately. Enabling the Bullet option will do extra work to ensure collisions properly take in to account the high speed. You shouldn't use this on everything, since it will unnecessarily waste processing time, but it's a useful option to have in case you notice issues with very fast objects.

General physics tips

Performance

Physics simulations are very CPU intensive. It can take a lot of processing to work out the proper motion. To make sure your game runs fast, avoid using too many physics objects.

Note that objects which have come completely to a stop, and are not moving or rotating at all, are "put to sleep" by the simulation. Then, they don't need processing any more. If the object is hit by another one it "wakes up" and starts using processing again. However, if the object is even moving in the slightest, it won't be put to sleep. For example, all the blocks in a teetering tower will remain awake. It's just something useful to bear in mind for performance: if you have hundreds of objects asleep and at most only ever 20-30 moving (even slightly), the game should still run well.

Stability

Physics simulations are not totally robust. If you simulate unrealistic things, like a gigantic concrete object hitting a block of foam at the speed of sound, the result is likely not to be realistic either. In fact, anything involving extreme forces is likely to cause the simulation to become unstable (causing unrealistic results, e.g. objects moving through or inside each other).

Things like incredibly heavy objects piled on a stack of really light boxes, huge piles of objects, or very fast moving heavy objects tend to cause instability. Try to keep everything in your game at reasonable proportions.

The same also applies to object sizes. Very small (under 5 pixels) or very large (over 500 pixels) objects may not simulate realistically either. Try to keep all widths and heights in the 5-500 pixel range, and still avoid extreme proportions (e.g. a 5x500 sized object).

In other words, Physics will work best with objects of about the same size and mass interacting at relatively low speeds.

Manual movement & other behaviors

If you move objects by events (e.g. set X, set Y) or other behaviors (e.g. also adding 8 direction to a physics object), the physics simulation will do its best to keep up with what you've done. However, it is usually more realistic to achieve the same thing by applying forces and impulses to physics objects. This keeps everything "in the physics world" and realistic.

For example, if you use the Set Position action to move an object to the other side of the layout instantly, it has effectively teleported. That's not a realistic physical phenomenon, so the result may be unrealistic as well. To keep up, the physics behavior will spot this, and simulate the object suddenly moving extremely fast towards its destination point for one tick (about 1/60th of a second). That's an incredible amount of acceleration, speed, then deceleration. Remember, to keep a simulation stable, you should avoid extremes.

Although you can add the Platform behavior to a Physics object, the two tend not to get along very well. Again, it's better to achieve the same thing by applying forces. See the Physics - rolling platformer example for a demonstration of how you can achieve this.

Animated sprites

Do not use animated sprites with the Physics behavior. Since animation frames can be a different size, every time the frame changes Construct has to remove the physics object and teleport a new one in its place. This has similar issues to using Set position, as described previously.

If you need to make a physics object look animated, use an invisible sprite with no animation for the Physics behavior, and then position an animated sprite on top (e.g. using the Pin behavior).

Conclusion

Physics can be really fun to have in your games, but don't forget: avoid having too many objects, avoid extremes, try to move things by forces and impulses alone - and it doesn't hurt to know some of the theory!

Happy with the basics? You might be interested in the next tutorial: Physics in Construct: Forces, impulses, torque and joints.

Next Tutorial In Course

Physics in Construct: Forces, impulses, torque and joints 08:10

Covers the use of forces, impulses, torque and joints, including what they do and how to use them in your games.

  • 7 Comments

  • Order by
Want to leave a comment? Login or Register an account!