Skip to content

Physics ​

You can easily add physics to existing objects using the UI or with the code. Uva uses a custom physics engine based off Cannon ES, it's highly optimised, simple and efficient.

Adding physics to an object: ​

You can set an object to use physics in the UI with:

Assets

or using code:

js
const physicsOptions = {
	type: 'Static|Kinematic|Dynamic',
	shape: 'Plane|Sphere|Box|Hull|Mesh',
	mass: [optional] number,
	angularDamping: [optional] number,
	linearDamping: [optional] number,
	friction: [optional] number,
	restitution: [optional] number // bounceyness
}

const object = new THREE.Mesh(geometry, material)

world.physics.addBody(object, physicsOptions)

Type ​

Static

Use this for physics objects which don't move e.g. floors, walls.

Dynamic

Objects that move naturally with physics - they bounce, fall, and react to forces on their own. The physics engine handles all their movement automatically.

Kinematic

Objects you control directly that can push physics objects around but aren't affected by gravity or forces themselves. You position these manually in your code.

Shape ​

Hull

This is the most efficient way to make a mesh have physics, and uses cannon's ConvexPolyhedron

Mesh

Typically has not as good performance compared to hull, however not all geometry will work with a hull. If you have issues with hulls use this instead.

Box / Sphere

These are automatically used for primitive geometry. You can make meshes use these too if you need efficiency with less accuracy.

Plane

This is an infinite plane, useful for prototyping, and for scenes with a constant floor height.

Heightfield

Not yet implemented, used for floor terrain.

Removing physics from an object ​

js
world.physics.remove(object)

Modifying a physics object ​

When an objects has physics enabled it has an extra object.physics property. You can use this for updating the object at runtime:

js
object.physics.setPosition(new THREE.Vector3(0, 1, 3))

// move the object towards a position, call this each frame frame
object.physics.moveTo(
	new THREE.Vector3(0, 1, 3),
	speed : number[optional],
	maxSpeed : number[optional]
)


// You can set any property of a Cannon Body, e.g.
object.physics.setProperty(new THREE.Quaternion(0, 0, 0, 1), 'quaternion')
object.physics.setProperty(new THREE.Vector3(0, 0.5, 0), 'linearVelocity')
object.physics.setProperty(0.55, 'friction')


// Kinematic objects
object.physics.setForce(new THREE.Vector3(0, 1, 3))


object.physics.sleep() // pause physics simulation
object.physics.setAwake() // resume physics simulation

Setting gravity ​

js
// Gravity is set to earths 9.81 by default, you can change by using:
world.physics.setGravity(7.0)

Resetting physics ​

js
// Remove all physics objects in a scene
world.physics.clear()