Creating a Blob in Android using Box2D physics engine and AndEngine

DSC00037Here is a short post on my attempt to create a Blob using Box2D physics engine and AndEngine. This demo tries to recreate the Blob Joint at GwtBox2D Showcase. This Blob Joint demo in Java uses a ConstantVolume Joint for creating the Blob. For my blob I use a distanceJoint for maintaining the shape of the Blob.

Here is the clip of the blob in action : Blob clip
You can clone the project from Github from the Blob code

A Blob is created in the initial shape of an ellipse as follows
// Add 20 circle bodies around an ellipse
for (int i=0; i<nBodies; ++i) {
FIXTURE_DEF = PhysicsFactory.createFixtureDef(30f, 0.5f, 0.5f)
Vector2 v1 = new Vector2(x1,y1);
final VertexBufferObjectManager vb = this.getVertexBufferObjectManager();
circle[i] = new AnimatedSprite(x1, y1, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
circleBody[i] = PhysicsFactory.createCircleBody(this.mPhysicsWorld, circle[i], BodyType.DynamicBody, FIXTURE_DEF);


}

A distance Joint is created between every body as follows

// Create a distanceJoint between every other day
for(int i= 0;i < nBodies-1; i++) {
for(int j=i+1; j 0) {
connectionLine[i] = new Line(centers[i][0],centers[i][1],centers[i-1][0],centers[i-1][1],lineWidth,this.getVertexBufferObjectManager());
connectionLine[i].setColor(0.0f,0.0f,1.0f);
this.mScene.attachChild(connectionLine[i]);
}

// Join the first body with the last body
if(i == 19){
connectionLine[0] = new Line(centers[0][0],centers[0][1],centers[19][0],centers[19][1],lineWidth,this.getVertexBufferObjectManager());
connectionLine[0].setColor(.0f,.0f,1.0f);
this.mScene.attachChild(connectionLine[0]);
}

The connecting lines move along with the moving shapes as below
// Update connection line so that the line moves along with the body
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(circle[i], circleBody[i], true, true) {
@Override
public void onUpdate(final float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
for(int i=1;i < nBodies;i++) {
connectionLine[i].setPosition(circle[i].getX(),circle[i].getY(),circle[i-1].getX(),circle[i-1].getY());

} connectionLine[0].setPosition(circle[0].getX(),circle[0].getY(),circle[19].getX(),circle[19].getY());
}
}
);

So here is the clip of the blob in action : Blob clip
You can clone the project from Github from the Blob code

Some cool simulations using AndEngine & Box2D
1. Simulating the domino effect using Box2D and AndEngine
2. Simulating a Web Joint in Android
3. Modeling a Car in Android
4. Fun simulation of a Chain in Android
5. A closer look at “Robot horse on a Trot! in Android”
and many more
Find me on Google+

3 thoughts on “Creating a Blob in Android using Box2D physics engine and AndEngine

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s