Example usage for com.badlogic.gdx.math Vector2 angleRad

List of usage examples for com.badlogic.gdx.math Vector2 angleRad

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Vector2 angleRad.

Prototype

public float angleRad() 

Source Link

Usage

From source file:org.ams.prettypaint.OutlinePolygon.java

License:Open Source License

private void updateVertexDefault(int index, boolean inside) {

    BoundingBox box = boundingBoxes.items[index / verticesPerBox];
    box.needsCullingUpdate = true;//from   www . j  av  a2s  .  com

    int k = index % vertices.size;
    StripVertex stripVertex = vertexDataArray.items[k];
    AuxVertexFinder auxVertexFinder = this.auxVertexFinder;

    Array<Float> vertexData = inside ? stripVertex.insideVertexData : stripVertex.outsideVertexData;
    vertexData.clear();

    auxVertexFinder.setInsideStrip(inside);

    Vector2 currentVertex = vertices.items[k];
    Vector2 defaultAux = auxVertexFinder.getAux(vertices, k);

    boolean roundCorner = false;

    { // within these brackets i figure out if i should round the corner.
      // i hope to rewrite this code to something i can understand one day after writing it
        Vector2 previous = tmp.set(vertices.items[(k - 1 + vertices.size) % vertices.size]);
        Vector2 copyOfDefaultAux = tmp1.set(defaultAux);

        previous.sub(currentVertex);
        copyOfDefaultAux.sub(currentVertex);

        float angle = previous.angleRad() - copyOfDefaultAux.angleRad();
        angle = ((angle + MathUtils.PI2) % MathUtils.PI) * 2f;
        boolean angleMoreThanPI;
        if (inside) {
            angleMoreThanPI = angle > MathUtils.PI * 1.1f;
        } else {
            angleMoreThanPI = angle < MathUtils.PI * 0.9f;
        }

        if (auxVertexFinder.clockWisePolygon)
            angleMoreThanPI = !angleMoreThanPI;

        if (angleMoreThanPI) {
            boolean sharpCorner = Math.abs(MathUtils.PI - angle) > Math.PI * 0.4f;
            roundCorner = roundSharpCorners && sharpCorner;
        }
    }

    if (roundCorner) {
        Vector2 beginningAux = auxVertexFinder.getAuxEnding(vertices, k, 0);
        Vector2 endingAux = auxVertexFinder.getAuxBeginning(vertices, k, 0);
        Vector2 middleAux = tmp.set(defaultAux).sub(currentVertex).nor().scl(halfWidth).add(currentVertex);

        add(currentVertex, VERTEX_TYPE_USER, vertexData);
        add(beginningAux, VERTEX_TYPE_AUX, vertexData);

        add(currentVertex, VERTEX_TYPE_USER, vertexData);
        add(middleAux, VERTEX_TYPE_AUX, vertexData);

        add(currentVertex, VERTEX_TYPE_USER, vertexData);
        add(endingAux, VERTEX_TYPE_AUX, vertexData);
    } else {
        add(currentVertex, VERTEX_TYPE_USER, vertexData);
        add(defaultAux, VERTEX_TYPE_AUX, vertexData);
    }

}

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;/* www. j  a v  a2  s .  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;
}

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  . c o  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;
}

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

License:Open Source License

/**
 * Instantly show a tip on the screen./*from ww  w.ja  v  a  2 s  . c o  m*/
 *
 * @param key     String for remembering whether to display this tips in the feature.
 * @param text    The tips.
 * @param arrowTo If not null an arrow will point to this location.
 */
private void showTip(final String key, final String text, final ChangingVector arrowTo) {
    if (debug)
        debug("Showing tip with key " + key + ".");

    final Array<OutlinePolygon> arrow = new Array<OutlinePolygon>();

    stage.getActors().removeValue(window, true);

    // update this tip on resize
    Tips.this.onResize = new Runnable() {
        @Override
        public void run() {

            arrows.removeAll(arrow, true);
            stage.getActors().removeValue(window, true);
            showTip(key, text, arrowTo);
        }
    };

    // prepare text table
    Label label = new Label(text, skin);
    label.setColor(Color.BLACK);

    Table textTable = new Table();
    textTable.add(label);

    // prepare button table
    TextButton hide = new TextButton("Hide", skin);
    hide.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            stage.getActors().removeValue(window, true);
            arrows.removeAll(arrow, true);
            showNextTip();
        }
    });

    TextButton ok = new TextButton("Got It", skin);
    ok.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            stage.getActors().removeValue(window, true);
            arrows.removeAll(arrow, true);
            preferences.putBoolean(key, true);
            preferences.flush();
            showNextTip();
        }
    });

    Table buttonTable = new Table();
    buttonTable.add(hide).pad(SceneUtil.getPreferredPadding(stage));
    buttonTable.add(ok).pad(SceneUtil.getPreferredPadding(stage));

    // put it all in table
    Table mainTable = new Table();

    mainTable.add(textTable).row();
    mainTable.add(buttonTable);

    // prepare arrow
    if (arrowTo != null) {
        float sw = stage.getWidth();
        float sh = stage.getHeight();

        // figure out where to start the arrow
        Vector2 arrowFrom = new Vector2(mainTable.getPrefWidth() * 0.5f, 0);
        Vector2 v = new Vector2(arrowTo.get()).sub(sw * 0.5f, sh * 0.5f);
        arrowFrom.rotateRad(v.angleRad());

        arrowFrom.add(sw * 0.5f, sh * 0.5f);
        arrowFrom.scl(1, -1).add(0, sh);

        // convert to world coordinates
        Vector2 screenCoordinates = stage.stageToScreenCoordinates(arrowFrom);
        Vector2 from = CoordinateHelper.getWorldCoordinates(arrowCamera, screenCoordinates.x,
                screenCoordinates.y);

        // we know where to end the arrow, just convert to world coordinates
        screenCoordinates = stage.stageToScreenCoordinates(new Vector2(arrowTo.get()).scl(1, -1).add(0, sh));
        Vector2 to = CoordinateHelper.getWorldCoordinates(arrowCamera, screenCoordinates.x,
                screenCoordinates.y);

        // make and save arrow
        arrow.addAll(makeArrow(from, to));
        arrows.addAll(arrow);
    }

    window = new Window("", skin);
    window.setBackground(new TextureRegionDrawable(skin.getRegion("gray")));
    window.add(mainTable).center();

    window.setSize(mainTable.getPrefWidth(), mainTable.getPrefHeight());

    // center on stage
    float x = stage.getWidth() * 0.5f;
    float y = stage.getHeight() * 0.5f;
    window.setPosition(x, y, Align.center);

    stage.addActor(window);
}

From source file:skyranger.game.InputController.java

License:Apache License

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    //if right mouse button pressed we act as mouse look
    if (this.screen.mouseLook) {
        Vector3 cameraV3 = this.screen.camera.unproject(new Vector3(screenX, screenY, 0));

        Vector2 cameraV2 = new Vector2(cameraV3.x, cameraV3.y);

        // Take the vector that goes from body origin to mouse in camera
        // space//from  ww w  . j a va 2  s.c o m
        Vector2 bodyPos = this.screen.player.getBody().getPosition();
        Vector2 newCamVector = cameraV2.sub(bodyPos);

        // Now you can set the angle;
        this.screen.player.getBody().setTransform(this.screen.player.getBody().getPosition(),
                newCamVector.angleRad());

    }

    //if left shift pressed and RMB not pressed and we do some mouse drag we move selected object
    if ((this.isLeftShift) && (!this.screen.mouseLook) && (prevDragPos.x > 0) && (prevDragPos.y > 0)) {

        Vector3 dragPos = new Vector3(screenX, screenY, 0);

        this.screen.camera.unproject(dragPos);
        this.screen.camera.unproject(prevDragPos);

        Vector3 delta = new Vector3(dragPos.x - prevDragPos.x, dragPos.y - prevDragPos.y, 0);

        IBox2dObject obj = this.clickCallback.getObject();

        Vector2 objPos = obj.getPosition();

        Vector3 newpos = new Vector3(objPos.x + delta.x, objPos.y + delta.y, 0);

        SwtEvents.changeObjectPosition(obj.getId(), new Vector2(newpos.x, newpos.y));

        this.clickCallback.getObject()
                .setAngle(this.clickCallback.getObject().getBody().getAngle() * MathUtils.radiansToDegrees);

        GameMouseEvent.mouseClickOnObject(obj);

        GameMouseEvent.mouseMoved(new Vector2(screenX, screenY));

    }

    prevDragPos = new Vector3(screenX, screenY, 0);

    return false;
}