Up until now, the physics engine used by Physion was Box2D. Box2D is an open source 2D physics engine written in C++ and it is used in many games and apps.
Although Box2D is a mature physics engine with many features, it doesn't "natively" support fluid simulation. After some research on the web, I came across LiquidFun. As mentioned in LiquidFun's website:
LiquidFun is an extension of Box2D. It adds a particle based fluid and soft body simulation to the rigid body functionality of Box2D.
As it turns out, transitioning from Box2D to LiquidFun is quite easy. All the features supported in Box2D are also supported in LiquidFun so there's no need for any major code changes when switching to LiquidFun.
Although LiquidFun supports a rich set of features, we will start simple; the transition to LiquidFun will have one simple goal: Allow the user to convert a rigid body into a liquid or an elastic (soft) body.
When a rigid body is "liquified" or "elastified" it will be removed from the Scene and in its place a new node will be added. This new node will contain all the particles representing the liquified or elastified body.
Liquify / Elastify Tools
The first change that was implemented was to add two new tools in the toolbars.
These two new tools are:
- Liquify Tool
- Elastify Tool
The user can select any of these tools and then click on a rigid body in the Scene in order to liquify or elastify it.
Some notes:
- The visual feedback of these new tools is minimal (just a single sprite)
- These tools will only allow liquifying/elastifying a single body at a time (i.e. single click selection)
Liquify / Elastify Commands
Both of these new commands will work in a very similar manner:
- They will create a new
LiquidNode
orElasticNode
using the selected body's: transformed shape, linear velocity and fill color. - They will remove the selected body (and its dependencies) from the Scene.
- They will insert the new
LiquidNode
orElasticNode
in the Scene.
Note that in contrast with most commands that append new nodes in the Scene, these commands will insert the new Node instead. The reason this is done is to have all liquids and soft bodies stacked behind rigid bodies (which looks more realistic).
Scene Updates
The main change in the Scene
node is the addition of a particle system.
This means that when a new Scene is constructed a new particle system will be constructed with it.
All liquids and elastic bodies that will be created by the user will belong to this particle system.
A particle system has many options that can be defined but for the time being these options will
not be exposed to the user nor they will be saved in the Scene (this might be done in the future).
Also, a new property was added in the Scene.
Its name is particleIterations
and its default value is 3
.
The higher this value is the better (more accurate) the particle simulation will be.
ParticleGroupNode
The ParticleGroupNode
serves as the base class for both LiquidNode and ElasticNode.
As the name suggests, this node represents a group of particles.
Each particle is round, has a fixed rotation and has the following properties:
- Position
- Velocity
- Color
Because both of the liquify and elastify commands produce lots of particles (typically in the range of thousands)
we need to better handle how the ParticleGroupNode
is saved. In order to solve this it was decided to use
(lz) compression when packing the Scene.
Some general key points for the ParticleGroupNode
are:
- Its x, y and angle properties are always zero and cannot be changed.
- Its bounding rectangle is defined by the positions of the particles it contains.
Selection
As mentioned above, the ParticleGroupNode
is a group of particles.
For the time being, the user can only select the group as a whole and individual particles within the group cannot be selected.
Rendering
Ideally, for rendering the particles we would use shaders but since shaders are not yet supported in Physion we will use a simpler approach. To render each particle a single sprite will be used.
The texture for this sprite looks like:
Limitations
- A
ParticleGroupNode
cannot be moved or mirrored.
Future Work
- Use shaders to better visualize liquid and elastic bodies.
- Expose more LiquidFun properties (Particle System/Group) in the UI.