Physics in Construct 2: The basics

Favourite 107 favourites
Tutorial written by AshleyOriginally published on 29th, September 2011 - 5 revisions

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.

Setting the world gravity to 0.

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.

Setting a physics object to immovable.
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.

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.

Comments

11
Kyatric 41.4k rep

Thanks for the clear introduction.

Saturday, October 01, 2011 at 2:49:56 AM
7
ngreiner 2,323 rep

Very helpful explanation of the Physics objects properties. Thanks!

Monday, October 03, 2011 at 10:56:56 PM
0
Nickydude 9,613 rep

This has definitely helpedme get to grips with the physics side of C2. :)

Wednesday, November 16, 2011 at 11:18:58 AM
2
Bigheti 12.7k rep

I'm satisfied for this tutorial. Tks, and get more...

Wednesday, November 23, 2011 at 11:52:05 AM
2
tavitooo 4,801 rep

Mmm I dont understand very well english, and if I use google translate it dont work very well. My question if is possible create a menu in this great website to trasnlate to other languages? please I like to know all features of this great great great program. PLEASE!!! Help us all the world!! I speak Spanish! muchas gracias

Saturday, November 26, 2011 at 3:47:04 AM
2
Velojet 16.9k rep

Superbly understandable intro! Even better: your implementation of physics in C2.

Thursday, December 08, 2011 at 1:34:42 AM
1
Ulisses 8,451 rep

Helpful!!!

Saturday, December 10, 2011 at 1:59:41 AM
1
Funbit 2,903 rep

As a newcomer to this software this is very helpful.

Saturday, March 10, 2012 at 3:28:03 AM
4
Behamut 3,499 rep

Thanks guys! i'll finally be able to aply what seem wasted years in school...

Tuesday, June 19, 2012 at 4:26:18 PM
1
blackmwana 2,333 rep

absolutely love it

Thursday, June 28, 2012 at 4:39:25 PM
2
PedroGamer 2,388 rep

Construct 2 is fantastic!

Friday, July 06, 2012 at 6:25:14 PM
1
Jander 3,219 rep

That's a great tutorial! Thank you...

Tuesday, October 02, 2012 at 6:05:58 PM
2
viniciusmarcius 1,938 rep

Congratulation, very cool! tks

Friday, October 05, 2012 at 6:11:36 AM
0
kalimullahsheikh 2,166 rep

Every one doing best begenors

Sunday, October 21, 2012 at 12:58:58 PM
1
jamesnepomuceno 2,384 rep

Once again, a great tutorial. Tks.

Tuesday, November 27, 2012 at 12:01:57 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.