Sunday, June 12, 2016

Construct 2 Tutorial - 32 More Gravity #1- Advenced Player Environment I...



https://youtu.be/RdqrRydib-A
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.

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.

construct 2 tutorial | construct 2 gravity | construct 2 physics tutorial | construct 2 physics | construct 2 physics platformer | construct 2 course

No comments: