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

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

Introduction

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

Prototype

@Override
    public float len() 

Source Link

Usage

From source file:at.juggle.games.counting.screens.MenuScreen.java

License:Apache License

@Override
public void render(float delta) {
    handleInput();//from  w  ww  .  ja  v a2s .  c  o  m
    // camera:
    cam.update();
    batch.setProjectionMatrix(cam.combined);

    Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    // draw bgImage ...
    batch.draw(backgroundImage, 0, 0, CountingGame.GAME_WIDTH, CountingGame.GAME_HEIGHT);

    for (int i = 0; i < background.length; i++) {
        for (int j = 0; j < background.length; j++) {
            if (i != j && background[i] != null && background[j] != null) {
                Vector2 dist = background[j].dist(background[i]);
                if (dist.len() < background[j].getHeight()) {
                    float d = (background[i].getHeight() - dist.len()) / background[i].getHeight();
                    background[j].setPosition(background[j].getX() + dist.x * d,
                            background[j].getY() + dist.y * d);
                    background[i].setPosition(background[i].getX() - dist.x * d,
                            background[i].getY() - dist.y * d);
                }
            }

        }
        background[i].setY(background[i].getY() + delta * 100f * background[i].getSpeed());
        background[i].draw(batch, delta);
        if (background[i].getY() > CountingGame.GAME_HEIGHT * 1.1f)
            background[i].setPosition(
                    CountingGame.GAME_WIDTH / 2f + ((float) (Math.random() * CountingGame.GAME_WIDTH / 2f)),
                    -((float) (Math.random() * CountingGame.GAME_HEIGHT)));
    }

    // draw buttons ...
    startMengen.draw(batch);
    startZaehlen.draw(batch);

    // draw Strings ...
    for (int i = 2; i < menuStrings.length; i++) {

        if (i == currentMenuItem)
            menuFont.setColor(0.2f, 0.2f, 0.8f, 1f);
        else
            menuFont.setColor(0f, 0f, 0f, 1f);
        String menuString = menuStrings[i];
        if (menuString.startsWith(ballons))
            menuString += ": " + getNumberOfBallons() + " max.";
        else if (menuString.startsWith(difficulty))
            menuString += ": " + ((CountingGame.difficulty == 0) ? "einfach"
                    : ((CountingGame.difficulty == 1) ? "mittel" : "schwer"));
        menuFont.draw(batch, menuString, offsetLeft, CountingGame.GAME_HEIGHT - offsetTop - i * offsetY);
    }
    batch.end();
}

From source file:com.agateau.pixelwheels.bonus.MissileGuidingSystem.java

License:Open Source License

private void move() {
    Vector2 velocity = mBody.getLinearVelocity();
    float speed = velocity.len();

    float delta = MAX_SPEED - speed;
    float impulse = delta * mBody.getMass();
    mTmp.set(impulse, 0).rotateRad(mBody.getAngle());
    mBody.applyLinearImpulse(mTmp, mBody.getWorldCenter(), true);

    Vector2 latImpulse = Box2DUtils.getLateralVelocity(mBody).scl(-mBody.getMass());
    mBody.applyLinearImpulse(latImpulse, mBody.getWorldCenter(), true);
}

From source file:com.agateau.pixelwheels.racer.Wheel.java

License:Open Source License

private void updateFriction() {
    // Kill lateral velocity
    Vector2 impulse = Box2DUtils.getLateralVelocity(mBody).scl(-mBody.getMass()).scl(mMaterial.getGrip());
    float maxImpulse = (float) GamePlay.instance.maxLateralImpulse / (mVehicle.isBraking() ? 0.2f : 1);
    if (mMaterial != Material.ICE && mCanDrift && impulse.len() > maxImpulse) {
        // Drift// www. java  2s .c om
        mDrifting = true;
        if (mSkidmarkCount == 0) {
            mSkidmarks.add().init(mBody.getWorldCenter());
        }
        mSkidmarkCount = (mSkidmarkCount + 1) % SKIDMARK_INTERVAL;
        maxImpulse = Math.max(maxImpulse, impulse.len() - DRIFT_IMPULSE_REDUCTION);
        impulse.limit(maxImpulse);
    } else if (mDrifting) {
        mSkidmarks.add().initAsEnd();
        mDrifting = false;
    }
    mBody.applyLinearImpulse(impulse, mBody.getWorldCenter(), true);

    // Kill angular velocity
    mBody.applyAngularImpulse(0.1f * mBody.getInertia() * -mBody.getAngularVelocity(), true);
}

From source file:com.hajnar.GravityShip.GameObjects.GameCamera.java

License:Apache License

public Vector2 followWithZooming(Player player, float delta) {
    Vector2 playerVelocity = player.getBody().getLinearVelocity();
    float playerVelocityLen = playerVelocity.len();
    offset.add(-offset.x / 20f, -offset.y / 20f);
    offset.add(playerVelocity.mul(1.2f));
    if (offset.x > 200)
        offset.x = 200;/* w  ww. j av a  2 s  .co  m*/
    else if (offset.x < -200)
        offset.x = -200;
    if (offset.y > 150)
        offset.y = 150;
    else if (offset.y < -150)
        offset.y = -150;
    cameraTranslation.set(player.getBody().getPosition().mul(Helper.BOX_TO_WORLD).sub(position.x - offset.x,
            position.y - offset.y));
    translate(cameraTranslation);
    if (playerVelocityLen < 20)
        if (playerVelocityBefore < playerVelocityLen && playerVelocityLen > 2)
            changeZoom(0.27f * delta);
        else
            changeZoom(-0.27f * delta);
    playerVelocityBefore = playerVelocityLen;
    return cameraTranslation;

}

From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java

License:Apache License

public static float DistanceBetweenPointAndPoint(Vector2 point1, Vector2 point2) {
    Vector2 v = TextureConverter.vectorSub(point1, point2);
    return v.len();
}

From source file:com.mygdx.game.model.Polygon.java

/**
 * Project the polygon onto an axis/*from w  ww  . java2  s  .c o  m*/
 * @param axis the axis onto which the polygon is to be projected.
 * @return the polygon's projection onto the axis.
 */
public Vector2 projectPolygon(Vector2 axis) {
    // set the minimum projection length to the first vertex
    float min = vertices[0].dot(axis);
    // the max will be the min for now
    float max = min;
    // At first, only the dot products will be compared and stored
    float dot = 0f;
    // Start at the second vertex since the first one was already checked
    for (int i = 1; i < vertices.length; i++) {
        dot = vertices[i].dot(axis);
        // Adjust max/min
        if (dot < min) {
            min = dot;
        }
        if (dot > max) {
            max = dot;
        }
    }

    // Now that the max and min dot product have been calculated, they can be converted into vector projections, thereby providing the bounds of the polygon's projection the given axis
    return new Vector2(min / axis.len(), max / axis.len());
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Autopilot.java

License:Apache License

/** calculates the force to continuously {@link Body#applyForce(Vector2, Vector2, boolean) apply} to reach the given {@code destination} and interpolates it based on distance
 *  @param destination the destination to go to
 *  @param force the force to apply/*from ww  w . jav a 2  s.  c  o m*/
 *  @param distanceScalar the distance at which the given force should be fully applied
 *  @param interpolation the interpolation used to interpolate the given {@code force} based on the {@code distanceScalar}
 *  @return the force to {@link Body#applyForce(Vector2, Vector2, boolean) apply} to navigate to the given {@code destination}
 *  @see #calculateForce(Vector2, Vector2) */
public static Vector2 calculateForce(Vector2 destination, Vector2 force, float distanceScalar,
        Interpolation interpolation) {
    return calculateForce(destination, force).scl(interpolation.apply(destination.len() / distanceScalar));
}

From source file:com.tnf.ptm.common.CommonDrawer.java

License:Apache License

public void drawLine(TextureRegion tex, Vector2 p1, Vector2 p2, Color col, float width, boolean precise) {
    Vector2 v = PtmMath.getVec(p2);
    v.sub(p1);/*from  w  w  w .  ja  v a  2  s.  co m*/
    drawLine(tex, p1.x, p1.y, PtmMath.angle(v, precise), v.len(), col, width);
    PtmMath.free(v);
}

From source file:com.tnf.ptm.common.PtmMath.java

License:Apache License

/**
 * @return a length of a projection of a vector onto a line defined by angle
 *///w  ww.j a v a 2s . co m
public static float project(Vector2 v, float angle) {
    float angleDiff = angle - PtmMath.angle(v);
    return v.len() * cos(angleDiff);
}

From source file:com.tnf.ptm.common.PtmMath.java

License:Apache License

public static boolean canAccelerate(float accAngle, Vector2 spd) {
    return spd.len() < Const.MAX_MOVE_SPD || angleDiff(angle(spd), accAngle) > 90;
}