Physics in Construct 2: The basics

4

Stats

32,274 visits, 61,548 views

Tools

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.

Published on 29 Sep, 2011. Last updated 19 Feb, 2019

Construct 2 includes the Physics behavior, powered by Box2DWeb. This allows you to have objects moving with real-world physics - here's a demo to show the idea. 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 2'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. Here's another link you might find useful.

Lots of physics examples come with Construct 2! Click Browse all examples on the start screen, and all the physics demos filenames start with "Physics - ", e.g. "Physics - basics.capx". 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 Add / Edit under Behaviors. Click the green plus icon and from the dialog pick Physics. 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 2). If you want to turn off gravity, 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.

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. Set the physics object's Immovable property to Yes to simulate this.

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

Note: Physics objects only interact with other physics objects. Adding the Solid behavior to the floor has no effect on physics objects. They ignore anything which does not also have the physics behavior. Physics objects will pass right through 'Solid' objects, unless you give them the Physics behavior and set them to immovable.

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.

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, it's recommended that you don't use too many objects at once. Over 100 physics objects moving at once is likely to slow your game down. Also, phones and tablets have much more limited processing power than a desktop computer. If you're targeting mobiles, you should be very conservative, and try not to have more than 20-30 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 (unrealistic, 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 Set Position 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 provided with Construct 2 for a demonstration of how you can achieve this.

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 2: Forces, impulses, torque and joints.

  • 0 Comments

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