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:halive.shootinoutside.util.VectorUtils.java

public static Vector2 getUnitVector(Vector2 v) {
    Vector2 uv = new Vector2();
    double vx = v.x / (Math.sqrt(Math.pow(v.x, 2.0D) + Math.pow(v.y, 2.0D)));
    double vy = v.y / (Math.sqrt(Math.pow(v.x, 2.0D) + Math.pow(v.y, 2.0D)));
    uv.set((float) vx, (float) vy);
    return uv;//from w  w w  .j  a va2  s.  com
}

From source file:io.piotrjastrzebski.dungen.Utils.java

License:Apache License

public static Vector2 pointInEllipse(float width, float height, Vector2 out) {
    float t = (float) (MathUtils.random() * Math.PI * 2);
    float u = MathUtils.random() + MathUtils.random();
    float r = (u > 1) ? (2 - u) : u;
    out.set(width * r * MathUtils.cos(t) / 2, height * r * MathUtils.sin(t) / 2);
    return out;//  ww w . ja v  a2s. c om
}

From source file:jordanlw.gdxGame.Game.java

License:Open Source License

private void handleInput(Vector2 clickRelativePlayer, Vector2 mousePressedPosition, Vector2 distanceToMouse,
        Vector2 bulletVector) {//from   w  w  w .j  a v a  2 s  .c  o  m
    Player player = getLocalPlayer();
    Integer movementSpeed = 250;
    Vector2 deltaPosition = new Vector2(0, 0);
    if (Gdx.input.isKeyPressed(Input.Keys.W)) {
        deltaPosition.y += movementSpeed;
        movementThisFrame = true;
    }
    if (Gdx.input.isKeyPressed(Input.Keys.S)) {
        deltaPosition.y -= movementSpeed;
        movementThisFrame = true;
    }
    if (Gdx.input.isKeyPressed(Input.Keys.A)) {
        deltaPosition.x -= movementSpeed;
        movementThisFrame = true;
    }
    if (Gdx.input.isKeyPressed(Input.Keys.D)) {
        deltaPosition.x += movementSpeed;
        movementThisFrame = true;
    }
    deltaPosition = deltaPosition.nor().scl(movementSpeed * Gdx.graphics.getDeltaTime());
    player.position.setPosition(player.position.getPosition(new Vector2()).add(deltaPosition));

    if (Gdx.input.isButtonPressed(Input.Buttons.LEFT))
        mousePressedPosition.set(Gdx.input.getX(), camera.viewportHeight - Gdx.input.getY());
    clickRelativePlayer.set(mousePressedPosition.x - player.position.x,
            -(mousePressedPosition.y - player.position.y));
    distanceToMouse.x = (float) Math.sqrt(
            clickRelativePlayer.x * clickRelativePlayer.x + clickRelativePlayer.y * clickRelativePlayer.y);
    bulletVector.x = ((windowSize.x + windowSize.y) * clickRelativePlayer.x) + player.position.x;
    bulletVector.y = ((windowSize.x + windowSize.y) * -clickRelativePlayer.y) + player.position.y;
}

From source file:net.dermetfan.gdx.scenes.scene2d.ui.ScrollPaneSnapAction.java

License:Apache License

/** @param slot is set to the Slot closest to the target with the visual scroll amount
 *  @return true if a slot was found */
public boolean findClosestSlot(Vector2 slot) {
    float targetX = this.targetX.get(pane) + pane.getVisualScrollX(),
            targetY = this.targetY.get(pane) + pane.getVisualScrollY();
    float closestDistance = Float.POSITIVE_INFINITY;
    boolean found = false;
    for (int i = 1; i < slots.size; i += 2) {
        float slotX = slots.get(i - 1), slotY = slots.get(i);
        float distance = Vector2.dst2(targetX, targetY, slotX, slotY);
        if (distance <= closestDistance) {
            closestDistance = distance;/*from  w  w  w.j a  v a 2 s  .c o  m*/
            slot.set(slotX, slotY);
            found = true;
        }
    }
    return found;
}

From source file:net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** @see #size(Shape) */
public static Vector2 size(Shape shape, Vector2 output) {
    if (shape.getType() == Type.Circle) // no call to #vertices(Shape) for performance
        return output.set(shape.getRadius() * 2, shape.getRadius() * 2);
    else if (cache.containsKey(shape))
        return output.set(cache.get(shape).width, cache.get(shape).height);
    return output.set(width(shape), height(shape));
}

From source file:net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** @return the relative position of the given Shape to its Body
 *  @param rotation the rotation of the body in radians */
public static Vector2 positionRelative(Shape shape, float rotation, Vector2 output) {
    if (cache.containsKey(shape)) {
        ShapeCache sc = cache.get(shape);
        return rotate(output.set(sc.maxX - sc.width / 2, sc.maxY - sc.height / 2), rotation); // faster
    }/* w  ww .ja  v  a  2s  .c o  m*/
    return rotate(output.set(maxX(shape) - width(shape) / 2, maxY(shape) - height(shape) / 2), rotation);
}

From source file:net.dermetfan.utils.libgdx.math.GeometryUtils.java

License:Apache License

/** rotates {@code point} by {@code radians} around [0:0] (local rotation)
 *  @param point the point to rotate/* www.j  a v  a2s. c  o  m*/
 *  @param radians the rotation
 *  @return the given {@code point} rotated by {@code radians} */
public static Vector2 rotate(Vector2 point, float radians) {
    float xx = point.x, xy = point.y, yx = point.x, yy = point.y;
    xx = xx * cos(radians) - xy * sin(radians);
    yy = yx * sin(radians) + yy * cos(radians);
    return point.set(xx, yy);
}

From source file:net.onedaybeard.keyflection.demo.DemoUI.java

License:Apache License

@Override
public void render() {
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClearColor(background.r, background.g, background.b, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    if (Gdx.input.isTouched()) {
        Vector2 stageCoords = Vector2.tmp;
        stage.screenToStageCoordinates(stageCoords.set(Gdx.input.getX(), Gdx.input.getY()));
        Actor actor = stage.hit(stageCoords.x, stageCoords.y, true);
        if (actor instanceof Image)
            ((Image) actor).setColor((float) Math.random(), (float) Math.random(), (float) Math.random(),
                    0.5f + 0.5f * (float) Math.random());
    }/*from   w ww.  j a v a  2s  . c  o  m*/

    Array<Actor> actors = stage.getActors();
    int len = actors.size;
    if (rotateSprites) {
        for (int i = 0; i < len; i++)
            actors.get(i).rotate(Gdx.graphics.getDeltaTime() * 10);
    }

    scale += vScale * Gdx.graphics.getDeltaTime();
    if (scale > 1) {
        scale = 1;
        vScale = -vScale;
    }
    if (scale < 0.5f) {
        scale = 0.5f;
        vScale = -vScale;
    }

    len = images.size;
    for (int i = 0; i < len; i++) {
        Image img = images.get(i);
        if (rotateSprites)
            img.rotate(-40 * Gdx.graphics.getDeltaTime());
        else
            img.setRotation(0);

        if (scaleSprites) {
            img.setScale(scale);
        } else {
            img.setScale(1);
        }
        img.invalidate();
    }

    stage.draw();

    renderer.begin(ShapeType.Point);
    renderer.setColor(1, 0, 0, 1);
    len = actors.size;
    for (int i = 0; i < len; i++) {
        Group group = (Group) actors.get(i);
        renderer.point(group.getX() + group.getOriginX(), group.getY() + group.getOriginY(), 0);
    }
    renderer.end();

    if (drawHud) {
        fps.setText("fps: " + Gdx.graphics.getFramesPerSecond() + ", actors " + images.size + ", groups "
                + actors.size);
        ui.draw();
    }
}

From source file:net.zzorn.minild37.utils.SimplexGradientNoise.java

License:Open Source License

/**
 * 2D simplex noise with derivatives./*from   w w  w . ja  v  a 2  s  .c  o m*/
 * If the last two arguments are not null, the analytic derivative
 * (the 2D gradient of the scalar noise field) is also calculated.
 */
public static final double sdnoise2(final double x, final double y, final Vector2 dnoise) {
    double n0, n1, n2; /* Noise contributions from the three simplex corners */

    double t0, t1, t2, x1, x2, y1, y2;
    double t20, t40, t21, t41, t22, t42;
    double temp0, temp1, temp2, noise;
    double ga0x, ga0y, ga1x, ga1y, ga2x, ga2y;

    /* Skew the input space to determine which simplex cell we're in */
    double s = (x + y) * F2; /* Hairy factor for 2D */
    double xs = x + s;
    double ys = y + s;
    int ii, i = fastFloor(xs);
    int jj, j = fastFloor(ys);

    double t = (i + j) * G2;
    double X0 = i - t; /* Unskew the cell origin back to (x,y) space */
    double Y0 = j - t;
    double x0 = x - X0; /* The x,y distances from the cell origin */
    double y0 = y - Y0;

    /* For the 2D case, the simplex shape is an equilateral triangle.
     * Determine which simplex we are in. */
    int i1, j1; /* Offsets for second (middle) corner of simplex in (i,j) coords */
    if (x0 > y0) {
        i1 = 1;
        j1 = 0;
    } /* lower triangle, XY order: (0,0)->(1,0)->(1,1) */ else {
        i1 = 0;
        j1 = 1;
    } /* upper triangle, YX order: (0,0)->(0,1)->(1,1) */

    /* A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
     * a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
     * c = (3-sqrt(3))/6   */
    x1 = x0 - i1 + G2; /* Offsets for middle corner in (x,y) unskewed coords */
    y1 = y0 - j1 + G2;
    x2 = x0 - 1.0 + 2.0 * G2; /* Offsets for last corner in (x,y) unskewed coords */
    y2 = y0 - 1.0 + 2.0 * G2;

    /* Wrap the integer indices at 256, to avoid indexing perm[] out of bounds */
    ii = i & 0xFF;
    jj = j & 0xFF;

    /* Calculate the contribution from the three corners */
    t0 = 0.5 - x0 * x0 - y0 * y0;
    if (t0 < 0.0) {
        t40 = t20 = t0 = n0 = ga0x = ga0y = 0.0; /* No influence */
    } else {
        int h = perm[(ii + perm[jj]) & 0xFF] & 7;
        ga0x = grad2lut[h][0];
        ga0y = grad2lut[h][1];

        t20 = t0 * t0;
        t40 = t20 * t20;
        n0 = t40 * (ga0x * x0 + ga0y * y0);
    }

    t1 = 0.5 - x1 * x1 - y1 * y1;
    if (t1 < 0.0)
        t21 = t41 = t1 = n1 = ga1x = ga1y = 0.0; /* No influence */
    else {
        int h = perm[(ii + i1 + perm[(jj + j1) & 0xFF]) & 0xFF] & 7;
        ga1x = grad2lut[h][0];
        ga1y = grad2lut[h][1];

        t21 = t1 * t1;
        t41 = t21 * t21;
        n1 = t41 * (ga1x * x1 + ga1y * y1);
    }

    t2 = 0.5 - x2 * x2 - y2 * y2;
    if (t2 < 0.0)
        t42 = t22 = t2 = n2 = ga2x = ga2y = 0.0; /* No influence */
    else {
        int h = perm[(ii + 1 + perm[(jj + 1) & 0xFF]) & 0xFF] & 7;
        ga2x = grad2lut[h][0];
        ga2y = grad2lut[h][1];

        t22 = t2 * t2;
        t42 = t22 * t22;
        n2 = t42 * (ga2x * x2 + ga2y * y2);
    }

    /* Add contributions from each corner to get the final noise value.
     * The result is scaled to return values in the interval [-1,1]. */
    noise = 40.0 * (n0 + n1 + n2);

    /* Compute derivative, if requested by supplying a non-null pointer for the last argument */
    if (dnoise != null) {
        /*  A straight, unoptimised calculation would be like:
        *    dx = -8.0 * t20 * t0 * x0 * ( gx0 * x0 + gy0 * y0 ) + t40 * gx0;
        *    dy = -8.0 * t20 * t0 * y0 * ( gx0 * x0 + gy0 * y0 ) + t40 * gy0;
        *    dx += -8.0 * t21 * t1 * x1 * ( gx1 * x1 + gy1 * y1 ) + t41 * gx1;
        *    dy += -8.0 * t21 * t1 * y1 * ( gx1 * x1 + gy1 * y1 ) + t41 * gy1;
        *    dx += -8.0 * t22 * t2 * x2 * ( gx2 * x2 + gy2 * y2 ) + t42 * gx2;
        *    dy += -8.0 * t22 * t2 * y2 * ( gx2 * x2 + gy2 * y2 ) + t42 * gy2;
        */
        temp0 = t20 * t0 * (ga0x * x0 + ga0y * y0);
        double dx = temp0 * x0;
        double dy = temp0 * y0;
        temp1 = t21 * t1 * (ga1x * x1 + ga1y * y1);
        dx += temp1 * x1;
        dy += temp1 * y1;
        temp2 = t22 * t2 * (ga2x * x2 + ga2y * y2);
        dx += temp2 * x2;
        dy += temp2 * y2;
        dx *= -8.0;
        dy *= -8.0;
        dx += t40 * ga0x + t41 * ga1x + t42 * ga2x;
        dy += t40 * ga0y + t41 * ga1y + t42 * ga2y;
        dx *= 40.0; /* Scale derivative to match the noise scaling */
        dy *= 40.0;
        dnoise.set((float) dx, (float) dy);
    }
    return noise;
}

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

License:Open Source License

public static Vector2 getScreenCoordinates(OrthographicCamera camera, float cameraX, float cameraY, float zoom,
        Vector2 result) {

    float mouseXRelativeToScreenCenter = cameraX - camera.position.x;
    float mouseYRelativeToScreenCenter = cameraY - camera.position.y;

    mouseXRelativeToScreenCenter /= zoom;
    mouseYRelativeToScreenCenter /= zoom;

    mouseXRelativeToScreenCenter = mouseXRelativeToScreenCenter * 2 / camera.viewportWidth;
    mouseYRelativeToScreenCenter = mouseYRelativeToScreenCenter * 2 / camera.viewportHeight;

    float screenX = mouseXRelativeToScreenCenter * (Gdx.graphics.getWidth() / 2)
            + (float) Gdx.graphics.getWidth() / 2;
    float screenY = mouseYRelativeToScreenCenter * (Gdx.graphics.getHeight() / 2)
            + (float) Gdx.graphics.getHeight() / 2;

    result.set(screenX, screenY);
    return result;
}