Example usage for com.badlogic.gdx.physics.box2d.joints MouseJoint getTarget

List of usage examples for com.badlogic.gdx.physics.box2d.joints MouseJoint getTarget

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d.joints MouseJoint getTarget.

Prototype

public Vector2 getTarget() 

Source Link

Usage

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;/*from   w w  w .  j  a  va  2 s  .  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;
}

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

License:Open Source License

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    if (activeCard == null)
        return false;
    if (mouseJointStorage.size < 2)
        return false;

    Vector2 worldCoordinates = CoordinateHelper.getWorldCoordinates(camera, screenX, screenY);

    MouseJoint mouseJoint = mouseJointStorage.get(mouseJointStorage.size - 2);
    MouseJoint auxMouseJoint = mouseJointStorage.get(mouseJointStorage.size - 1);

    if (turning) {
        Vector2 v = new Vector2(worldCoordinates).sub(mouseJoint.getTarget());
        float a = v.angleRad();

        angleWhenRotating = a - angleOffset;

        if (roundedTurning) {
            // rounding
            float round = angleRounding * MathUtils.degreesToRadians;
            angleWhenRotating = Util.roundToNearestN(angleWhenRotating, round);
            float f = angleAtRotateStart % round;
            angleWhenRotating -= f;//  ww  w  . ja  v a 2 s  .  co  m
        }

        auxMouseJoint.setTarget(
                new Vector2(auxMouseJointDistance, 0).rotateRad(angleWhenRotating).add(mouseJoint.getTarget()));

    } else {

        if (turnCircleInner.isOutsideTurnCircle(worldCoordinates))
            return false;

        mouseJoint.setTarget(new Vector2(worldCoordinates).sub(offset));

        auxMouseJoint.setTarget(new Vector2(auxMouseJointDistance, 0).rotateRad(angleWhenRotating)
                .add(worldCoordinates).sub(offset));

        updateTurnCirclePos(mouseJoint.getTarget());
    }

    return true;
}