Example usage for com.badlogic.gdx.physics.box2d Body setActive

List of usage examples for com.badlogic.gdx.physics.box2d Body setActive

Introduction

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

Prototype

public void setActive(boolean flag) 

Source Link

Document

Set the active state of the body.

Usage

From source file:com.github.mkjensen.breakall.actor.Box2DActor.java

License:Apache License

private Body setupBody() {
    Body body = createBody(world);
    body.setActive(false);
    body.setUserData(this);
    return body;
}

From source file:com.johnogel.astrobros.levels.Level.java

protected void cleanUp() {
    for (Body b : to_be_destroyed) {

        b.setActive(false);
        //b.setAwake(false);

        for (int i = 0; i < locators.size; i++) {
            if (locators.get(i).getPlayer().getBody().equals(b)
                    || locators.get(i).getOtherBro().getBody().equals(b)) {
                locators.removeIndex(i);
            }//from  w ww. j a  v a2s.  c om
        }

        for (int i = 0; i < bros.size; i++) {
            if (bros.get(i).getBody().equals(b)) {
                if (b.equals(player.getBody())) {
                    player.disablePlayer();
                    player = null;
                }
                bodies.removeValue(b, true);
                free_bodies.removeValue(b, true);
                controlled_bodies.removeValue(b, true);
                bro_bodies.removeValue(b, true);
                mngr.removeGameObject(b);
                free_bros.removeValue(bros.get(i), true);
                controlled_bros.removeValue(bros.get(i), true);
                bros.removeIndex(i);
            }

        }

        //world.destroyBody(b);

        //b = null;

    }

    to_be_destroyed.clear();
}

From source file:com.mcprog.ragnar.screens.GameScreen.java

License:Apache License

/**
 * Safely destroys arrow that have been added to an array of bodies to remove
 * These are arrows that have either hit each other, the player, or the outside bounds
 * <li>1. sets the bodies inactive</li>
 * <li>2. destroys the inactive bodies</li>
 * This means that it takes 2 frames to completely delete the bodies
 *//*w w w  . java  2  s  .  c o  m*/
private void safelyDestroyBodies() {
    world.getBodies(bodies);
    if (!world.isLocked()) {
        for (Body b : bodiesToDelete) {
            b.setActive(false);
        }
        bodiesToDelete.clear();
        for (Body i : bodies) {
            if (!i.isActive()) {
                world.destroyBody(i);
            }
        }
    }
}

From source file:com.netthreads.gdx.app.layer.SimulationLayer.java

License:Apache License

/**
 * Drop ball into centre./* w  w  w . j  av  a  2s  . c o  m*/
 * 
 */
private void handleDropShape(float x, float y) {

    // Assign random starting position and angle.
    float tx = x / pixelsPerMetre;
    float ty = y / pixelsPerMetre;
    float angle = rand.nextFloat() * MathUtils.PI * 2;

    // -----------------------
    // Body
    // -----------------------
    BodyDef ballBodyDef = new BodyDef();
    ballBodyDef.position.set(tx, ty);
    ballBodyDef.angle = angle;
    ballBodyDef.type = BodyType.DynamicBody;

    CircleShape ballShape = new CircleShape();
    ballShape.setRadius(BALL_BODY_SIZE.x / 2);

    Body body = world.createBody(ballBodyDef);
    body.createFixture(ballShape, 1);
    body.setUserData(BALL_BODY_SIZE);

    // Properties
    body.setActive(true);
    body.setAwake(true);
    body.setLinearVelocity(tmpVec.set(0, 0));
    body.setAngularVelocity(0);

    // -----------------------
    // Get sprite.
    // -----------------------
    SimpleSprite sprite = pool.obtain();

    // Size.
    sprite.setScaleX((BALL_BODY_SIZE.x * pixelsPerMetre) / sprite.getWidth());
    sprite.setScaleY((BALL_BODY_SIZE.y * pixelsPerMetre) / sprite.getHeight());

    // Centre of rotation.
    sprite.setOriginX(sprite.getWidth() / 2);
    sprite.setOriginY(sprite.getHeight() / 2);

    // -----------------------
    // Add actor to the view.
    // -----------------------
    addActor(sprite);

    // -----------------------
    // Create Action
    // -----------------------
    BodyUpdateAction updateAction = BodyUpdateAction.$(world, body, pixelsPerMetre, true);

    // -----------------------
    // Run action on actor.
    // -----------------------
    sprite.addAction(updateAction);
}

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

License:Open Source License

/**
 * Change the size of an actor, and/or change its position
 *
 * @param x      The new X coordinate of its bottom left corner
 * @param y      The new Y coordinate of its bototm left corner
 * @param width  The new width of the actor
 * @param height The new height of the actor
 *//*from w w  w. j  a va 2 s  .c o m*/
public void resize(float x, float y, float width, float height) {
    // To scale a polygon, we'll need a scaling factor, so we can
    // manually scale each point
    float xscale = height / mSize.y;
    float yscale = width / mSize.x;
    // set new height and width
    mSize.x = width;
    mSize.y = height;
    // read old body information
    Body oldBody = mBody;
    // make a new body
    if (mIsCircleBody) {
        Fixture oldFix = oldBody.getFixtureList().get(0);
        setCirclePhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(),
                oldBody.isBullet(), x, y, (width > height) ? width / 2 : height / 2);
    } else if (mIsBoxBody) {
        Fixture oldFix = oldBody.getFixtureList().get(0);
        setBoxPhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(),
                oldBody.isBullet(), x, y);
    } else if (mIsPolygonBody) {
        Fixture oldFix = oldBody.getFixtureList().get(0);
        // we need to manually scale all the vertices
        PolygonShape ps = (PolygonShape) oldFix.getShape();
        float[] verts = new float[ps.getVertexCount() * 2];
        for (int i = 0; i < ps.getVertexCount(); ++i) {
            ps.getVertex(i, mTmpVert);
            verts[2 * i] = mTmpVert.x * xscale;
            verts[2 * i + 1] = mTmpVert.y * yscale;
        }
        setPolygonPhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(),
                oldBody.isBullet(), x, y, verts);
    }
    // clone forces
    mBody.setAngularVelocity(oldBody.getAngularVelocity());
    mBody.setTransform(mBody.getPosition(), oldBody.getAngle());
    mBody.setGravityScale(oldBody.getGravityScale());
    mBody.setLinearDamping(oldBody.getLinearDamping());
    mBody.setLinearVelocity(oldBody.getLinearVelocity());
    // disable the old body
    oldBody.setActive(false);
}

From source file:fr.plnech.igem.game.AbstractGameActivity.java

License:Open Source License

private void loadPhysics(BaseGame game) {
    final ContactListener contactListener = game.getContactListener();
    if (contactListener == null) {
        return;/*from   ww  w.j  a v  a2 s  .  c om*/
    }

    physicsWorld = new PhysicsWorld(game.getPhysicsVector(), false) {
        @Override
        public void onUpdate(float pSecondsElapsed) {
            super.onUpdate(pSecondsElapsed);
            if (!physicsWorld.isLocked()) {
                for (PhysicalWorldObject object : objectsToDelete) {
                    destroyObject(object);
                }
                objectsToDelete.clear();

                for (PhysicalWorldObject object : objectsToAdd) {
                    createObject(object);
                }
                objectsToAdd.clear();
            }
        }

        private void createObject(final PhysicalWorldObject object) {
            object.onAddToWorld();
        }

        private void destroyObject(final PhysicalWorldObject object) {
            Body body = object.getBody();
            Sprite sprite = object.getSprite();
            body.setActive(false);
            body.setAwake(false);
            unregisterPhysicsConnector(
                    physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(sprite));
            body.setActive(false);
            destroyBody(body);
            object.onRemoveFromWorld();
        }
    };
    physicsWorld.setContactListener(contactListener);
    gameScene.registerUpdateHandler(physicsWorld);
}

From source file:org.ams.testapps.paintandphysics.cardhouse.CardMover.java

License:Open Source License

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    Vector2 touchCoordinates = CoordinateHelper.getWorldCoordinates(camera, screenX, screenY);

    // check if touch is for turning

    boolean onInner = turnCircleInner.isOnTurnCircle(touchCoordinates);
    boolean onOuter = turnCircleOuter.isOnTurnCircle(touchCoordinates);

    turning = onInner || onOuter;// w w w.  j  a  va2s. c  om

    if (turning) {
        roundedTurning = onInner;
        // start turning routine

        if (debug)
            debug("Turning card.");
        MouseJoint mouseJoint = mouseJointStorage.get(mouseJointStorage.size - 2);
        MouseJoint auxMouseJoint = mouseJointStorage.get(mouseJointStorage.size - 1);

        Vector2 v = new Vector2(auxMouseJoint.getTarget()).sub(mouseJoint.getTarget());
        float a = v.angleRad();

        Vector2 w = new Vector2(touchCoordinates).sub(mouseJoint.getTarget());
        float b = w.angleRad();

        angleOffset = b - a;

        return true;
    }

    // find card to move
    Thing touchedThing = WorldUtil.getClosestThingIntersectingCircle(world.things, touchCoordinates.x,
            touchCoordinates.y, Util.getTouchRadius(camera.zoom), filter);

    PPPolygon card = null;
    if (touchedThing == null && turnCircleInner.isInsideTurnCircle(touchCoordinates)) {
        card = this.activeCard;
    } else if (touchedThing != null && touchedThing.getBody().isActive()) {
        card = (PPPolygon) touchedThing.getUserData();
    }

    // cancel if no card
    if (card == null) {
        setActiveCard(null);
        return false;

    }

    if (debug)
        debug("Moving card.");

    boolean directTouch = touchedThing != null;
    boolean newCard = this.activeCard != card;

    if (newCard || directTouch) {
        // prepare to move card with fresh mouse joints

        resetColors();
        setActiveCard(card);

        // make new mouse joints
        destroyMouseJoints(card.getPhysicsThing());
        MouseJoint mouseJoint = createMouseJoint(touchCoordinates);
        mouseJointStorage.add(mouseJoint);
        mouseJointStorage.add(createAuxMouseJoint(touchCoordinates));

        // move turn circle
        updateTurnCirclePos(mouseJoint.getTarget());

        // remove oldest joints
        enforceStorageLimit();

        // let listeners know which card is added and which was removed
        notifyListenersAddedRemoved();

        // force it to move
        Body body = card.getPhysicsThing().getBody();
        body.setAwake(true);
        body.setActive(true);

        // reset some stuff
        offset.set(0, 0);
        angleWhenRotating = 0;

        angleAtRotateStart = activeCard.getPhysicsThing().getBody().getAngle();

    } else if (turnCircleInner.isInsideTurnCircle(touchCoordinates)) {
        // just update offsets for current card
        offset.set(touchCoordinates).sub(mouseJointStorage.get(mouseJointStorage.size - 2).getTarget());

    }

    return true;
}