0

I am creating a 'Chain Reaction' type game.

In onLoadResources I create 4 static bodies (ground, roof, left border and right border) to act as boundaries. I then create 30 dynamic bodies (which are balls that bounce around).

My question is, how can I detect which ball has just collided with a boundary?

The way I have it right now, I believe the last 'particle_body' that is created, is the only one that is ever modified during a collision.

Any suggestions??

Here is my code:

this.mPhysicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0, 0), false); // Gravity (x, y) scene.registerUpdateHandler(mPhysicsWorld);

    // Create the ground
    groundFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f);
    ground = new Rectangle(0, CAMERA_HEIGHT-1, CAMERA_WIDTH, CAMERA_HEIGHT);
    ground.setColor(0.0f, 0.0f, 0.0f);
    ground_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.ground, BodyType.StaticBody, groundFixture);
    ground_body.setUserData("ground");
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ground, ground_body, true, false));
    scene.attachChild(ground);

    // Create the roof
    roofFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f);
    roof = new Rectangle(0, 0, CAMERA_WIDTH, 1);
    roof.setColor(0.0f, 0.0f, 0.0f);
    roof_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.roof, BodyType.StaticBody, roofFixture);
    roof_body.setUserData("roof");
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(roof, roof_body, true, false));
    scene.attachChild(roof);

    // Create the left border
    leftBorderFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f);
    leftBorder = new Rectangle(0, 0, 1, CAMERA_HEIGHT);
    leftBorder.setColor(0.0f, 0.0f, 0.0f);
    leftBorder_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.leftBorder, BodyType.StaticBody, leftBorderFixture);
    leftBorder_body.setUserData("leftBorder");
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(leftBorder, leftBorder_body, true, false));
    scene.attachChild(leftBorder);

    // Create the right border
    rightBorderFixture = PhysicsFactory.createFixtureDef(1, 1.0f, 0.5f);
    rightBorder = new Rectangle(CAMERA_WIDTH-1, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    rightBorder.setColor(0.0f, 0.0f, 0.0f);
    rightBorder_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.rightBorder, BodyType.StaticBody, rightBorderFixture);
    rightBorder_body.setUserData("rightBorder");
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rightBorder, rightBorder_body, true, false));
    scene.attachChild(rightBorder);

    // Prepare a random number generator
    Random rand = new Random();

    // Create Particles
    for(int i=0; i < 30; i++)
    {
        int x = rand.nextInt(CAMERA_WIDTH) - 1;
        int y = rand.nextInt(CAMERA_HEIGHT) - 1;
        this.particle = new Particle(x, y, this.mPurpleParticleTextureRegion, 100);
        particle.setScale(0.3f);
        scene.attachChild(particle);

        particleFixture = PhysicsFactory.createFixtureDef(10, 0.9f, 0.1f);
        particle_body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.particle, BodyType.DynamicBody, particleFixture);
        particle_body.setUserData("particle_" + i);
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this.particle, particle_body, true, false));

        // Assign a random velocity to the particle
        x = rand.nextInt(20) + 3;
        y = rand.nextInt(20) + 3;   
        particle_body.setLinearVelocity(new Vector2(x, y));  // x, y impulse

        mParticleList.add(particle);
        mBodyList.add(particle_body);
        mFixtureList.add(particleFixture);
        particleNumAR[i] = i;
    }

    // Create the Physics Collision Detection listener
    mPhysicsWorld.setContactListener(new ContactListener() 
    {    
        public void endContact(Contact contact)
        {
        }

        public void beginContact(Contact contact)
        {

            if (contact.getFixtureA().getBody().getUserData() != null &&
                contact.getFixtureB().getBody().getUserData() != null) 
            {
                final String objA = (String)contact.getFixtureA().getBody().getUserData();
                final String objB = (String)contact.getFixtureB().getBody().getUserData();

                // Particle hits ground
                if (objA.equals("ground") || objB.equals("ground")) 
                {
                        Random rand2 = new Random();

                        int xx = rand2.nextInt(20) + 3;
                        int yy = rand2.nextInt(10) + 3;   
                        particle_body.setLinearVelocity(new Vector2(xx, yy));  // x, y impulse
                }

                // Particle hits roof
                if (objA.equals("roof") || objB.equals("roof")) 
                {
                        Random rand2 = new Random();

                        int xx = rand2.nextInt(20) + 3;
                        int yy = rand2.nextInt(10) + 3;   
                        particle_body.setLinearVelocity(new Vector2(xx, yy));  // x, y impulse
                }

                // Particle hits left border
                if (objA.equals("leftBorder") || objB.equals("leftBorder")) 
                {
                        Random rand2 = new Random();

                        int xx = rand2.nextInt(10) + 3;
                        int yy = rand2.nextInt(20) + 3;   
                        particle_body.setLinearVelocity(new Vector2(xx, yy));  // x, y impulse
                }

                // Particle hits right border
                if (objA.equals("rightBorder") || objB.equals("rightBorder")) 
                {
                        Random rand2 = new Random();

                        int xx = rand2.nextInt(10) + 3;
                        int yy = rand2.nextInt(20) + 3;   
                        particle_body.setLinearVelocity(new Vector2(xx, yy));  // x, y impulse
                }
            }
        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) 
        {
        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) 
        {
        }
    });

1 Answers1

0

You can use the other fixture of the contact. One of the fixtures will be one of your walls. The other fixture will be your particle that collided.

So you could do something like this (untested pseudo code):

// I don't know what class the Box2D body is in this case (b2Body maybe? or just Body?)
// so please insert appropriate type

final String objA = (String)contact.getFixtureA().getBody().getUserData();
Body bodyA = contact.getFixtureA().getBody();
final String objB = (String)contact.getFixtureB().getBody().getUserData();
Body bodyB = contact.getFixtureB().getBody();


Body particleBody = objA.startsWith("particle_") ? bodyA : bodyB; 

...

// then use particleBody for setting the velocity in your different if cases

particleBody.setLinearVelocity(new Vector2(x,y));
Dennis Korbar
  • 500
  • 2
  • 7