John Jared
John Jared

Reputation: 800

Android AndEngine two circles collision perfectly

Android AndEngine two circles collision perfectly. I have two circle and a collision method for them, I want when they touch each other the collision happens, currently when they near each other the collision happens.

I think that it is because of the transparent free space in the .png file of each circle.

enter image description here

In the picture you can see that now they collide from a distance, I want when both touch each other.

My collision method:

                            if (circle1.collidesWith(circle)){
                                Score += 1;
                            }

Upvotes: 0

Views: 517

Answers (2)

Shihab Uddin
Shihab Uddin

Reputation: 6931

If you are not in Box2d , You must use Pixel-Perfect Collision library. Well default AndEngine Library, Does not support pixel perfect collision. To get this support, you need to import this library in eclipse and add this to your project uses library.

Here, I Demonstrate how to use this library. When you define Texture and Atlas for your sprite write as below.

 private BitmapTextureAtlas lifeAtlas;
 public PixelPerfectTiledTextureRegion life_Texture;

PixelPerfectTextureRegionFactory.setAssetBasePath("gfx/game/");

lifeAtlas = new BitmapTextureAtlas(textureManager, 1280, 128,
                    TextureOptions.BILINEAR);

 life_Texture = PixelPerfectTextureRegionFactory.createTiledFromAsset(
                    lifeAtlas, activity, "heart_tiled.png", 0, 0, 10, 1, 0);

    lifeAtlas.load();

For your custom sprite class,

public class Plane extends PixelPerfectAnimatedSprite {

public Plane(float pX, float pY,
        PixelPerfectTiledTextureRegion pTiledTextureRegion,
        VertexBufferObjectManager pVertexBufferObjectManager) {

    super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
    setColor(Color.GREEN);
}

}

You also need some adjustment with your AndEngine library to use it. Follow this thread to go.

Upvotes: 1

Mateusz Gaweł
Mateusz Gaweł

Reputation: 683

I am almost sure you are right that transparent places in png causes it. You probably creating BoxBody. In your case you should use circle body like this:

Body circleBody = PhysicsFactory.createCircleBody(pWorld, pSprite, BodyType.StaticBody, FixtureDef);

If it doesn't help there is method overload where you can provide position and size of the body. I can recommend you using DebugRender which you only have to attach to scene:

new DebugRenderer(physicsWorld, vbom)

When u use this you will see how helpful it can be:) Just remember that it may slowdown your phone when you have a lot of bodies on the scene.

PS. You didn't give us a lot of information but you should use contactListener to check colisions. There are plenty of tutorials in the internet for it

PS2. If you don't use Box2D extension - do it. This is great feature of AndEngine and it's pointless to implement that for yourself. It will be hard to detect circle shape collision of 2 objects without Box2D.

Upvotes: 1

Related Questions