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

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

Introduction

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

Prototype

public Vector2 add(float x, float y) 

Source Link

Document

Adds the given components to this vector

Usage

From source file:org.ams.core.CameraNavigator.java

License:Open Source License

/**
 * @param zoom    the cameras zoom will be set to this value.
 * @param screenX the screen x-coordinate you want to zoom in on(or away from).
 * @param screenY the screen y-coordinate you want to zoom in on(or away from).
 */// w  w w . j av a2 s.c o  m
public void setZoom(float zoom, float screenX, float screenY) {
    Vector2 touchWorldBefore = getPositionOfTouch(screenX, screenY);
    touchWorldBefore.add(camera.position.x, camera.position.y);

    camera.zoom = zoom;
    camera.update();

    Vector2 touchWorldAfter = getPositionOfTouch(screenX, screenY);
    touchWorldAfter.add(camera.position.x, camera.position.y);

    camera.translate(touchWorldBefore.sub(touchWorldAfter));
    camera.update();
}

From source file:org.ams.core.CoordinateHelper.java

License:Open Source License

public static Vector2 getWorldCoordinates(OrthographicCamera camera, float screenX, float screenY) {

    float halfWidth = Gdx.graphics.getWidth() * 0.5f;
    float halfHeight = Gdx.graphics.getHeight() * 0.5f;

    Vector2 pos = new Vector2(screenX - halfWidth, halfHeight - screenY);
    pos.scl(1f / halfWidth, 1f / halfHeight);

    pos.scl(camera.zoom);/*from www .  j a  v a 2 s  . co  m*/

    // convert to world units relative to center of screen
    pos.scl(camera.viewportWidth * 0.5f, camera.viewportHeight * 0.5f);

    return pos.add(camera.position.x, camera.position.y);
}

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

License:Open Source License

@Override
public Array<Vector2> getVerticesRotatedScaledAndTranslated(float rotation, float scale, float transX,
        float transY) {

    for (int i = 0; i < vertices.size; i++) {
        Vector2 w = verticesRotatedAndTranslated.items[i];
        w.set(vertices.items[i]);/*w  w w  .ja  va 2  s .c o  m*/
        w.rotateRad(rotation);
        w.scl(scale);
        w.add(transX, transY);
    }
    return verticesRotatedAndTranslated;
}

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

License:Open Source License

private Vector2 getOrigin(Array<TexturePolygon> texturePolygons, int origin) {
    Rectangle boundingRectangle = new Rectangle();
    boolean initialized = false;
    for (TexturePolygon texturePolygon : texturePolygons) {
        if (!initialized) {
            initialized = true;//ww w  . j  ava2s. c  om
            boundingRectangle.set(texturePolygon.getBoundingRectangle());
        } else
            boundingRectangle.merge(texturePolygon.getBoundingRectangle());
    }

    Vector2 v = boundingRectangle.getCenter(new Vector2());
    v.scl(texturePolygons.first().getTextureScale());

    TextureRegion firstRegion = texturePolygons.first().getTextureRegion();
    float firstTextureScale = texturePolygons.first().getTextureScale();
    v.add(firstRegion.getRegionWidth() * firstTextureScale * 0.5f,
            firstRegion.getRegionHeight() * firstTextureScale * 0.5f);

    return v;
}

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

License:Open Source License

/**
 * This method lets you align the texture so that if two or more different {@link #TexturePolygon}'s are overlapping
 * the texture still looks seamless. They must have the same scale and textureScale for this to work.
 *
 * @param extraTranslationX Extra translation allows you to move the texture around within the polygon.
 *                          Use the same value for all the {@link #TexturePolygon}'s  you wish to align.
 * @param extraTranslationY Extra translation allows you to move the texture around within the polygon.
 *                          Use the same value for all the {@link #TexturePolygon}'s you wish to align.
 * @return this for chaining./*from   w w  w  .  j a v a 2s .  c o  m*/
 */
public TexturePolygon alignTexture(float extraTranslationX, float extraTranslationY) {
    Vector2 v = new Vector2(position);
    v.rotateRad(-textureAngle - angleRad);

    v.add(extraTranslationX, extraTranslationY);

    setTextureTranslation(v);
    return this;
}

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

License:Open Source License

/**
 * Instantly show a tip on the screen./*from  w  w w  .  j av a2  s.co 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:org.ams.testapps.paintandphysics.physicspuzzle.PhysicsPuzzle.java

License:Open Source License

/**
 * Finds the vertices for the chain body that is the "ground" for the active blocks.
 * It forms an outline around the walls, floor and locked blocks.
 *///  w ww.ja v a2s.  c om
private Vector2[] computeChainVertices() {

    Array<Vector2> platformVertices = new Array<Vector2>();

    platformVertices.addAll(wallVerticesForGroundBody);

    // first i find all the platforms that the locked blocks form
    Array<Array<Integer>> platforms = new Array<Array<Integer>>();
    {
        Array<Integer> platform = new Array<Integer>();
        platforms.add(platform);

        int row = platformLevels.get(0);
        int previousRow = row;
        platform.add(row);

        for (int i = 1; i < platformLevels.size; i++) {
            row = platformLevels.get(i);
            if (row != previousRow) {
                platform = new Array<Integer>();
                platforms.add(platform);
            }
            platform.add(row);

            previousRow = row;
        }
    }

    // for each platform 2 vertices is added
    // special treatment for platforms with index -1(a platform on the floor)
    // and for platforms touching the left or right wall
    int i = 0;
    int previousPlatformRow = -1;
    for (Array<Integer> platform : platforms) {
        int platformColumnBegin = i; // inclusive
        int platformColumnEnd = i + platform.size - 1; // not inclusive
        int platformRow = platform.first();

        Vector2 beginPos, endPos;

        if (platformRow >= 0) {
            PPPolygon platformBegin = getBlock(platformRow, platformColumnBegin);
            beginPos = new Vector2(platformBegin.getPhysicsThing().getBody().getPosition());
            beginPos.add(-physicsBlockDim * 0.5f, physicsBlockDim * 0.5f);

            PPPolygon platformEnd = getBlock(platformRow, platformColumnEnd);
            endPos = new Vector2(platformEnd.getPhysicsThing().getBody().getPosition());
            endPos.add(physicsBlockDim * 0.5f, physicsBlockDim * 0.5f);
        } else {
            // platform is on the floor
            // we get coordinates for an equally wide platform just
            // one row higher up, then lower height by one block height

            PPPolygon platformBegin = getBlock(0, platformColumnBegin);
            beginPos = new Vector2((Vector2) platformBegin.getUserData());
            beginPos.add(-physicsBlockDim * 0.5f, physicsBlockDim * 0.5f - blockDim);

            PPPolygon platformEnd = getBlock(0, platformColumnEnd);
            endPos = new Vector2((Vector2) platformEnd.getUserData());
            endPos.add(physicsBlockDim * 0.5f, physicsBlockDim * 0.5f - blockDim);

        }

        if (platformRow < previousPlatformRow) {
            beginPos.sub((blockDim - physicsBlockDim), 0);
        } else {
            if (platformVertices.size > 0 && platformColumnBegin != 0) {
                platformVertices.peek().add(blockDim - physicsBlockDim, 0);
            }
        }

        if (platformColumnEnd == columns - 1) {
            // platform touching right wall, adjust slightly
            endPos.add(blockDim - physicsBlockDim, 0);
        }
        if (platformColumnBegin == 0) {
            // platform touching left wall, adjust slightly
            beginPos.sub(blockDim - physicsBlockDim, 0);
        }

        // make sure i don't add vertices that are too close to the previous
        // (this is only an issue for the top row)

        boolean distanceOk = platformVertices.peek().dst(beginPos) > 0.001f;
        distanceOk &= platformVertices.first().dst(beginPos) > 0.001f;

        if (platformVertices.size == 0 || distanceOk)
            platformVertices.add(beginPos);

        distanceOk = platformVertices.peek().dst(endPos) > 0.001f;
        distanceOk &= platformVertices.first().dst(endPos) > 0.001f;

        if (platformVertices.size == 0 || distanceOk)
            platformVertices.add(endPos);

        i += platform.size;
        previousPlatformRow = platformRow;

    }

    return platformVertices.toArray(Vector2.class);
}

From source file:org.ams.testapps.prettypaint.CircleAndBackground.java

License:Open Source License

/**
 * @param zoom    the cameras zoom will be set to this value.
 * @param screenX the screen x-coordinate you want to zoom in on(or away from).
 * @param screenY the screen y-coordinate you want to zoom in on(or away from).
 *///from   w w  w  .  j  ava 2s .co  m
public void setZoom(float zoom, int screenX, int screenY) {
    Vector2 touchWorldBefore = getPositionOfTouch(screenX, screenY);
    touchWorldBefore.add(camera.position.x, camera.position.y);

    camera.zoom = zoom;
    camera.update();

    Vector2 touchWorldAfter = getPositionOfTouch(screenX, screenY);
    touchWorldAfter.add(camera.position.x, camera.position.y);

    camera.translate(touchWorldBefore.sub(touchWorldAfter));
    camera.update();

}

From source file:org.ams.testapps.prettypaint.JaggedPolygon.java

License:Open Source License

/**
 * @param zoom    the cameras zoom will be set to this value.
 * @param screenX the screen x-coordinate you want to zoom in on(or away from).
 * @param screenY the screen y-coordinate you want to zoom in on(or away from).
 *//*w  w  w  .jav a 2  s  .c  om*/
public void setZoom(float zoom, int screenX, int screenY) {
    Vector2 touchWorldBefore = getPositionOfTouch(screenX, screenY);
    touchWorldBefore.add(camera.position.x, camera.position.y);

    camera.zoom = zoom;
    camera.update();

    Vector2 touchWorldAfter = getPositionOfTouch(screenX, screenY);
    touchWorldAfter.add(camera.position.x, camera.position.y);

    camera.translate(touchWorldBefore.sub(touchWorldAfter));
    camera.update();
}

From source file:org.destinationsol.game.chunk.ChunkFiller.java

License:Apache License

/**
 * Fill the background of a given chunk with floating junk.
 *
 * @param game    The {@link SolGame} instance to work with
 * @param chunk   The coordinates of the chunk
 * @param remover/*from   www .ja  v a 2  s.  co  m*/
 * @param farBg   Determines which of the background layers should be filled. <code>true</code> fills the layers furthest away, <code>false</code> fills the closer one.
 */
public void fill(SolGame game, Vector2 chunk, RemoveController remover, boolean farBg) {
    if (DebugOptions.NO_OBJS)
        return;

    // Determine the center of the chunk by multiplying the chunk coordinates with the chunk size and adding half a chunk's size
    Vector2 chCenter = new Vector2(chunk);
    chCenter.scl(Const.CHUNK_SIZE);
    chCenter.add(Const.CHUNK_SIZE / 2, Const.CHUNK_SIZE / 2);

    // Define the density multiplier for different layers of junk in the far background
    float[] densityMul = { 1 };

    // Get the environment configuration
    SpaceEnvConfig conf = getConfig(game, chCenter, densityMul, remover, farBg);

    if (farBg) {
        fillFarJunk(game, chCenter, remover, DraLevel.FAR_DECO_3, conf, densityMul[0]);
        fillFarJunk(game, chCenter, remover, DraLevel.FAR_DECO_2, conf, densityMul[0]);
        fillFarJunk(game, chCenter, remover, DraLevel.FAR_DECO_1, conf, densityMul[0]);
    } else {
        fillDust(game, chCenter, remover);
        fillJunk(game, remover, conf, chCenter);
    }
}