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

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

Introduction

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

Prototype

public WorldManifold getWorldManifold() 

Source Link

Usage

From source file:com.arkanoid.game.model.GameField.java

License:Apache License

public void processBallAndVausContact(Contact contact) {
    WorldManifold wm = contact.getWorldManifold();
    Vector2 normal = wm.getNormal();
    normal.x = normal.x * 1000000;//w  w w . ja v  a 2 s.c  o m
    normal.y = normal.y * 1000000;
    for (int i = 0; i < 10; i++) {
        world.step(1 / 60f, 10, 10);
        ball.getBody().applyLinearImpulse(normal, ball.getBody().getPosition(), true);
    }
    getWorldListener().processBallAndVausContact();
}

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  ww .  j a va2  s.  c o  m
    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 w  w w.  jav a  2 s  .co  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;/*  w w w.  j  ava 2  s  . c om*/
    Vector2 point = worldManifold.getPoints()[0];
    renderer.setColor(getColorByBody(contact.getFixtureA().getBody()));
    renderer.point(point.x, point.y, 0);
}

From source file:com.tnf.ptm.common.PtmContactListener.java

License:Apache License

@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
    PtmObject soa = (PtmObject) contact.getFixtureA().getBody().getUserData();
    PtmObject sob = (PtmObject) contact.getFixtureB().getBody().getUserData();
    if (soa instanceof Projectile && ((Projectile) soa).getConfig().density <= 0) {
        return;//from   w w  w  . j a  v a2 s.c  o m
    }
    if (sob instanceof Projectile && ((Projectile) sob).getConfig().density <= 0) {
        return;
    }

    float absImpulse = calcAbsImpulse(impulse);
    Vector2 collPos = contact.getWorldManifold().getPoints()[0];
    soa.handleContact(sob, impulse, true, absImpulse, myGame, collPos);
    sob.handleContact(soa, impulse, false, absImpulse, myGame, collPos);
    myGame.getSpecialSounds().playColl(myGame, absImpulse, soa, collPos);
    myGame.getSpecialSounds().playColl(myGame, absImpulse, sob, collPos);
}

From source file:de.cwclan.gdxtest.core.entities.Player.java

@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
    if (contact.getFixtureA() == fixture || contact.getFixtureB() == fixture) {
        if (contact.getWorldManifold().getPoints()[0].y <= body.getPosition().y - HEIGHT / 2) {
            body.applyLinearImpulse(0, jumpPower, body.getWorldCenter().x, body.getWorldCenter().y, true);
        }//w w  w .  j  av a 2s.c om
    }
}

From source file:DungeonCleanerGame.DungeonCollissions.java

@Override
public void beginContact(Contact contact) {
    Fixture fixA = contact.getFixtureA();
    Fixture fixB = contact.getFixtureB();
    Body Player;/*from  ww  w.ja v a  2s. com*/
    Body Monster;
    if ((fixA.getFilterData().groupIndex == gPW && fixB.getFilterData().groupIndex == gM)
            || (fixA.getFilterData().groupIndex == gM && fixB.getFilterData().groupIndex == gPW)) {
        ++numPlyHits;
        eng.gameRender().addDebugString("PlayerHits = " + numPlyHits, 1);
        if (fixB.getFilterData().groupIndex == gM) {
            Monster = fixB.getBody();
            Player = fixA.getBody();
        } else {
            Monster = fixA.getBody();
            Player = fixB.getBody();
        }
        WorldManifold mani = contact.getWorldManifold();
        combat.computeStrikeToMonster(Player, Monster, mani);
    } else if ((fixA.getFilterData().groupIndex == gP && fixB.getFilterData().groupIndex == gM)
            || (fixA.getFilterData().groupIndex == gM && fixB.getFilterData().groupIndex == gP)) {
        ++numMonHits;
        eng.gameRender().addDebugString("MonsterHits = " + numMonHits, 2);
        if (fixB.getFilterData().groupIndex == gP) {
            Player = fixB.getBody();
            Monster = fixA.getBody();
        } else {
            Player = fixA.getBody();
            Monster = fixB.getBody();
        }
        WorldManifold mani = contact.getWorldManifold();
        combat.computeStrikeToPlayer(Monster, Player, mani);
    } else if ((fixA.getFilterData().groupIndex == gP && fixB.getFilterData().groupIndex == gMV)
            || (fixA.getFilterData().groupIndex == gMV && fixB.getFilterData().groupIndex == gP)) {
        ++numMonDetect;
        eng.gameRender().addDebugString("MonsterDetection = " + numMonDetect, 3);
        if (fixB.getFilterData().groupIndex == gP) {
            Player = fixB.getBody();
            Monster = fixA.getBody();
        } else {
            Player = fixA.getBody();
            Monster = fixB.getBody();
        }

        int monsterID = (Integer) Monster.getUserData();
        gmap.getEnemy(monsterID).getIA().actAlert(Player);

    } else if ((fixA.getFilterData().groupIndex == gP && fixB.getFilterData().groupIndex == gB)
            || (fixA.getFilterData().groupIndex == gB && fixB.getFilterData().groupIndex == gP)) {
        if (fixB.getFilterData().groupIndex == gP) {
            Player = fixB.getBody();
            Monster = fixA.getBody();
        } else {
            Player = fixA.getBody();
            Monster = fixB.getBody();
        }
        WorldManifold mani = contact.getWorldManifold();
        combat.computeStrikeToPlayer(Monster, Player, mani);

        //SE 
        //eng.gamePhysics().getWorld().
    }
}

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

License:Open Source License

/**
 * When a hero collides with a "sticky" obstacle, this is the code we run to
 * figure out what to do/*  w  w w  .j  a va  2 s. c  o  m*/
 *
 * @param sticky  The sticky actor... it should always be an obstacle for now
 * @param other   The other actor... it should always be a hero for now
 * @param contact A description of the contact event
 */
static void handleSticky(final Actor sticky, final Actor other, Contact contact) {
    // don't create a joint if we've already got one
    if (other.mDJoint != null)
        return;
    // don't create a joint if we're supposed to wait
    if (System.currentTimeMillis() < other.mStickyDelay)
        return;
    // handle sticky obstacles... only do something if we're hitting the
    // obstacle from the correct direction
    if ((sticky.mIsSticky[0] && other.getYPosition() >= sticky.getYPosition() + sticky.mSize.y)
            || (sticky.mIsSticky[1] && other.getXPosition() + other.mSize.x <= sticky.getXPosition())
            || (sticky.mIsSticky[3] && other.getXPosition() >= sticky.getXPosition() + sticky.mSize.x)
            || (sticky.mIsSticky[2] && other.getYPosition() + other.mSize.y <= sticky.getYPosition())) {
        // create distance and weld joints... somehow, the combination is
        // needed to get this to work. Note that this function runs during
        // the box2d step, so we need to make the joint in a callback that
        // runs later
        final Vector2 v = contact.getWorldManifold().getPoints()[0];
        Lol.sGame.mCurrentLevel.mOneTimeEvents.add(new LolAction() {
            @Override
            public void go() {
                other.mBody.setLinearVelocity(0, 0);
                DistanceJointDef d = new DistanceJointDef();
                d.initialize(sticky.mBody, other.mBody, v, v);
                d.collideConnected = true;
                other.mDJoint = (DistanceJoint) Lol.sGame.mCurrentLevel.mWorld.createJoint(d);
                WeldJointDef w = new WeldJointDef();
                w.initialize(sticky.mBody, other.mBody, v);
                w.collideConnected = true;
                other.mWJoint = (WeldJoint) Lol.sGame.mCurrentLevel.mWorld.createJoint(w);
            }
        });
    }
}

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
 *///ww w.  java2s  .  c  o  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:EntityManager.ContactListenerSystem.java

public void addContact(float delta, Contact contact) {
    for (Entity e : entities) {

        if (contact.getFixtureA() == bm.get(e).body.getFixtureList().first()) {
            for (Entity player : players) {
                if (contact.getFixtureB() == sensorm.get(player).sensor) {
                    e.add(new CollisionComponent(player.getId(), contact.getWorldManifold()));
                    player.add(new CollisionComponent(e.getId(), contact.getWorldManifold()));
                    System.out.println("hit a player");

                    return;
                }//from   w ww.  j  ava 2 s.  c o  m
            }
        }

    }
}