Example usage for com.badlogic.gdx.physics.box2d Fixture isSensor

List of usage examples for com.badlogic.gdx.physics.box2d Fixture isSensor

Introduction

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

Prototype

public boolean isSensor() 

Source Link

Document

Is this fixture a sensor (non-solid)?

Usage

From source file:be.ac.ucl.lfsab1509.bouboule.game.body.Obstacle.java

License:Open Source License

/**
 * Method that that remove/display the body from the screen and make it 
 * insensible/sensible to all chocks./*from  www  .j  ava  2s  . c  o  m*/
 * 
 * inverseBlink()
 */
public void inverseBlink() {
    Gdx.app.log("Obstacle", "reset Obstacle");
    ArrayList<Fixture> fixt = getBody().getFixtureList();

    for (Fixture fix : fixt) {
        fix.setSensor(!fix.isSensor());
    }

    getEntity().setAlive(!getEntity().isAlive());
}

From source file:com.sertaogames.cactus2d.Cactus2DLevel.java

License:Open Source License

private void initPhysics() {
    world = new World(new Vector2(0, -25f), true);
    world.setContactListener(new ContactListener() {
        @Override/*  www.  j  av a  2s .co m*/
        public void beginContact(Contact co) {
            Fixture fa = co.getFixtureA();
            Fixture fb = co.getFixtureB();
            if (fa.isSensor() && fb.isSensor())
                return;

            GameObject a = (GameObject) fa.getBody().getUserData();
            GameObject b = (GameObject) fb.getBody().getUserData();

            if (fa.isSensor() || fb.isSensor()) {
                a.onTriggerEnter(co, b);
                b.onTriggerEnter(co, a);
            } else {
                a.onCollisionEnter(co, b);
                b.onCollisionEnter(co, a);
            }
        }

        @Override
        public void endContact(Contact co) {
            Fixture fa = co.getFixtureA();
            Fixture fb = co.getFixtureB();
            if (fa.isSensor() && fb.isSensor())
                return;

            GameObject a = (GameObject) fa.getBody().getUserData();
            GameObject b = (GameObject) fb.getBody().getUserData();

            if (fa.isSensor() || fb.isSensor()) {
                a.onTriggerExit(co, b);
                b.onTriggerExit(co, a);
            } else {
                a.onCollisionExit(co, b);
                b.onCollisionExit(co, a);
            }
        }

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

        @Override
        public void postSolve(Contact co, ContactImpulse impulse) {
            Fixture fa = co.getFixtureA();
            Fixture fb = co.getFixtureB();
            if (fa.isSensor() && fb.isSensor())
                return;

            GameObject a = (GameObject) fa.getBody().getUserData();
            GameObject b = (GameObject) fb.getBody().getUserData();

            if (fa.isSensor() || fb.isSensor()) {
                a.onTriggerStay(co, b);
                b.onTriggerStay(co, a);
            } else {
                a.onCollisionStay(co, b);
                b.onCollisionStay(co, a);
            }
        }
    });
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** @param fixture the fixture for which to setup a new {@link FixtureDef}
 *  @return a new {@link FixtureDef} instance that can be used to clone the given fixture */
public static FixtureDef createDef(Fixture fixture) {
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.density = fixture.getDensity();
    Filter filter = fixture.getFilterData();
    fixtureDef.filter.categoryBits = filter.categoryBits;
    fixtureDef.filter.groupIndex = filter.groupIndex;
    fixtureDef.filter.maskBits = filter.maskBits;
    fixtureDef.friction = fixture.getFriction();
    fixtureDef.isSensor = fixture.isSensor();
    fixtureDef.restitution = fixture.getRestitution();
    fixtureDef.shape = fixture.getShape();
    return fixtureDef;
}

From source file:com.strategames.catchdastars.game.CatchDaStars.java

License:Open Source License

/**
 * beginContact is called when two fixtures make contact
 *//* ww w  .j a  v  a2s  .  c  o  m*/
@Override
public void beginContact(Contact contact) {
    //      Gdx.app.log("CatchDaStars", "beginContact: START");
    Fixture fixtureA = contact.getFixtureA();
    Fixture fixtureB = contact.getFixtureB();
    GameObject collidingGameObject1 = (GameObject) fixtureA.getBody().getUserData();
    GameObject collidingGameObject2 = (GameObject) fixtureB.getBody().getUserData();
    if ((collidingGameObject1 instanceof Balloon) && (fixtureB.isSensor())) {
        handleSensorCollision((Balloon) collidingGameObject1, collidingGameObject2);
    } else if ((collidingGameObject2 instanceof Balloon) && (fixtureA.isSensor())) {
        handleSensorCollision((Balloon) collidingGameObject2, collidingGameObject1);
    }
    //      Gdx.app.log("CatchDaStars", "beginContact: END");
}

From source file:headmade.arttag.Guard.java

License:Apache License

public void update(ArtTagScreen artTag, float delta) {
    if (body == null) {
        return;/*from   w  w  w. j a v a 2  s .  c om*/
    }
    isRunning = isAlert || isCautious;
    moveSpeed = isRunning ? maxMoveSpeed * runFactor
            : isSuspicious ? maxMoveSpeed * 0.75f : isHeardPlayer ? 0.5f : maxMoveSpeed;
    final Vector2 oldMoveVec = targetMoveVec.cpy();

    Vector2 targetPoint = null;
    Float distanceToPlayer = null;
    if (isLightOn) {
        for (final Fixture fixture : playerInView) {
            targetPoint = fixture.getBody().getWorldCenter().cpy();
            final Vector2 diffVec = body.getWorldCenter().cpy().sub(targetPoint);
            boolean seesPlayer = false;
            distanceToPlayer = diffVec.len();
            if (distanceToPlayer < 1f) {
                seesPlayer = true;
            } else {
                final Vector2 outOfBodyVec = diffVec.scl(fixture.getShape().getRadius() * 1.01f);
                targetPoint.add(outOfBodyVec);
                seesPlayer = light.contains(targetPoint.x, targetPoint.y);
            }
            // Gdx.app.log(TAG, light.contains(targetPoint.x, targetPoint.y) + " diffVec.length " + diffVec.len());
            if (seesPlayer) {
                // Gdx.app.log(TAG, "Guard sees player");
                playerLastSeenVec = fixture.getBody().getWorldCenter().cpy();
                if (!isAlert) {
                    Assets.instance.playSound(AssetSounds.whosThere);
                }
                isAlert = true;
                Player.instance.isSpotted = true;
                reactionTime = 0f;
            } else {
                if (isAlert) {
                    Assets.instance.playSound(AssetSounds.huh);
                    reactionTime = MAX_REACTION_TIME;
                    isCautious = true;
                }
                isAlert = false;
            }
        }
    }

    // handle hearing
    if (Player.instance.isRunning && (Player.instance.isMoveDown || Player.instance.isMoveUp
            || Player.instance.isMoveLeft || Player.instance.isMoveRight) && null != Player.instance.body) {
        if (distanceToPlayer == null) {
            distanceToPlayer = body.getWorldCenter().cpy().sub(Player.instance.body.getWorldCenter()).len();
        }
        if (distanceToPlayer < MAX_LIGHT_CONE_LENGTH * 0.9f) {
            sumDeltaSinceHeardPlayer = 0;

            final RayCastCallback callback = new RayCastCallback() {
                @Override
                public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
                    // Gdx.app.log(TAG, "reportRayFixture " + point + " normal " + normal);
                    if (fixture.isSensor()) {
                        // Gdx.app.log(TAG, "Raycast ignoring sensor");
                        return 1;
                    }
                    if (body.equals(fixture.getBody())) {
                        // this is the guards body so there is nothing inbetween them
                        // Gdx.app.log(TAG, "Guard CAN see the point where the noise happened" + Player.instance.body.getWorldCenter());
                        // playerLastHeardVisibleVec = Player.instance.body.getWorldCenter().cpy();
                        return 1;
                    }
                    // Gdx.app.log(TAG, "Fall through");
                    isHearingObstructed = true;
                    return 0;
                }
            };
            try {
                isSuspicious = true;
                playerLastHeardVec = Player.instance.body.getWorldCenter().cpy();
                isHearingObstructed = false;
                // Gdx.app.log(TAG, "###################################");
                // Gdx.app.log(TAG, "Guard " + body.getWorldCenter());
                // Gdx.app.log(TAG, "Player " + playerLastHeardVec);
                artTag.world.rayCast(callback, playerLastHeardVec, body.getWorldCenter());
                if (!isHearingObstructed) {
                    playerLastHeardVisibleVec = Player.instance.body.getWorldCenter().cpy();
                }
            } catch (final Exception e) {
                Gdx.app.error(TAG, "Error Raycasting :(", e);
            }
        } else {
            sumDeltaSinceHeardPlayer += delta;
        }
    } else {
        sumDeltaSinceHeardPlayer += delta;
    }
    isHeardPlayer = playerLastHeardVisibleVec != null || sumDeltaSinceHeardPlayer < 3;

    if (isTouchingPlayerLightCone && Player.instance.isLightOn) {
        // isAlert = true;
    }

    // handle backToPath
    if (isAlert || isCautious || (isSuspicious && playerLastHeardVisibleVec != null)) {
        if (lastVisibleVecBackToPath == null) {
            lastVisibleVecBackToPath = body.getWorldCenter();
        }

        Vector2 checkPoint = path.get(currentPathIndex);
        if (backToPath.size > 0) {
            checkPoint = backToPath.get(backToPath.size - 1);
        }
        if (BODY_RADIUS < checkPoint.dst(body.getWorldCenter())) {
            // not touching checkpoint
            // Gdx.app.log(TAG, "Goard not touching checkpoint");
            final RayCastCallback callback = new RayCastCallback() {
                @Override
                public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
                    // Gdx.app.log(TAG, "reportRayFixture adds new backToPathPoint" + lastVisibleVecBackToPath);
                    backToPath.add(lastVisibleVecBackToPath.cpy());
                    return 0;
                }
            };
            try {
                artTag.world.rayCast(callback, body.getWorldCenter(), checkPoint);
            } catch (final Exception e) {
                Gdx.app.error(TAG, "Error Raycasting :(", e);
            }
        }
        lastVisibleVecBackToPath = body.getWorldCenter();
    }

    // determine targetPoint
    if (isAlert && playerInView.size > 0 && Player.instance.body != null) {
        targetPoint = Player.instance.body.getWorldCenter();
    } else if (isCautious && playerLastSeenVec != null) {
        targetPoint = playerLastSeenVec;
        if (BODY_RADIUS / 10 > targetPoint.dst2(body.getPosition())) {
            // Lost player
            Assets.instance.playSound(AssetSounds.hm);
            Gdx.app.log(TAG, "Guard no longer cautious");
            isCautious = false;
            isSuspicious = true;
        }
    } else if (isSuspicious && playerLastHeardVisibleVec != null) {
        targetPoint = playerLastHeardVisibleVec;
        // Gdx.app.log(TAG, "Guard going to playerLastHeardVisibleVec");
        if (BODY_RADIUS / 10 > targetPoint.dst2(body.getPosition())) {
            // Lost player
            Assets.instance.playSound(AssetSounds.hm);
            Gdx.app.log(TAG, "Guard no longer suspicious");
            isSuspicious = false;
            playerLastHeardVisibleVec = null;
        }
    } else {
        lastVisibleVecBackToPath = null;
        if (backToPath.size > 0) {
            // following Path back to path
            targetPoint = backToPath.get(backToPath.size - 1);
            if (BODY_RADIUS / 10 > targetPoint.dst(body.getPosition())) {
                // Gdx.app.log(TAG, "Guard reached target back to path point " + targetPoint);
                backToPath.pop();
            }
        } else {
            // following path
            isSuspicious = false;
            targetPoint = path.get(currentPathIndex);
            if (BODY_RADIUS > targetPoint.dst(body.getPosition())) {
                // Gdx.app.log(TAG, "Guard reached target point " + targetPoint);
                currentPathIndex++;
                if (currentPathIndex >= path.size) {
                    currentPathIndex = 0;
                }
                targetPoint = path.get(currentPathIndex);
                // Gdx.app.log(TAG, "New target point " + targetPoint);
            }
        }

    }

    targetMoveVec = targetPoint.cpy().sub(body.getPosition());
    targetMoveVec.nor().scl(moveSpeed);

    if (MathUtils.isEqual(0f, oldMoveVec.angle(targetMoveVec))) {
        sumDeltaSinceMoveChange += delta;
    } else {
        // movment direction changed
        sumDeltaSinceMoveChange = 0f;
    }

    final float alpha = reactionTime > 0 ? MathUtils.clamp(sumDeltaSinceMoveChange / reactionTime, 0.1f, 1f)
            : 1f;
    body.setLinearVelocity(targetMoveVec);

    final Vector2 bodyRotVec = new Vector2(1f, 0f);
    bodyRotVec.setAngleRad(body.getAngle());
    float angleDiff;
    if (!isAlert && !isSuspicious && isHeardPlayer && null == playerLastHeardVisibleVec
            && null != playerLastHeardVec) {
        // look at last heard
        angleDiff = bodyRotVec.angleRad(playerLastHeardVec.cpy().sub(body.getWorldCenter()).rotate90(-1));
    } else {
        angleDiff = bodyRotVec.angleRad(targetMoveVec.cpy().rotate90(-1));
    }
    final float rotByRad = MathUtils.clamp(angleDiff, -(MAX_ROTATION_SPEED * delta) / reactionTime,
            MAX_ROTATION_SPEED * delta / reactionTime);
    // Gdx.app.log(TAG, "angleDiff: " + angleDiff + " rotByRad: " + rotByRad + " bodyRotVec: " + bodyRotVec + " - targetMoveVec:
    // "
    // + targetMoveVec);

    // is moving?
    if (!MathUtils.isEqual(targetMoveVec.len2(), 0f)) {
        if (Player.instance.body != null) {
            final float dist = body.getPosition().dst(Player.instance.body.getPosition());
            float volume = isRunning ? STEP_VOLUME * 2 : STEP_VOLUME;
            if (dist > 1) {
                volume = volume / dist;
            }
            sound.setVolume(stepSoundId, volume);
            sound.setPitch(stepSoundId, isRunning ? runFactor : 1f);
            body.setTransform(body.getPosition(), body.getAngle() + rotByRad);
        }
    } else {
        sound.setVolume(stepSoundId, 0f);
        sound.setPitch(stepSoundId, 1f);
    }

    light.setActive(isLightOn);
}

From source file:io.piotrjastrzebski.sfg.game.ContactDispatcher.java

License:Open Source License

@Override
public void beginContact(Contact contact) {
    // TODO this is ass, research better way of doing this
    Fixture fA = contact.getFixtureA();
    Fixture fB = contact.getFixtureB();//  ww w.  jav a  2 s  .  co  m
    // all sensors in game are static, so only one fixture can be a sensor
    if (fA.isSensor() && checkSensor(fA, fB)) {
        return;
    } else if (fB.isSensor() && checkSensor(fB, fA)) {
        return;
    }

    final Object o1 = contact.getFixtureA().getBody().getUserData();
    final Object o2 = contact.getFixtureB().getBody().getUserData();

    checkPickup(o1, o2);
    checkRocket(o1, o2);
    // parts are small and dont really intersect, so just getting first one is ok
    final Vector2 position = contact.getWorldManifold().getPoints()[0];
    checkPlayer(o1, o2, position);
}

From source file:net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** creates a deep copy of a {@link Fixture}
 *  @param fixture the {@link Fixture} to copy
 *  @param body the {@link Body} to create a copy of the given {@code fixture} on
 *  @param shape if the {@link Fixture#getShape() shape} of the given {@code fixture} should be deep {@link #copy(Shape) copied} as well
 *  @return the copied {@link Fixture} */
public static Fixture copy(Fixture fixture, Body body, boolean shape) {
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.density = fixture.getDensity();
    Filter filter = fixture.getFilterData();
    fixtureDef.filter.categoryBits = filter.categoryBits;
    fixtureDef.filter.groupIndex = filter.groupIndex;
    fixtureDef.filter.maskBits = filter.maskBits;
    fixtureDef.friction = fixture.getFriction();
    fixtureDef.isSensor = fixture.isSensor();
    fixtureDef.restitution = fixture.getRestitution();
    fixtureDef.shape = shape ? copy(fixture.getShape()) : fixture.getShape();
    Fixture copy = body.createFixture(fixtureDef);
    copy.setUserData(copy.getUserData());
    return copy;//ww  w.jav  a 2s  . c  o  m
}