0

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.

Alexey
  • 714
  • 8
  • 21

2 Answers2

1

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

Aleksandrs
  • 1,488
  • 1
  • 14
  • 34
  • Maybe this: http://stackoverflow.com/questions/12998860/andengine-always-keep-the-same-speed-of-the-body/13005298#13005298 will be useful to you – Aleksandrs Nov 22 '12 at 09:13
0

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.

SVS
  • 200
  • 9
  • Thank you. With that I got the balls fall, once and throw away but I can not stand that they are moving around the screen continually colliding with each other and with the edges, you know as you would? – user1840088 Nov 23 '12 at 07:00
  • Check out the collision filtering example https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/PhysicsCollisionFilteringExample.java you should be able to make them only collide with certain edges and not with each others – Jimmar Nov 24 '12 at 23:37