Here is a small post on how I managed to simulate an Edge Shape in Android. I wanted to do this after I saw the Java demo of Box2D in JBox2D(see RayCast). For the Edge Shape I chose a cosine curve. I used the same method I used, mentioned in my earlier post “Making of the Total Control Android Game”

The simulation can be seen at Simulating an Edge Shape on YouTube

You can clone the entire project from Github at EdgeShape.

Since it is a cosine curve I place a ball shape for every point on a cosine function, close to each other to seem continuous and create a body from the Sprite with physical properties.

**for** (**int** i = 0; i < nBodies; ++i)

{

**float** angle = (**float**) ((10.0 * PI * i)/180.0);

**float** y1 = 300 + (**float**)Math.cos(angle/20) * 120;

//Log.d(“Test”,”Test” + “x:” + x1 +”y:”+ y1);

circles[i] = **new** Sprite(x1, y1, **this**.mBallTextureRegion, **this**.getVertexBufferObjectManager());

circlesBody[i] = PhysicsFactory.createCircleBody(**this**.mPhysicsWorld, circles[i], BodyType.StaticBody, FIXTURE_DEF);

**this**.mScene.attachChild(circles[i]);

x1 = (**float**) (x1 + 0.5);

}

So this can be done for every mathematical curve. I also intended to create other curves like the Archimedes Spiral and the Lemniscate. Maybe I will leave that for a rainy day!! 🙂

However, I am not sure how to create a irregular edge shape. I will probably figure that out.

The egde shape is made to be a STATIC_BODY. I then took code of creating Animated Sprites at the point where I touch from AndEngine examples (PhysicsExample.java). To make the simulation more fun I also added a Linear Impulse to the Animated Sprite.

face = **new** AnimatedSprite(pX, pY, **this**.mCircleFaceTextureRegion, **this**.getVertexBufferObjectManager());

body = PhysicsFactory.*createCircleBody*(**this**.mPhysicsWorld, face, BodyType.*DynamicBody*, gameFixtureDef);

face.animate(200);

body.applyLinearImpulse(-200,200,pX,pY);

The effect is better if the phone is horizontal to the ground and not so great if the phone is kept vertical and the bodies do not seem to shoot off.

The simulation can be seen at Simulating an Edge Shape

You can clone the entire project from Github at EdgeShape.

Take a look at some cool simulations using AndEngine & Box2D

1. Simulating the domino effect using Box2D and AndEngine

2. Blob with an attitude(stiffness) in Android

3. The making of Total Control Android game

4. Simulating a Web Joint in Android

20. A closer look at “Robot horse on a Trot! in Android”

and many more …

Find me on Google+

## 13 thoughts on “Simulating an Edge Shape in Android”