user1840088
user1840088

Reputation: 9

Collision between multiple balls andegine android

I using andengine for android and I have multiple balls that bounce between them and the limits of the screen. The problem is that what I do when collide is reverse the direction X and Y so that the bouncing effect is not real, how can you do?

@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
    if(this.mX < 0) {
        this.mPhysicsHandler.setVelocityX(DEMO_VELOCITY);
    } else if(this.mX + this.getWidth() > CAMERA_WIDTH) {
        this.mPhysicsHandler.setVelocityX(-DEMO_VELOCITY);
    }

    if(this.mY < 0) {
        this.mPhysicsHandler.setVelocityY(DEMO_VELOCITY);
    } else if(this.mY + this.getHeight() > CAMERA_HEIGHT) {
        this.mPhysicsHandler.setVelocityY(-DEMO_VELOCITY);
    }

    for (int i = 0; i < Bolas.size(); i++) {
        if (this.collidesWith(Bolas.get(i)) && Bolas.get(i).equals(this) == false) {                               
            // ????????????????????????????????                      
            this.mPhysicsHandler.setVelocityY((-1) * this.mPhysicsHandler.getVelocityY());
            this.mPhysicsHandler.setVelocityX((-1) * this.mPhysicsHandler.getVelocityX());                   
        }
    }                 

    super.onManagedUpdate(pSecondsElapsed);
}

Thank you.

Upvotes: 0

Views: 888

Answers (2)

SVS
SVS

Reputation: 200

You should use the AndEnginePhysicsBoxExtension. You'll be able to set density, elasticity, friction, etc, and have your own collision handler. I like the following example: https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/BasePhysicsJointExample.java

Also I recommend you to read about BodyCollisionHandler.

Upvotes: 0

Aleksandrs
Aleksandrs

Reputation: 1509

If I understood you correctly, than you can use Body (box2d) for your sprites:

Sprite yourSprite = new Sprite(pX, pY, this.mYourSpriteTextureRegion);
Body yourSpriteBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, yourSprite , BodyType.DynamicBody, FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(yourSprite , yourSpriteBody , true, true));

bouncing will be automaticaly.

look in this example

Upvotes: 1

Related Questions