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:org.ams.core.Util.java

License:Open Source License

/**
 * create a polygon with the shape of a circle
 */// w w  w .  j  a va2s .  c  o  m
public static Array<Vector2> makeCircle(Vector2 origin, float radius) {
    Vector2 v2 = new Vector2();
    Vector2 v3 = new Vector2();
    float verticeCount = (int) (36 + radius * 8);
    v2.set(radius, 0);
    // calculate number of segments and length of each segment
    float radiansPerSegment = 0;
    float segmentLength = 0;
    while (segmentLength < 0.05F && verticeCount > 5) {
        verticeCount--;
        radiansPerSegment = (float) (2 * Math.PI / verticeCount);
        v3.x = radius;
        v3.y = 0;
        v3.rotateRad(radiansPerSegment);
        segmentLength = v2.dst(v3);
    }
    Array<Vector2> vertices = new Array<Vector2>(true, (int) verticeCount, Vector2.class);
    for (int i = 0; i < verticeCount; i++) {
        v2.x = radius;
        v2.y = 0;
        v2.rotateRad(i * radiansPerSegment);
        v2.x += origin.x;
        v2.y += origin.y;
        vertices.add(new Vector2(v2));
    }
    return vertices;
}

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

License:Open Source License

/**
 * With texture angle you can rotate the texture without rotating the edges of the polygon.
 *
 * @param textureAngle the angle of the texture in radians.
 *///from  w  ww  .  j a v a  2 s  .  c om
public void setTextureAngle(float textureAngle) {
    Vector2 v = new Vector2();
    for (int i = 0; i < polygonRegions.size; i++) {
        PolygonRegion toReplace = polygonRegions.get(i);

        float[] vertices = toReplace.getVertices();

        for (int j = 0; j < vertices.length;) {
            v.set(vertices[j], vertices[j + 1]);
            v.rotateRad(this.textureAngle - textureAngle);
            vertices[j] = v.x;
            vertices[j + 1] = v.y;
            j += 2;
        }

        PolygonRegion replacement = new PolygonRegion(toReplace.getRegion(), vertices,
                toReplace.getTriangles());

        polygonRegions.set(i, replacement);

    }
    this.textureAngle = textureAngle;
}

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

License:Open Source License

/** Set the texture to be upright at the polygons current angle. */
public void setTextureUprightForCurrentAngle() {
    Vector2 v = new Vector2();
    for (int i = 0; i < polygonRegions.size; i++) {
        PolygonRegion toReplace = polygonRegions.get(i);

        float[] vertices = toReplace.getVertices();

        for (int j = 0; j < vertices.length;) {
            v.set(vertices[j], vertices[j + 1]);
            v.rotateRad(textureAngle + angleRad);
            vertices[j] = v.x;/*from   ww  w .  j ava2s  . co m*/
            vertices[j + 1] = v.y;
            j += 2;
        }

        PolygonRegion replacement = new PolygonRegion(toReplace.getRegion(), vertices,
                toReplace.getTriangles());

        polygonRegions.set(i, replacement);

    }
    textureAngle = -angleRad;

}

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

License:Open Source License

/** Get the height of the topmost part of a card. */
public Vector2 getHeightOfCard(Body body, Vector2 result) {
    float hh = cardHouseDef.cardHeight * 0.5f;
    float hw = cardHouseDef.cardWidth * 0.5f;

    float top = body.getPosition().y;
    top += Math.max(Math.abs(Math.cos(body.getAngle()) * hh), hw);

    float angle = body.getAngle();
    float sin = (float) Math.sin(angle);
    float cos = (float) Math.cos(angle);

    float x = body.getPosition().x;
    if (cos < 0)
        x += sin * hh;//from   ww  w.  jav a2  s .  c o  m
    else
        x -= sin * hh;

    return result.set(x, top);
}

From source file:org.bladecoder.bladeengine.ui.Pointer.java

License:Apache License

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

    v.unproject(out);//  ww  w . ja v  a2 s.  c o m

    //      if (out.x >= v.getWorldWidth())
    //         out.x = v.getWorldWidth() - 1;
    //      else if (out.x < 0)
    //         out.x = 0;
    //
    //      if (out.y >= v.getWorldHeight())
    //         out.y = v.getWorldHeight() - 1;
    //      else if (out.y < 0)
    //         out.y = 0;
}

From source file:org.bladecoder.bladeengine.ui.SceneViewport.java

License:Apache License

public void getInputUnProject(Vector2 out) {
    out.set(Gdx.input.getX(), Gdx.input.getY());

    unproject(out);
}

From source file:org.catrobat.catroid.sensing.CollisionDetection.java

License:Open Source License

public static double collidesWithFinger(Look look) {
    /*The touching points are expanded to circles with Constants.COLLISION_WITH_FINGER_TOUCH_RADIUS
    to simulate the real touching area of the finger (which is not only a point, but a small area)
    To improve performance first check if the circle is contained in the bounding box of that polygon
    If that's the case, check if any line segment of the polygon intersects with the circle, to evaluate
    a collision. If there is no intersection, but the circle is contained in an uneven number of polygons
    it means that there still is a collision
    Example:/*  w w w . ja v a 2s. c o m*/
       _______
      |  ___ x|  point "o" is not colliding, while point "x" is colliding
      | | o | |
      | |___| |
      |_______|
    */
    ArrayList<PointF> touchingPoints = TouchUtil.getCurrentTouchingPoints();
    Vector2 start = new Vector2();
    Vector2 end = new Vector2();
    Vector2 center = new Vector2();
    float touchRadius = Constants.COLLISION_WITH_FINGER_TOUCH_RADIUS;

    for (PointF point : touchingPoints) {
        center.set(point.x, point.y);
        int containedIn = 0;
        for (Polygon polygon : look.getCurrentCollisionPolygon()) {
            Rectangle boundingRectangle = polygon.getBoundingRectangle();
            boundingRectangle.x -= touchRadius;
            boundingRectangle.y -= touchRadius;
            boundingRectangle.width += touchRadius * 2;
            boundingRectangle.height += touchRadius * 2;
            if (boundingRectangle.contains(point.x, point.y)) {
                float[] vertices = polygon.getTransformedVertices();
                int f = 0;
                while (f < polygon.getVertices().length - 3) {
                    start.x = vertices[f++];
                    start.y = vertices[f++];
                    end.x = vertices[f++];
                    end.y = vertices[f++];
                    if (Intersector.intersectSegmentCircle(start, end, center, touchRadius * touchRadius)) {
                        return 1d;
                    }
                }
                start.x = vertices[vertices.length - 2];
                start.y = vertices[vertices.length - 1];
                end.x = vertices[0];
                end.y = vertices[1];
                if (Intersector.intersectSegmentCircle(start, end, center, touchRadius * touchRadius)) {
                    return 1d;
                }
                if (polygon.contains(point.x, point.y)) {
                    containedIn++;
                }
            }
        }
        if (containedIn % 2 != 0) {
            return 1d;
        }
    }
    return 0d;
}

From source file:org.destinationsol.game.planet.PlanetManager.java

License:Apache License

private boolean recoverObj(SolObject obj, float toNp, float npMinH) {
    if (npMinH < toNp)
        return false;
    if (!(obj instanceof SolShip))
        return false;
    SolShip ship = (SolShip) obj;//  www  . jav  a2s .  c om
    Hull hull = ship.getHull();
    if (hull.config.getType() == HullConfig.Type.STATION)
        return false;
    float fh = myNearestPlanet.getFullHeight();
    Vector2 npPos = myNearestPlanet.getPos();
    Vector2 toShip = SolMath.distVec(npPos, ship.getPosition());
    float len = toShip.len();
    if (len == 0) {
        toShip.set(0, fh);
    } else {
        toShip.scl(fh / len);
    }
    toShip.add(npPos);
    Body body = hull.getBody();
    body.setTransform(toShip, 0);
    body.setLinearVelocity(Vector2.Zero);
    SolMath.free(toShip);
    return true;
}

From source file:skyranger.game.userInterface.Cursor.java

License:Apache License

Vector2 unproject(Camera cam, Vector2 vector) {
    Vector3 tmp = new Vector3();
    tmp.set(vector.x, vector.y, 0f);//from ww  w  . j a va 2 s . com
    cam.unproject(tmp);
    vector.set(tmp.x, tmp.y);
    return vector;
}

From source file: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 w  w  .  j a v a 2 s .c o m*/
        float[] vertices = null;
        Attachment attachment = slot.attachment;
        if (attachment instanceof RegionAttachment) {
            vertices = ((RegionAttachment) attachment).updateWorldVertices(slot, false);

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

        } else if (attachment instanceof SkinnedMeshAttachment) {
            vertices = ((SkinnedMeshAttachment) attachment).updateWorldVertices(slot, true);
        }
        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);
}