Physics

These puzzles are used to simulate physics behavior of the objects.

Visual programming blocks to simulate physics

Contents

Physics Puzzles Reference

create physics world

Initializes the physics engine using the specified gravity and frames-per-second parameters. The default gravity value of 9.8 corresponds to Earth surface conditions. Zero value means no gravity as in space. The higher fps value the better simulation quality at the expense of performance.

The soft body checker activates the soft body simulation capability.

Creating physics world with visual scripting

Under the hood, this puzzle also enables collision detection and automatic synchronization with graphics.

create rigid body

Creates a physics body from a specified object, of dynamic, kinematic, static or ghost type. Assigns the collision shape and sets the mass for the body (valid for dynamic bodies only). Also works for a list of objects, a group (or a list of groups) or with the all objects puzzle.

Creating rigid body with visual scripting

Physics body types:

Supported rigid body shapes:

For performance reasons, give priority to primitive physics shapes (boxes, spheres, capsules, cones and cylinders) over mesh shapes. Also, static meshes have much better performance than dynamic, kinematic and ghost.

create soft body

Creates a soft body from a specified object with a given total mass and optional pressure params. Also works for a list of objects, a group (or a list of groups) or with the all objects puzzle.

Visual logic block to create and simulate soft bodies

Supported soft body shapes:

Don't forget to activate soft body simulation on the physics world before creating soft bodies.

remove physics body

Removes physics from a specified object by destroying the physics body associated with it. Also works for a list of objects, a group (or a list of groups) or with the all objects puzzle.

Block to remove physics body

apply body param

Sets parameters for the physics body associated with a specified object. Also works for a list of objects, a group (or a list of groups) or with the all objects puzzle.

Visual programming block to apply physics params

Parameters:

Continuous collision detection with visual programming Visual logic block to apply physical values

When two bodies collide, friction and restitution parameters of both of them are taken into consideration.

get body param

Gets a parameter from the physics body associated with a specified object.

Visual logic block to retrieve physical values

Parameters:

set body state

Change the state to the physics body associated with a specified object. Also works for a list of objects, a group (or a list of groups) or with the all objects puzzle.

Visual logic block to set collision body state

Parameters:

on simulation tick

Trigger puzzles specified in the *do" slot just before or just after the physics simulation tick.

On simulation tick visual scripting block

Physics simulation tick corresponds to the FPS value specified when creating physics world and does not coincide with rendering frame. As such, we recommend that you apply forces/velocities/impulses before simulation tick and detect collisions between your bodies after simulation tick. This way you can achieve more stable and realistic simulations.

snap body

Moves a physics body associated with a specified object, and the object itself, to the position of another object by copying its transform data. Also copies rotation. Does not work with lists, groups or the "all objects" puzzle.

Visual programming block to snap physics bodies

Works similar to apply body param / position puzzle.

add constraint to

Connect two physics bodies by a constraint of the given type.

Visual programming block to create physical contraints

Constraint types:

Physical constraints example script

remove constraint from

Remove physics constraint.

Block to remove physical constraints

anchor soft body

Anchor soft body to a given rigid body at the point in space represented by the given vector.

Visual logic block to anchor collision objects

detect collisions and collision info

Detects if there is a collision at the moment of a specified body with another body (or any body from a list or a group). If there is a collision, the puzzles in the "if touching do" slot are triggered, otherwise - in the "if not touching do".

The "detect collisions" puzzle triggers either of its callback slots every rendering frame.

Visual programming block for collision detection

The collision info puzzle outputs a dictionary with the following fields:

Visual programming block to retrieve collision info

Extending with JavaScript

Before you start reading, please get familiar with the Using JavaScript section of this User Manual.

To build physics puzzles we use a JavaScript library called Ammo.js. This library is basically the popular Bullet physics engine compiled to be used in the browser.

To get familiar with Bullet, check out the Bullet Physics SDK Manual available on GitHub as well as official Bullet API Reference.

In Verge3D you can execute Bullet/Ammo.js APIs directly by using the Ammo namespace:

var myVector = new Ammo.btVector3(1.0, 0.0, 0.0); console.log('My physics vector is:', myVector.x(), myVector.y(), myVector.z());

Also you can get access to the following data structures we use in Puzzles via the puzzles argument of the runCode() function of your_app_name.js module or the built-in puzzles variable inside the exec script puzzle:

World

Depending on the initialized physics world it can be an instance to btDiscreteDynamicsWorld or btSoftRigidDynamicsWorld class:

var gravity = puzzles.physics.world.getGravity(); console.log('World gravity:', gravity.y());

Physics Bodies

Physics bodies are stored as key-value data inside the puzzles.physics.bodies object:

var body = puzzles.physics.bodies['Whirligig']; console.log('Body mass:', 1.0 / body.getInvMass());

Rigid bodies are instances of the btRigidBody class, while soft bodies are instances of the btSoftBody class.

Physics Constraints

Bullet/Ammo constraints are stored as key-value data inside the two-dimensional puzzles.physics.constraints object:

var hingeConstraint = puzzles.physics.constraints['Suzanne']['Axis']; hingeConstraint.enableAngularMotor(true, 10, 10);

The names represent the constraint's first and second bodies respectively.

Physics Sync List

Verge3D puzzles use the so-called synchronization list to move objects represented by physics bodies in space. Sync list is not directly accessible, so you need to use the following API methods to add/remove your bodies to/from that list.

// add obj / body to the sync list // the type can be 'DYNAMIC', 'KINEMATIC', 'STATIC', 'GHOST', or 'SOFT_BODY' puzzles.physics.addToSyncList(obj, body, 'DYNAMIC'); // remove obj / body from the sync list puzzles.physics.removeFromSyncList(obj, body);

Having Troubles with Puzzles?

Seek help on the forums!