Example usage for com.badlogic.gdx.physics.box2d Contact setEnabled

List of usage examples for com.badlogic.gdx.physics.box2d Contact setEnabled

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d Contact setEnabled.

Prototype

public void setEnabled(boolean flag) 

Source Link

Document

Enable/disable this contact.

Usage

From source file:com.agateau.pixelwheels.bonus.Bullet.java

License:Open Source License

@Override
public void preSolve(Contact contact, Fixture otherFixture, Manifold oldManifold) {
    if (isFinished()) {
        return;/*from   w  w  w .j a va 2  s  .c  o  m*/
    }
    Object other = otherFixture.getBody().getUserData();
    if (other == mShooter) {
        contact.setEnabled(false);
        return;
    }

    explode();
    if (other instanceof Racer) {
        ((Racer) other).spin();
    } else if (other instanceof Explosable) {
        ((Explosable) other).explode();
    }
}

From source file:com.agateau.pixelwheels.bonus.Missile.java

License:Open Source License

@Override
public void preSolve(Contact contact, Fixture otherFixture, Manifold oldManifold) {
    if (isFinished()) {
        return;// ww w.  j av  a2 s  .  c o m
    }
    if (mStatus == Status.WAITING) {
        contact.setEnabled(false);
        return;
    }
    Object other = otherFixture.getBody().getUserData();
    if (other == mShooter) {
        contact.setEnabled(false);
        return;
    }

    explode();
    if (other instanceof Racer) {
        ((Racer) other).spin();
    } else if (other instanceof Explosable) {
        ((Explosable) other).explode();
    }
}

From source file:com.agateau.pixelwheels.racer.Racer.java

License:Open Source License

@Override
public void preSolve(Contact contact, Fixture otherFixture, Manifold oldManifold) {
    Object other = otherFixture.getBody().getUserData();
    mAudioComponent.onCollision();//from w  w w . j  a  va  2s .co  m
    if (other instanceof Racer) {
        contact.setEnabled(false);
        applySimplifiedRacerCollision((Racer) other);
    }

    for (Collidable collidable : mCollidableComponents) {
        collidable.preSolve(contact, otherFixture, oldManifold);
    }
}

From source file:com.bensoft.mulg2.model.tiles.DoorStoneTileModel.java

@Override
public void preSolve(Contact contact, Manifold manifold, Model<?> otherElem) {
    if (open) {/*from   w w  w .  j  ava  2s .  co  m*/
        contact.setEnabled(false);
    }
    super.preSolve(contact, manifold, otherElem);
}

From source file:com.bensoft.mulg2.model.tiles.OneWayStoneTileModel.java

@Override
public void preSolve(Contact contact, Manifold manifold, Model<?> otherElem) {
    super.preSolve(contact, manifold, otherElem);
    contact.setEnabled(!allowThrough);
}

From source file:com.thirdgame.shootkillninjabomb.GameLayer.java

License:Open Source License

private ContactListener createContactListener() {
    ContactListener contactListener = new ContactListener() {

        @Override/*w ww.  ja  va  2  s .com*/
        public void beginContact(final Contact pContact) {

            final Fixture fixtureA = pContact.getFixtureA();
            final Fixture fixtureB = pContact.getFixtureB();

            final Body bodyA = fixtureA.getBody();
            final Body bodyB = fixtureB.getBody();

            final UserData userDataA = (UserData) bodyA.getUserData();
            final UserData userDataB = (UserData) bodyB.getUserData();

            if (userDataA == null || userDataB == null) {
                return;
            }
            isTouch = false;
            isWallsTouch = 0;
            if ((userDataA.type == "Floor" && userDataB.type == "Shape")
                    || (userDataB.type == "Floor" && userDataA.type == "Shape")) {
                i = 1;
                isWallsTouch = 1;
                if (userDataA.type == "Floor") {
                    //      bodyB.setActive(false);
                    userDataB.type = "Remove";
                    if (GameScene.gameScene.countLives() > 1)
                        GameScene.gameScene.setGameOver();
                } else {
                    //      bodyA.setActive(false);
                    userDataA.type = "Remove";
                    if (GameScene.gameScene.countLives() > 1)
                        GameScene.gameScene.setGameOver();
                }
            }
            if (((userDataA.type == "Floor" || userDataA.type == "Wall") && userDataB.type == "Bullet")
                    || ((userDataB.type == "Floor" || userDataB.type == "Wall")
                            && userDataA.type == "Bullet")) {

                if (userDataA.type == "Bullet") {
                    userDataA.type = "Remove";
                    if (userDataA.count > 0)
                        GameScene.gameScene.setBulletHitCount();
                    if (userDataA.count < 2) {
                        isWallsTouch = 1;
                        mComboCount = 0;
                    }
                    if (mComboCount > 1 && userDataA.count > 1) {
                        touchCount += (mComboCount - 1) * 4;
                        mGameScene.fireComboParticle(mComboCount - 1);
                    }
                } else {
                    if (userDataB.count < 2) {
                        mComboCount = 0;
                        isWallsTouch = 1;
                    }
                    if (userDataB.count > 0)
                        GameScene.gameScene.setBulletHitCount();
                    if (mComboCount > 1 && userDataB.count > 1) {
                        touchCount += (mComboCount - 1) * 4;
                        mGameScene.fireComboParticle(mComboCount - 1);
                    }
                    userDataB.type = "Remove";
                }

            }
            if ((userDataA.eggType == "redyellowegg" || userDataB.eggType == "redyellowegg")
                    && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) {

                isTouch = true;
                //   mGameScene.setScore(120);
                mGameScene.fireParticle(fixtureA);
                i = 1;
                isWallsTouch = 2;
            }
            if ((userDataA.eggType == "brownskyblueegg" || userDataB.eggType == "brownskyblueegg")
                    && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) {
                isTouch = true;
                isWallsTouch = 2;
                //   mGameScene.setScore(900);
                mGameScene.fireParticleOne(fixtureA);
                i = 1;
            }
            if ((userDataA.eggType == "orangeeyellowegg" || userDataB.eggType == "orangeeyellowegg")
                    && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) {
                isTouch = true;
                isWallsTouch = 2;
                mGameScene.fireParticleTwo(fixtureA);
                i = 1;
                //   mGameScene.setScore(550);
            }
            if ((userDataA.eggType == "blueeegreenegg" || userDataB.eggType == "blueeegreenegg")
                    && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) {
                isTouch = true;
                isWallsTouch = 2;
                mGameScene.fireParticleThree(fixtureA);
                i = 1;
            }
            if ((userDataA.eggType == "newblueyellow" || userDataB.eggType == "newblueyellow")
                    && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) {
                isTouch = true;
                isWallsTouch = 2;
                mGameScene.fireParticleFour(fixtureA);
                i = 1;
                getAudioPlayer().play(SoundKeys.MULTIPLIER);
            }
            if ((userDataA.eggType == "newblueredegg" || userDataB.eggType == "newblueredegg")
                    && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) {
                isTouch = true;
                isWallsTouch = 2;
                mGameScene.fireParticleFive(fixtureA);
                i = 1;
                getAudioPlayer().play(SoundKeys.FREEZE);
            }

            if (i == 1 && isWallsTouch == 2) {
                multiplier = 1;
                if (GameScene.gameScene.getMultiplier())
                    multiplier = 2;
                touchCount += multiplier;

            }

            if (isTouch) {
                if (userDataA.type == "Shape") {
                    userDataB.count++;
                    userDataA.type = "Remove";
                    if (userDataB.count == 2) {
                        touchCount += 2 * multiplier;
                        mComboCount++;
                        mGameScene.fireBonusParticle(1, fixtureA);
                        getAudioPlayer().play(SoundKeys.DOUBLE);
                    } else if (userDataB.count == 3) {
                        touchCount += 3 * multiplier;
                        mGameScene.fireBonusParticle(2, fixtureA);
                        getAudioPlayer().play(SoundKeys.DOUBLE);
                    } else if (userDataA.isCritical) {
                        touchCount += 5;
                        mGameScene.fireBonusParticle(3, fixtureA);
                        getAudioPlayer().play(SoundKeys.DOUBLE);
                    }
                } else {
                    userDataA.count++;
                    userDataB.type = "Remove";
                    if (userDataA.count == 2) {
                        touchCount += 2 * multiplier;
                        mComboCount++;
                        mGameScene.fireBonusParticle(1, fixtureA);
                        getAudioPlayer().play(SoundKeys.DOUBLE);
                    } else if (userDataA.count == 3) {
                        touchCount += 3 * multiplier;
                        mGameScene.fireBonusParticle(2, fixtureA);
                        getAudioPlayer().play(SoundKeys.DOUBLE);
                    } else if (userDataB.isCritical) {
                        touchCount += 5;
                        mGameScene.fireBonusParticle(3, fixtureA);
                        getAudioPlayer().play(SoundKeys.DOUBLE);
                    }
                }

                if (i == 1) {

                    mGameScene.setScore(touchCount);
                    getAudioPlayer().play(SoundKeys.EGG_BREAK);
                }

            }

            //   mGameScene.cleanObjectList();
            runOnUpdateThread(new Runnable() {

                @Override
                public void run() {
                    switch (i) {
                    case 1:
                        mGameScene.cleanObjectList();
                        break;

                    default:
                        break;
                    }
                }
            });

        }

        @Override
        public void endContact(Contact contact) {
            // TODO Auto-generated method stub

        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
            // TODO Auto-generated method stub

            final Fixture fixtureA = contact.getFixtureA();
            final Fixture fixtureB = contact.getFixtureB();
            UserData uA = (UserData) fixtureA.getBody().getUserData();
            UserData uB = (UserData) fixtureB.getBody().getUserData();
            if ((uB.type == "Bullet") || (uA.type == "Bullet"))
                contact.setEnabled(false);
        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {
            // TODO Auto-generated method stub

        }
    };
    return contactListener;
    /* Collision of a hen with the floor. */
    /*   if (CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(),
    Names.FlyingHenName, Names.FloorName)
    || CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(),
          Names.FlyingBombName, Names.FloorName)
    || CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(),
          Names.FlyingThunderboltName, Names.FloorName)) {
          // TODO: Check with bomb.
          getAudioPlayer().play(SoundKeys.BOUNCE);
            
          final IBullet bullet = userDataA.getObjectName().equals(Names.FlyingHenName) ? (IBullet) userDataA
       : (IBullet) userDataB;
            
          mGunLayer.onCollision(bullet);
            
       } else if (collisionWithRoofOrWalls(userDataA.getObjectName(), userDataB.getObjectName())) {
          getAudioPlayer().play(SoundKeys.BOUNCE);
          /* Collision of a hen with the hen top. */
    /*   } else if (CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(),
    Names.FlyingHenName, Names.HenName)) {
          getAudioPlayer().play(SoundKeys.CONNECTION);
            
          runOnUpdateThread(new Runnable() {
            
    @Override
    public void run() {
       mGunLayer.resetGunLayerAfterConnectedBulletOrTimeElapsed(true);
    }
          });
          mHenLayer.onCollision(pContact);
            
          /* Collision of a bomb with the hen top. */
    /*   } else if (CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(),
    Names.FlyingBombName, Names.HenName)
    || CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(),
          Names.FlyingThunderboltName, Names.HenName)) {
          final IBullet bullet;
          if (Names.FlyingBombName.equals(userDataA.getObjectName())
       || Names.FlyingThunderboltName.equals(userDataA.getObjectName())) {
    bullet = (IBullet) userDataA;
          } else {
    bullet = (IBullet) userDataB;
          }
            
          bullet.setBodyType(BodyType.StaticBody);
          final AnimatedSpriteWithAreaToTouch animatedSpriteWithAreaToTouch = bullet
       .getAnimatedSpriteWithAreaToTouch();
          final long[] tabOfAnimations = new long[] { 100, 100, 100 };
          final float xPosition = animatedSpriteWithAreaToTouch.getX();
          final float yPosition = animatedSpriteWithAreaToTouch.getY();
          mEffectsLayer.prepareBombExplosion(xPosition, yPosition);
          animatedSpriteWithAreaToTouch.animate(tabOfAnimations, 3, 5, false, new OnAnimationFinishedListener(
       new IOnAnimationFinishedListener() {
          @Override
          public void onAnimationFinished(final AnimatedSprite pAnimatedSprite) {
             runOnUpdateThread(new Runnable() {
            
                @Override
                public void run() {
                   if (bullet.getBulletType() == BulletType.THUNDERBOLT) {
                      getAudioPlayer().play(SoundKeys.THUNDER);
                   } else {
                      getAudioPlayer().play(SoundKeys.BOMB_EXPLOSION);
                   }
            
                   if (bullet.getBulletType() == BulletType.BOMB) {
                      mEffectsLayer.explodeBullet();
                   }
                   mHenLayer.onCollisionWithBullet(pContact);
                   animatedSpriteWithAreaToTouch.detachSelf();
                   final PhysicsWorld pw = getPhysicsWorld();
                   final PhysicsConnector mPhysicsConnector = pw.getPhysicsConnectorManager()
                         .findPhysicsConnectorByShape(animatedSpriteWithAreaToTouch);
                   if (mPhysicsConnector != null) {
                      pw.unregisterPhysicsConnector(mPhysicsConnector);
                      final Body b = mPhysicsConnector.getBody();
                      pw.destroyBody(b);
                   }
                   mGunLayer.resetGunLayerAfterConnectedBulletOrTimeElapsed(true);
                }
             });
          }
       }));
       }*/
}

From source file:edu.lehigh.cse.lol.Physics.java

License:Open Source License

/**
 * Configure physics for the current level
 *
 * @param defaultXGravity The default force moving actors to the left (negative) or
 *                        right (positive)... Usually zero
 * @param defaultYGravity The default force pushing actors down (negative) or up
 *                        (positive)... Usually zero or -10
 *//*from   w  w w  .ja v  a  2  s. co m*/
public static void configure(float defaultXGravity, float defaultYGravity) {
    // create a world with gravity
    Lol.sGame.mCurrentLevel.mWorld = new World(new Vector2(defaultXGravity, defaultYGravity), true);

    // set up the collision handlers
    Lol.sGame.mCurrentLevel.mWorld.setContactListener(new ContactListener() {
        /**
         * When two bodies start to collide, we can use this to forward to
         * our onCollide methods
         */
        @Override
        public void beginContact(final Contact contact) {
            // Get the bodies, make sure both are actors
            Object a = contact.getFixtureA().getBody().getUserData();
            Object b = contact.getFixtureB().getBody().getUserData();
            if (!(a instanceof Actor) || !(b instanceof Actor))
                return;

            // the order is Hero, Enemy, Goodie, Projectile, Obstacle, Destination
            //
            // Of those, Hero, Enemy, and Projectile are the only ones with
            // a non-empty onCollide
            final Actor c0;
            final Actor c1;
            if (a instanceof Hero) {
                c0 = (Actor) a;
                c1 = (Actor) b;
            } else if (b instanceof Hero) {
                c0 = (Actor) b;
                c1 = (Actor) a;
            } else if (a instanceof Enemy) {
                c0 = (Actor) a;
                c1 = (Actor) b;
            } else if (b instanceof Enemy) {
                c0 = (Actor) b;
                c1 = (Actor) a;
            } else if (a instanceof Projectile) {
                c0 = (Actor) a;
                c1 = (Actor) b;
            } else if (b instanceof Projectile) {
                c0 = (Actor) b;
                c1 = (Actor) a;
            } else {
                return;
            }

            // Schedule an event to run as soon as the physics world
            // finishes its step.
            //
            // NB: this is called from render, while world is updating...
            // you can't modify the world or its actors until the update
            // finishes, so we have to schedule collision-based updates to
            // run after the world update.
            Lol.sGame.mCurrentLevel.mOneTimeEvents.add(new LolAction() {
                @Override
                public void go() {
                    c0.onCollide(c1, contact);
                }
            });
        }

        /**
         * We ignore endcontact
         */
        @Override
        public void endContact(Contact contact) {
        }

        /**
         * Presolve is a hook for disabling certain collisions. We use it
         * for collision immunity, sticky obstacles, and one-way walls
         */
        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
            // get the bodies, make sure both are actors
            Object a = contact.getFixtureA().getBody().getUserData();
            Object b = contact.getFixtureB().getBody().getUserData();
            if (!(a instanceof Actor) || !(b instanceof Actor))
                return;
            Actor gfoA = (Actor) a;
            Actor gfoB = (Actor) b;

            // handle sticky obstacles... only do something if at least one
            // actor is a sticky actor
            if (gfoA.mIsSticky[0] || gfoA.mIsSticky[1] || gfoA.mIsSticky[2] || gfoA.mIsSticky[3]) {
                handleSticky(gfoA, gfoB, contact);
                return;
            } else if (gfoB.mIsSticky[0] || gfoB.mIsSticky[1] || gfoB.mIsSticky[2] || gfoB.mIsSticky[3]) {
                handleSticky(gfoB, gfoA, contact);
                return;
            }

            // if the actors have the same passthrough ID, and it's
            // not zero, then disable the contact
            if (gfoA.mPassThroughId != 0 && gfoA.mPassThroughId == gfoB.mPassThroughId) {
                contact.setEnabled(false);
                return;
            }

            // is either one-sided? If not, we're done
            Actor onesided = null;
            Actor other;
            if (gfoA.mIsOneSided > -1) {
                onesided = gfoA;
                other = gfoB;
            } else if (gfoB.mIsOneSided > -1) {
                onesided = gfoB;
                other = gfoA;
            } else {
                return;
            }

            // if we're here, see if we should be disabling a one-sided
            // obstacle collision
            WorldManifold worldManiFold = contact.getWorldManifold();
            int numPoints = worldManiFold.getNumberOfContactPoints();
            for (int i = 0; i < numPoints; i++) {
                Vector2 vector2 = other.mBody.getLinearVelocityFromWorldPoint(worldManiFold.getPoints()[i]);
                // disable based on the value of isOneSided and the vector
                // between the actors
                if (onesided.mIsOneSided == 0 && vector2.y < 0)
                    contact.setEnabled(false);
                else if (onesided.mIsOneSided == 2 && vector2.y > 0)
                    contact.setEnabled(false);
                else if (onesided.mIsOneSided == 1 && vector2.x > 0)
                    contact.setEnabled(false);
                else if (onesided.mIsOneSided == 3 && vector2.x < 0)
                    contact.setEnabled(false);
            }
        }

        /**
         * We ignore postsolve
         */
        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {
        }
    });
}

From source file:Helper.GameContactListener.java

@Override
public void preSolve(Contact contact, Manifold mnfld) {
    WorldManifold manifold = contact.getWorldManifold();
    for (int j = 0; j < manifold.getNumberOfContactPoints(); j++) {
        if (contact.getFixtureA().getUserData() != null && contact.getFixtureB().getUserData() != null) {
            if ((checkFixtureA(contact, "PINGUIN") || checkFixtureB(contact, "PINGUIN"))
                    && pinguin.getIsRide()) {
                contact.setEnabled(false);
            }//  www. j av  a 2 s. c  o  m
            antelopeContact(contact);
            contactGiraffe(contact);
            snakeContact(contact);
            coinsPreContact(contact);
            deleteContact(contact);
            hippoContact(contact);
        }
    }
}

From source file:Helper.GameContactListener.java

private void deleteContact(Contact contact) {
    if (contact.getFixtureA().getUserData().equals("DELETE")
            || contact.getFixtureB().getUserData().equals("DELETE")) {
        contact.setEnabled(false);
    }//from  w w  w .  j  av  a2  s. c o m
}

From source file:Helper.GameContactListener.java

private void coinsPreContact(Contact contact) {
    if (contact.getFixtureA().getUserData() != null && contact.getFixtureB().getUserData() != null) {
        Coin g;/*  ww  w .j  ava 2  s.  c  o  m*/
        if (contact.getFixtureB().getUserData() instanceof Coin
                && contact.getFixtureA().getUserData().equals("PINGUIN")) {
            contact.setEnabled(false);

        } else if (contact.getFixtureA().getUserData() instanceof Coin
                && contact.getFixtureB().getUserData().equals("PINGUIN")) {
            contact.setEnabled(false);
        }
    }
}