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

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

Introduction

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

Prototype

public void setAwake(boolean flag) 

Source Link

Document

Set the sleep state of the body.

Usage

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

License:Apache License

/**
 * Drop ball into centre.//  w  w w  .  j  av a 2 s .co 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:fr.plnech.igem.game.AbstractGameActivity.java

License:Open Source License

private void loadPhysics(BaseGame game) {
    final ContactListener contactListener = game.getContactListener();
    if (contactListener == null) {
        return;//  w  w w  . j a  va2  s .co m
    }

    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;// ww  w .j  a  v a2s . c o m

    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;
}