Example usage for com.badlogic.gdx.physics.box2d WorldManifold getNumberOfContactPoints

List of usage examples for com.badlogic.gdx.physics.box2d WorldManifold getNumberOfContactPoints

Introduction

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

Prototype

public int getNumberOfContactPoints() 

Source Link

Usage

From source file:com.blindtigergames.werescrewed.debug.SBox2DDebugRenderer.java

License:Apache License

private void drawContact(Contact contact) {
    WorldManifold worldManifold = contact.getWorldManifold();
    if (worldManifold.getNumberOfContactPoints() == 0)
        return;/*  w  w  w  .  j  a  v  a  2  s  . c om*/
    Vector2 point = worldManifold.getPoints()[0];
    renderer.point(point.x, point.y, 0);
}

From source file:com.me.main.startgame.java

License:Apache License

private boolean isPlayerGrounded(float deltaTime) {
    Array<Contact> contactList = world.getContactList();
    for (int i = 0; i < contactList.size; i++) {
        Contact contact = contactList.get(i);
        if (contact.isTouching() && (contact.getFixtureA() == playerSensorFixture
                || contact.getFixtureB() == playerSensorFixture)) {

            Vector2 pos = player.getPosition();
            WorldManifold manifold = contact.getWorldManifold();
            boolean below = true;
            for (int j = 0; j < manifold.getNumberOfContactPoints(); j++) {
                below &= (manifold.getPoints()[j].y < pos.y - 0.1f);
            }/*from ww  w .  j a  va2  s  . c  o  m*/
            if (below) {
                return true;
            }
            return false;
        }
    }
    return false;
}

From source file:com.me.mygdxgame.Entities.MydebugRenderer.java

License:Apache License

private void drawContact(Contact contact) {
    WorldManifold worldManifold = contact.getWorldManifold();
    if (worldManifold.getNumberOfContactPoints() == 0)
        return;/*from ww w  .java2  s  .  co  m*/
    Vector2 point = worldManifold.getPoints()[0];
    renderer.setColor(getColorByBody(contact.getFixtureA().getBody()));
    renderer.point(point.x, point.y, 0);
}

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 ww  w.j  ava 2 s  .  c  om
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);
            }/*from  ww  w.  ja  v a2  s . com*/
            antelopeContact(contact);
            contactGiraffe(contact);
            snakeContact(contact);
            coinsPreContact(contact);
            deleteContact(contact);
            hippoContact(contact);
        }
    }
}

From source file:org.box2d.r3.gdx.GDXBox2DDebugRenderer.java

License:Apache License

private void drawContact(final Contact contact) {
    final WorldManifold worldManifold = contact.getWorldManifold();
    if (worldManifold.getNumberOfContactPoints() == 0) {
        return;/*from  ww w .  ja  va 2s.  co m*/
    }
    final Vector2 point = worldManifold.getPoints()[0];
    this.renderer.setColor(this.getColorByBody(contact.getFixtureA().getBody()));
    this.renderer.point(point.x, point.y, 0);
}