introduction to ammo.js
david evans
dave@playcanvas.com
what is ammo.js?
port of bullet, from C++ to javascript via emscripten
what is bullet?
open-source "full-fat" physics engine
why does physics matter?
games are about movement and interaction
physics engine essentials
or why you shouldn't write your own.
rigidbody
idealised representation of a body in which deformation is ignored.
but what is it?
a shape which doesn't change, and can be manipulated via certain properties.
position and orientation
linear velocity
angular velocity
forces and impulses
torques and torque impulses
rigidbody types
dynamic
static
kinematic
volumes and intersection
essential for:
collision detection
trigger volumes
crates
mesh
complex visuals, simple collision
essential for:
environment collision
broadphase
very quickly determine if two objects ARE NOT intersecting or colliding
narrowphase
accurately determine the nature of an intersection or collision
essential for:
speed!
ray-casting
collide a straight-line with a set of rigid-bodies, return the first or all intersections found.
essential for:
mouse interaction
AI vision
rail guns
trigger volumes
A hidden secret in ammo.js
btGhostObject - a collision object with no collision
stores intersecting pairs for efficient access
essential for:
triggers
goals
death pits
constraints
ball and socket
hinge
slider
essential for:
vehicles
physics-based puzzle games
altogether - ragdoll physics
special cases
character controller
dynamic character controller
move character indirectly by applying forces and impulses
physics engine resolves collision "realistically"
why not model a player with "real" physics?
often "real" physics is no fun
if (y_speed < 0 and y_speed is > -4 )
{
if (abs(x_speed) >= 0.125)
{
x_speed = x_speed * 0.96875
}
}
kinematic character controller
move the character by setting velocities directly
resolve collisions manually from penetration depths and position
designed to handle your game, e.g. auto-climb stairs
vehicles
simulated vehicle
4 wheels, hinge constraints, forces, torques
make sure you have plenty of time
raycast vehicle
var tuning = new Ammo.btVehicleTuning();
var vehicleRayCaster = new Ammo.btDefaultVehicleRaycaster(dynamicsWorld);
var vehicle = new Ammo.btRaycastVehicle(tuning, carChassis, vehicleRayCaster);
gotchas
stacking
lots of simultaneous collisions is hard.
don't expect a lot of stability.
penetration
fast-moving objects cause problems because of discrete collision detection
solution:
continuous collision detection
performance anxiety
asm.js - up to 1.3-1.7x native performance in firefox
no garbage collection in emscripten code
run in a web worker
thanks
dave@playcanvas.com
http://playcanvas.com/
slides: