1 of 32

Inside LiquidFun

Kentaro Suto

Senior Software Engineer

Google

2 of 32

Contents

Overview of LiquidFun

What is particle simulation?

Data structures

Simulation Algorithm

An open source game that uses LiquidFun

Sample LiquidFun app for Android and IOS

EyeCandy

LiquidFun Paint

3 of 32

Overview of LiquidFun

  • 2D rigid body and fluid simulation library
  • Extension of Box2D
  • Written in C++, bindings for Java, translated to JavaScript
  • Runs on Android, iOS, Windows, OS X, Linux, browsers
  • Distinguishing feature: particle simulation

4 of 32

What is particle simulation?

In particle simulation...

objects are composed of particles (circles, in LiquidFun)

physics is described by interactions between particles

Particle simulation works well with...

Fluids--liquids and gasses

Deformable objects

Interactions of different materials

5 of 32

Particle simulation

6 of 32

Data structures

Particle data

flags

positions

velocities

...

Particle group

flags

first index

last index

...

7 of 32

Data structures

position

velocity

group list

...

next

next

b2Vec2

b2Vec2

b2ParticleGroup

b2ParticleSystem

p[0]

p[1]

p[2]

v[0]

v[1]

v[2]

...

...

index

index

8 of 32

Simulation Algorithm

Box2D simulation

Interaction bt particles & Box2D

Particle simulation

9 of 32

Legend

p[i]

v[i]

Δt

R

D

position of particle i

velocity of particle i

simulation time step

particle radius

particle diameter = 2*R

These symbols will be used in the next few slides.

10 of 32

Particle simulation

Collision detection

Apply pressure

Apply additional forces (viscous, etc.)

Restrict particle velocity (rigid, wall, etc.)

Move particles

The wave machine, from LiquidFun’s testbed

Wave Machine

11 of 32

Collision detection

Find pairs of particles closer than the particle diameter

Record the following values:

i,j: indices of colliding particles

n[i,j]: normalized relative position

w[i,j]: calculated as 1-distance/diameter

0 if barely contacting, 1 if overlapping

distance

R

diameter=2R

12 of 32

Collision detection -- particle sort

x

y

Sort by floor(y/D)

D

13 of 32

Collision detection -- particle sort

1

2

4

7

9

3

5

6

8

x

y

D

Then sub-sort by x

14 of 32

Collision detection -- broad pass collision detection

1

2

4

7

9

3

5

6

8

x

y

The orange particle might collide with the blue particles

15 of 32

Collision detection -- broad pass collision detection

1

2

4

7

9

3

5

6

8

x

y

We consider only particles to the right and below.

This avoids duplicate collision pairs.

16 of 32

Apply Pressure

Sum up the weight of contacts for each particle

w[i] = sum(w[i,j],j)

17 of 32

Apply Pressure

Calculate the pressure of each particle using the weight sum

h[i] = max(0,η*(w[i]-w0))

h[i]

η

w0

pressure of particle i

constant coefficient of weight

constant average weight

18 of 32

Apply Pressure

Apply a repulsive force to each contacting particle according to pressure

v[i] ← v[i] + Δt * a * (h[i] + h[j]) * w[i,j] * n[i,j]

a: constant coefficient of repulsion

h[i]

h[j]

19 of 32

Apply pressure

compression

pressure

force

20 of 32

Apply Viscous Force

If a particle is viscous, mix velocity with its contacting particles

v[i] ← v[i] + Δt * μ * (v[j] - v[i])

μ : constant coefficient of viscosity

21 of 32

Apply Spring Force

If a particle is springy, apply force to restore distance between neighboring particles

v[i]←v[i] + Δt * k * (distance(p[j],p[i]) - L[i,j]) * n[i,j]

k

L[i,j]

constant spring coefficient

initial distance

22 of 32

Apply Elastic Force

If a particle is elastic, apply force to restore relative positions among neighboring particles

23 of 32

Apply Powder Force

If a particle is powder, apply simple potential force instead of pressure

v[i]←v[i] + Δt * c * max(0, w[i,j] - w1) * n[i,j]

c: constant potential coefficient

w1: constant threshold weight

24 of 32

Apply Tensile Force

If a particle is tensile, apply force to simulate

surface tension

w[i] = sum(w[i,j], j)

s[i] = sum((1 - w[i,j]) * w[i,j] * n[i,j], j)

A[i] = a * (w[i] + w[j] - 2 * w0)

B[i] = b * dot(s[j] - s[i], n[i,j]))

v[i] ← v[i] - Δt * (A[i] + B[i]) * n[i,j]

a,b: constant coefficients

w0: constant average weight

Tensile Particles

25 of 32

Restrict Particle Velocity

For wall particles, set velocity to zero.

v[i] ← 0

Wall Particles

26 of 32

Restrict Particle Velocity

For particles in a rigid group, set velocities to maintain relative positions.

v[i]←V+cross(Ω,p[i]-P)

V: linear velocity of the group

Ω: angular velocity of the group

P: center of mass of the group

V

P

Ω

27 of 32

Move Particles

Use velocity to update particle positions

p[i] ← p[i] + Δt * v[i]

Δt*v[i]

p[i]

28 of 32

Interaction with Box2D

Prevent penetration:

If a particle is about to penetrate a Box2D body during this step,

stop the particle at the surface

Box2D Bodies

29 of 32

Prevent penetration

Δt * v[i]

Δt * v[i]

30 of 32

Interaction with Box2D

If a particle is in contact with a Box2D body, execute the subsequent particle simulation as if the particle is in contact with another particle

Box2D Bodies

31 of 32

Continuous contact

contact with a pretend particle

n

n

32 of 32

Summary

  • Particle systems are suitable for fluids, deformable objects, and interactions between different materials
  • Collision testing is made tractable by ordering the particles top-to-bottom, left-to-right
  • Viscous, spring, elastic, powder, and tensile forces can be added to create different kinds of particles