Physics

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

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.

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.

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.

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.

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.

Parameters:

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.

Parameters:

physics body params

This puzzle is now deprecated. Please use apply body param instead.

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.

Parameters:

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

apply vector

This puzzle is now deprecated. Please use apply body param instead.

Applies Force, Gravity, Linear Velocity, Angular Velocity, Impulse, Torque, Torque Impulse or Position to the physics body associated with a specified object, in a specified direction in world space. Also works for a list of objects, a group (or a list of groups) or with the all objects puzzle.

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.

Parameters:

on simulation tick

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

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.

Works similar to apply body param / position puzzle.

add constraint to

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

Constraint types:

remove constraint from

Remove physics constraint.

anchor soft body

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

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.

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

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:

World

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

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

Physics Bodies

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

var body = v3d.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 v3d.puzzles.physics.constraints object:

var hingeConstraint = v3d.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' v3d.puzzles.physics.addToSyncList(obj, body, 'DYNAMIC'); // remove obj / body from the sync list v3d.puzzles.physics.removeFromSyncList(obj, body);

Having Troubles with Puzzles?

Seek help on the forums!