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

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

Introduction

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

Prototype

public Vector2 set(float x, float y) 

Source Link

Document

Sets the components of this vector

Usage

From source file:Tabox2D.java

License:Open Source License

private Tabody generateRegularPoly(String name, String type, float x, float y, float rad) {
    // Scale proportions:
    x /= meterSize;/* w w w .  java  2  s . co m*/
    y /= meterSize;
    rad /= meterSize;

    PolygonShape polygonShape;
    BodyDef defPoly = new BodyDef();

    setType(defPoly, type);

    // Generate points:
    List<Vector2> pts = new ArrayList<Vector2>();
    Vector2 p0 = new Vector2(0, rad);

    float conv = MathUtils.degreesToRadians;
    float angleInDeg = polyInfo.get(name + "_angle");
    float cos = MathUtils.cos(conv * angleInDeg);
    float sin = MathUtils.sin(conv * angleInDeg);

    for (int i = 0; i < polyInfo.get(name); i++) {
        pts.add(new Vector2(p0.x, p0.y));
        p0.set(p0.x, p0.y);

        float newX = p0.x * cos - p0.y * sin;
        float newY = p0.x * sin + p0.y * cos;

        p0.x = newX;
        p0.y = newY;
    }

    // Get bounding box:

    float[] rawPoints = new float[pts.size() * 2];
    int pointIndex = 0;
    for (int i = 0; i < rawPoints.length - 1; i += 2) {
        rawPoints[i] = pts.get(pointIndex).x;
        rawPoints[i + 1] = pts.get(pointIndex).y;
        pointIndex++;
    }

    Polygon polyForBox = new Polygon();
    polyForBox.setVertices(rawPoints);

    Rectangle boundingRect = polyForBox.getBoundingRectangle();
    float boxX = boundingRect.x;
    float boxY = boundingRect.y;
    float boxW = boundingRect.getWidth();
    float boxH = boundingRect.getHeight();

    Vector2 aabbCenter = new Vector2(boxX + boxW / 2, boxY + boxH / 2);
    defPoly.position.set(x, y);

    Tabody regularPoly = new Tabody();
    regularPoly.body = world.createBody(defPoly);
    //regularPoly.body.setFixedRotation(true);
    polygonShape = new PolygonShape();

    //polygonShape.setAsBox(w / 2, h / 2);
    for (int i = 0; i < rawPoints.length - 1; i += 2) {
        rawPoints[i] -= aabbCenter.x;
        rawPoints[i + 1] -= aabbCenter.y;
    }
    //rawPoints[0] += 0.5;
    polygonShape.set(rawPoints);

    FixtureDef fixtureBox = new FixtureDef();
    fixtureBox.shape = polygonShape;
    fixtureBox.density = 1;
    fixtureBox.friction = 1;
    fixtureBox.restitution = 0;

    ////////////////////////////////////////
    regularPoly.w = boxW * meterSize;//radius * 2 * meterSize;
    regularPoly.h = boxH * meterSize;//radius * 2 * meterSize;
    regularPoly.fixture = fixtureBox;
    regularPoly.bodyType = "poly";
    ////////////////////////////////////////

    regularPoly.body.createFixture(fixtureBox);
    polygonShape.dispose();
    tabodies.add(regularPoly);
    return regularPoly;
}

From source file:com.agateau.pixelwheels.map.Warper.java

License:Open Source License

private static void warp(float[] mat, float srcX, float srcY, Vector2 output) {
    final float result0 = srcX * mat[0] + srcY * mat[4] + mat[12];
    final float result1 = srcX * mat[1] + srcY * mat[5] + mat[13];
    final float result3 = srcX * mat[3] + srcY * mat[7] + mat[15];
    output.set(result0 / result3, result1 / result3);
}

From source file:com.badlogic.gdx.ai.tests.steer.scene2d.Scene2dTargetInputProcessor.java

License:Apache License

protected void setTargetPosition(int screenX, int screenY) {
    Vector2 pos = target.getPosition();
    target.getStage().screenToStageCoordinates(pos.set(screenX, screenY));
    target.getParent().stageToLocalCoordinates(pos);
    target.setPosition(pos.x, pos.y, Align.center);
}

From source file:com.bladecoder.engine.ui.defaults.ScenePointer.java

License:Apache License

private void getInputUnproject(Viewport v, Vector2 out) {
    out.set(Gdx.input.getX(), Gdx.input.getY());

    v.unproject(out);
}

From source file:com.bladecoder.engineeditor.scneditor.ScnWidget.java

License:Apache License

public void localToWorldCoords(Vector2 coords) {
    localToStageCoordinates(coords);/*  w  ww .ja v a  2  s . c  o  m*/
    getStage().stageToScreenCoordinates(coords);

    tmpV3.set(coords.x, coords.y, 0);
    camera.unproject(tmpV3, getX(), getY(), getWidth(), getHeight());
    coords.set(tmpV3.x, tmpV3.y);
}

From source file:com.bladecoder.engineeditor.scneditor.ScnWidget.java

License:Apache License

public void screenToWorldCoords(Vector2 coords) {
    tmpV2.set(0, 0);/*w w  w.  j a  v  a 2  s.  c om*/
    localToStageCoordinates(tmpV2);
    // getStage().stageToScreenCoordinates(tmpV2);
    tmpV3.set(coords.x, coords.y, 0);
    camera.unproject(tmpV3, tmpV2.x, tmpV2.y, getWidth(), getHeight());
    coords.set(tmpV3.x, tmpV3.y);
}

From source file:com.bladecoder.engineeditor.scneditor.ScnWidget.java

License:Apache License

public void worldToScreenCoords(Vector2 coords) {
    tmpV2.set(getX(), getY());//from  w ww  .  jav a  2  s . c  om
    localToStageCoordinates(tmpV2);
    tmpV3.set(coords.x, coords.y, 0);
    camera.project(tmpV3, tmpV2.x, tmpV2.y, getWidth(), getHeight());
    coords.set(tmpV3.x, tmpV3.y);
    stageToLocalCoordinates(coords);
}

From source file:com.bladecoder.engineeditor.scneditor.ScnWidgetInputListener.java

License:Apache License

@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

    super.touchDown(event, x, y, pointer, button);
    //      EditorLogger.debug("Touch Down - X: " + x + " Y: " + y);

    Scene scn = scnWidget.getScene();// w  ww  .  j  a va2 s  . c o  m
    if (scn == null)
        return false;

    Vector2 p = new Vector2(Gdx.input.getX(), Gdx.input.getY());
    scnWidget.screenToWorldCoords(p);
    org.set(p);

    if (button == Buttons.LEFT) {
        selActor = scnWidget.getSelectedActor();

        if (scn.getPolygonalNavGraph() != null && scnWidget.getShowWalkZone()) { // Check
            // WALKZONE

            // CHECK WALKZONE VERTEXS
            Polygon wzPoly = scn.getPolygonalNavGraph().getWalkZone();
            float verts[] = wzPoly.getTransformedVertices();

            for (int i = 0; i < verts.length; i += 2) {
                if (p.dst(verts[i], verts[i + 1]) < CanvasDrawer.CORNER_DIST) {
                    draggingMode = DraggingModes.DRAGGING_WALKZONE_POINT;
                    vertIndex = i;
                    return true;
                }
            }

            // CHECK FOR WALKZONE DRAGGING
            if (wzPoly.contains(p.x, p.y)) {
                draggingMode = DraggingModes.DRAGGING_WALKZONE;
                undoOrg.set(wzPoly.getX(), wzPoly.getY());
                return true;
            }

        }

        // SELACTOR VERTEXs DRAGGING
        if (selActor != null
                && (!(selActor instanceof SpriteActor) || !((SpriteActor) selActor).isBboxFromRenderer())
                && !(scnWidget.getSelectedActor() instanceof AnchorActor)) {

            Polygon bbox = selActor.getBBox();
            float verts[] = bbox.getTransformedVertices();
            for (int i = 0; i < verts.length; i += 2) {
                if (p.dst(verts[i], verts[i + 1]) < CanvasDrawer.CORNER_DIST) {
                    draggingMode = DraggingModes.DRAGGING_BBOX_POINT;
                    vertIndex = i;
                    return true;
                }
            }
        }

        BaseActor a = scn.getActorAt(p.x, p.y); // CHECK FOR ACTORS

        if (a != null && a != selActor) {
            selActor = a;
            BaseActor da = Ctx.project.getActor(selActor.getId());
            Ctx.project.setSelectedActor(da);
        }

        if (a != null) {
            draggingMode = DraggingModes.DRAGGING_ACTOR;
            undoOrg.set(selActor.getX(), selActor.getY());
            return true;
        }

        // CHECK FOR DRAGGING DEPTH MARKERS
        Vector2 depthVector = scnWidget.getScene().getDepthVector();
        if (depthVector != null) {
            p.set(0, depthVector.x);
            scnWidget.worldToScreenCoords(p);
            if (Vector2.dst(p.x - 40, p.y, x, y) < 50) {
                draggingMode = DraggingModes.DRAGGING_MARKER_0;
                return true;
            }

            p.set(0, depthVector.y);
            scnWidget.worldToScreenCoords(p);
            if (Vector2.dst(p.x - 40, p.y, x, y) < 50) {
                draggingMode = DraggingModes.DRAGGING_MARKER_100;
                return true;
            }
        }

    }

    return true;
}

From source file:com.brashmonkey.spriter.Curve.java

License:Apache License

/** Interpolates the given two points with the given weight and saves the result in the target point.
 * @param a the start point//from  w  ww .  j a v  a2 s  . co  m
 * @param b the end point
 * @param t the weight which lies between 0.0 and 1.0
 * @param target the target point to save the result in */
public void tweenPoint(Vector2 a, Vector2 b, float t, Vector2 target) {
    target.set(tween(a.x, b.x, t), tween(a.y, b.y, t));
}

From source file:com.company.minery.utils.spine.Skeleton.java

License:Open Source License

/** Returns the axis aligned bounding box (AABB) of the region, mesh, and skinned mesh attachments for the current pose.
 * @param offset The distance from the skeleton origin to the bottom left corner of the AABB.
 * @param size The width and height of the AABB. */
public void getBounds(Vector2 offset, Vector2 size) {
    Array<Slot> drawOrder = this.drawOrder;
    float minX = Integer.MAX_VALUE, minY = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE,
            maxY = Integer.MIN_VALUE;
    for (int i = 0, n = drawOrder.size; i < n; i++) {
        Slot slot = drawOrder.get(i);//from  w  ww .  j  a v a  2 s.  co m
        float[] vertices = null;
        Attachment attachment = slot.attachment;
        if (attachment instanceof RegionAttachment) {
            RegionAttachment region = (RegionAttachment) attachment;
            region.updateWorldVertices(slot, false);
            vertices = region.getWorldVertices();

        } else if (attachment instanceof MeshAttachment) {
            MeshAttachment mesh = (MeshAttachment) attachment;
            mesh.updateWorldVertices(slot, true);
            vertices = mesh.getWorldVertices();

        } else if (attachment instanceof SkinnedMeshAttachment) {
            SkinnedMeshAttachment mesh = (SkinnedMeshAttachment) attachment;
            mesh.updateWorldVertices(slot, true);
            vertices = mesh.getWorldVertices();
        }
        if (vertices != null) {
            for (int ii = 0, nn = vertices.length; ii < nn; ii += 5) {
                float x = vertices[ii], y = vertices[ii + 1];
                minX = Math.min(minX, x);
                minY = Math.min(minY, y);
                maxX = Math.max(maxX, x);
                maxY = Math.max(maxY, y);
            }
        }
    }
    offset.set(minX, minY);
    size.set(maxX - minX, maxY - minY);
}