Inside LiquidFun
Kentaro Suto
Senior Software Engineer
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
Overview of LiquidFun
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
Particle simulation
Data structures
Particle data
flags
positions
velocities
...
Particle group
flags
first index
last index
...
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
Simulation Algorithm
Box2D simulation
Interaction bt particles & Box2D
Particle simulation
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.
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
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
Collision detection -- particle sort
x
y
Sort by floor(y/D)
D
Collision detection -- particle sort
1
2
4
7
9
3
5
6
8
x
y
D
Then sub-sort by x
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
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.
Apply Pressure
Sum up the weight of contacts for each particle
w[i] = sum(w[i,j],j)
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
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]
Apply pressure
compression
pressure
force
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
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
Apply Elastic Force
If a particle is elastic, apply force to restore relative positions among neighboring particles
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
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
Restrict Particle Velocity
For wall particles, set velocity to zero.
v[i] ← 0
Wall Particles
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
Ω
Move Particles
Use velocity to update particle positions
p[i] ← p[i] + Δt * v[i]
Δt*v[i]
p[i]
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
Prevent penetration
Δt * v[i]
Δt * v[i]
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
Continuous contact
contact with a pretend particle
n
n
Summary