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.
Find me on Google+
No comments:
Post a Comment