This is the source code for a rubber image, similar to what is found in apps like FaceGoo and FatBooth.
It sets up a regular grid of physical points, and maps a texture onto it. Each point has a position, velocity and a rest position. Use interaction modifies the velocities of the points, and elastic forces try to pull the point back to its rest position. There are no forces between adjacent points so it is extremely fast.
For this iOS example, the setup code and texture loading are written in Objective C, but the pertinent code is C++, so it is very portable and can be compiled for almost any system.
In addition, it uses OpenGLES 1, making it compatible with all devices.
The onscreen sliders control the elasticity and damping.
Try these values:
Elasticity = 0%, Damping = 100% - Smear the image like in FaceGoo (plastic behaviour)
Elasticity = 100%, Damping = 0% - Jelly image that never stops wobbling (elastic behaviour)
In the code you will find two classes:
v2 is a useful 2D vector class with overloaded operators.
RubberImage creates an array of physical points and creates a triangle mesh, using
GL_TRIANGLE_STRIP for greatest speed.
To include in your own app:
1) Copy v2.h, v2.cpp, RubberImage.h and RubberImage.cpp to your own project.
2) Make sure you have an OpenGL canvas with proper view matrices and rendering states.
3) Create a RubberImage object
RubberImage *myRubberImage = new RubberImage();
4) Initialise it
myRubberImage->init( NUMBER_OF_HORIZONTAL_GRID_POINTS, NUMBER_OF_VERTICAL_GRID_POINTS, SCALE_OF_GRID );
5) Load and set the texture
myRubberImage->setTextureHandle( TEXTURE_HANDLE );
6) Set the elasticty and damping
myRubberImage->setElasticity( ELASTICITY_VALUE );
myRubberImage->setDamping( DAMPING_VALUE );
7) Apply perturbations
myRubberImage->applySmudgeForce( TIMESTEP, POSITION, VELOCITY, RADIUS );
8) Update the mesh
myRubberImage->update( TIMESTEP );
9) Draw the mesh
to clean up
10) Delete the object