How do I use the Physics Engine?

A physics engine provides a realistic approximation of physical phenomena including motion, gravity, and collisions. Tynker has a built-in Physics Engine and you can use it to build video games with realistic game play without having to program all the math that is usually required to program a game with physics.

Here is a simple use of the physics engine - to make a bouncing ball simulation. Create a new project and add an soccer ball as an Actor.

Start the Physics Engine
On the stage, you start physics and setup the gravity.

{“func”:“registerFlagTrigger”,“next”:{“func”:“blockPhysicsStart”,“next”:{“func”:“blockPhysicsSetGravity”,“values”:[{“type”:“number”,“value”:“0”},{“type”:“number”,“value”:“10”}]}}}

Bouncing Ball
When you run the program the ball should already drop to the bottom edge of the screen. The default bounciness of all objects is very low. So they don’t bounce. To adjust this, apply this code on the ball Actor.

{“func”:“registerFlagTrigger”,“next”:{“func”:“blockPhysicsSetRestitution”,“values”:[{“type”:“number”,“value”:0.9}]}}

The co-efficient of restitution is the ration of the velocities of an object after and before a collision. So by making it close to 1, we are making the ball very bouncy.

So, with just a few lines of code you can simulate a bouncing ball. Now try to change the gravity settings, and the restitution settings and see how the ball bounces differently each time. Can you simulate a ball bouncing on the Moon?

realistic approximation :slight_smile: