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

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

Introduction

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

Prototype

public Vector2(Vector2 v) 

Source Link

Document

Constructs a vector from the given vector

Usage

From source file:com.dongbat.invasion.util.BulletUtil.java

private static Vector2 calculateImpulse(Vector2 fireVector, int power, float angle) {
    Vector2 impulse = new Vector2(fireVector);
    // TODO: -40 =.=
    return impulse.scl(power).rotate(angle).scl(-40);
}

From source file:com.dongbat.invasion.util.EffectUtil.java

/**
 * Create explosions//w  w  w .j a va2s  .co m
 *
 * @param physPos physical position, center point for explosions
 * @param physRadius the radius where explosion can spawn
 * @param duration the duration to randomize the spawning time of explosion
 */
public static void explode(Vector2 physPos, float physRadius, int duration) {
    Vector2 position = RenderCameraUtil.physicsToRenderCoords(physPos);
    float radius = physRadius * PhysicsCameraUtil.getRatio();

    int count;
    if (radius <= SINGLE_EXPLOSION_RADIUS) {
        count = 1;
    } else if (radius > SINGLE_EXPLOSION_RADIUS && radius <= SINGLE_EXPLOSION_RADIUS * 2) {
        count = 4;
    } else if (radius > SINGLE_EXPLOSION_RADIUS * 2 && radius <= SINGLE_EXPLOSION_RADIUS * 4) {
        count = 8;
    } else {
        count = 12;
    }

    if (count == 1) {
        createSingleExplosion(position, 0);
        return;
    }

    for (int i = 0; i < count; i++) {
        Vector2 pos = new Vector2(position);
        pos.x += MathUtils.random(-radius, radius);
        pos.y += MathUtils.random(-radius, radius);
        int delay = MathUtils.random(duration);

        createSingleExplosion(pos, delay);
    }
}

From source file:com.dongbat.invasion.util.PhysicsUtil.java

public static Entity findNearestEnemy(Vector2 location) {
    ImmutableBag<Entity> allEnemies = ECSUtil.getWorld().getManager(GroupManager.class)
            .getEntities(Constants.ENEMY.GROUP_NAME);
    Entity nearest = null;//from  w  w  w . j av a  2s . c o m
    float closestDistanceSq = Float.MAX_VALUE;

    for (Entity enemy : allEnemies) {
        Vector2 position = getBody(enemy).getPosition();
        float distanceSq = new Vector2(position).sub(location).len2();
        if (distanceSq < closestDistanceSq) {
            nearest = enemy;
            closestDistanceSq = distanceSq;
        }
    }

    return nearest;
}

From source file:com.dongbat.invasion.util.PhysicsUtil.java

public static Array<Entity> findEnemiesInRadius(final Vector2 location, final float radius) {
    final Array<Entity> enemies = new Array<Entity>();

    QueryCallback callback = new QueryCallback() {

        @Override/*from w  w w  .j a  v a  2  s  .c o m*/
        public boolean reportFixture(Fixture fixture) {
            Body body = fixture.getBody();
            Entity entity = (Entity) body.getUserData();
            if (EntityUtil.isEnemy(entity)) {
                float distanceSq = new Vector2(body.getPosition()).sub(location).len2();
                if (distanceSq <= radius * radius) {
                    enemies.add(entity);
                }
            }
            return true;
        }
    };
    Vector2 lowerLeft = new Vector2(location).sub(radius, radius);
    Vector2 upperRight = new Vector2(location).add(radius, radius);
    getWorld().QueryAABB(callback, lowerLeft.x, lowerLeft.y, upperRight.x, upperRight.y);

    return enemies;
}

From source file:com.dongbat.invasion.util.PlayerInputUtil.java

public static void init() {
    Gdx.input.setInputProcessor(new InputAdapter() {

        @Override//  w  ww.  ja v a2 s  .c o  m
        public boolean touchDragged(int screenX, int screenY, int pointer) {
            aim(screenX, screenY);
            return true;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            fireVector = new Vector2(aimVector);
            aimVector = null;
            return true;
        }

        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            aim(screenX, screenY);
            return true;
        }

        private void aim(int x, int y) {
            Vector3 v = new Vector3(x, y, 0);
            v = camera.unproject(v);
            aimVector = new Vector2(v.x, v.y);
            if (aimVector.len2() >= 400) {
                aimVector.nor().scl(20);
            }
        }
    });
}

From source file:com.flaiker.reaktio.entities.DragSquareEntity.java

License:Open Source License

private float getTravelledDistance() {
    Vector2 tmpVec = new Vector2(startPos);
    return Math.abs(tmpVec.sub(getX(), getY()).len());
}

From source file:com.forerunnergames.peril.client.settings.PlayMapSettings.java

License:Open Source License

public static Vector2 countryArmyCircleSizeActualPlayMapSpace(final Vector2 playMapReferenceSize) {
    Arguments.checkIsNotNull(playMapReferenceSize, "playMapReferenceSize");

    return new Vector2(COUNTRY_ARMY_CIRCLE_SIZE_REFERENCE_PLAY_MAP_SPACE)
            .scl(referenceToActualPlayMapScaling(playMapReferenceSize));
}

From source file:com.forerunnergames.peril.client.ui.screens.game.play.modes.classic.playmap.actors.DefaultCountry.java

License:Open Source License

public DefaultCountry(final CountryImages<CountryPrimaryImageState, CountryPrimaryImage> primaryImages,
        final CountryImages<CountrySecondaryImageState, CountrySecondaryImage> secondaryImages,
        final CountryImageData imageData, final CountryArmyText armyText, final Vector2 playMapReferenceSize) {
    Arguments.checkIsNotNull(primaryImages, "primaryImages");
    Arguments.checkIsNotNull(secondaryImages, "secondaryImages");
    Arguments.checkIsNotNull(imageData, "imageData");
    Arguments.checkIsNotNull(armyText, "armyText");
    Arguments.checkIsNotNull(playMapReferenceSize, "playMapReferenceSize");

    this.primaryImages = primaryImages;
    this.secondaryImages = secondaryImages;
    this.imageData = imageData;
    this.armyText = armyText;

    final Vector2 referenceToActualPlayMapSpaceScaling = PlayMapSettings
            .referenceToActualPlayMapScaling(playMapReferenceSize);

    final Vector2 tempPosition = new Vector2(imageData.getReferenceDestination());
    tempPosition.y = playMapReferenceSize.y - tempPosition.y;
    tempPosition.scl(referenceToActualPlayMapSpaceScaling);

    group.setName(imageData.getCountryName());
    group.setTransform(false);//w  ww  .  j a  va 2 s.  co m

    for (final CountryPrimaryImage primaryImage : primaryImages.getAll()) {
        primaryImage.setVisible(false);
        primaryImage.setPosition(tempPosition);
        primaryImage.setScale(referenceToActualPlayMapSpaceScaling);
        group.addActor(primaryImage.asActor());
    }

    for (final CountrySecondaryImage countrySecondaryImage : secondaryImages.getAll()) {
        countrySecondaryImage.setVisible(false);
        countrySecondaryImage.setPosition(tempPosition);
        countrySecondaryImage.setScale(referenceToActualPlayMapSpaceScaling);
        group.addActor(countrySecondaryImage.asActor());
    }

    changePrimaryStateTo(primaryImageState);
    changeSecondaryStateTo(secondaryImageState);
}

From source file:com.github.unluckyninja.defenseofhuman.model.entity.Player.java

License:Open Source License

private void createBody(GameWorld gameWorld, Vector2 position) {
    Vector2 temp = new Vector2(position);
    if (body != null) {
        this.world.destroyBody(body);
    }/*from ww  w.  ja va  2s.  c  o  m*/
    this.world = gameWorld.getWorld();

    BodyDef def = new BodyDef();
    def.type = BodyDef.BodyType.DynamicBody;
    def.fixedRotation = true;
    def.bullet = true;
    def.position.set(temp);
    body = world.createBody(def);

    body.setUserData(this);

    CircleShape cir = new CircleShape();
    cir.setRadius(0.4f);
    cir.setPosition(temp.add(0, 0.4f));
    circle = body.createFixture(cir, 0);
    circle.setRestitution(0f);
    cir.dispose();

    PolygonShape poly = new PolygonShape();
    poly.setAsBox(0.4f, 0.8f, temp.add(0, 0.8f), 0);
    box = body.createFixture(poly, 1);

    poly.setAsBox(0.2f, 0.2f, temp.add(0, -0.4f), 0);
    localLaunchPosition = temp.cpy();

    FixtureDef fixDef = new FixtureDef();
    fixDef.isSensor = true;
    fixDef.density = 0.01f;
    fixDef.shape = poly;

    shootSensor = body.createFixture(fixDef);

    poly.dispose();
}

From source file:com.github.unluckyninja.defenseofhuman.model.entity.Player.java

License:Open Source License

private void updateAction(float delta) {

    Vector2 oldvel = body.getLinearVelocity();
    Vector2 oldpos = body.getPosition();

    // ??//from   w  w  w . j a  v  a2s .co m
    // 
    if (state == PlayerState.INAIR) {
        box.setFriction(0f);
        circle.setFriction(0f);
    } else if (run) {
        box.setFriction(1f);
        circle.setFriction(1f);
    } else {
        box.setFriction(100f);
        circle.setFriction(100f);
    }
    // 
    if (run && Math.abs(oldvel.x) <= MAX_VELOCITY) {
        if (runleft) {
            body.applyLinearImpulse(-1, 0, oldpos.x, oldpos.y, true);
        } else {
            body.applyLinearImpulse(1, 0, oldpos.x, oldpos.y, true);
        }
    } else if (!run && state != PlayerState.INAIR) {
        body.setLinearVelocity(oldvel.x * 0.9f, oldvel.y);
    }
    // ??
    if (run && Math.abs(body.getLinearVelocity().x) >= MAX_VELOCITY) {
        body.setLinearVelocity(Math.signum(oldvel.x) * MAX_VELOCITY, oldvel.y);
    }
    // 
    if (jump) {
        body.setLinearVelocity(oldvel.x, 0);
        body.applyLinearImpulse(0, 8, oldpos.x, oldpos.y, true);
    }
    // 
    if (shoot && shooter.canShoot) {
        shooter.shoot(new Vector2(gameWorld.cursorPosition).sub(body.getWorldPoint(localLaunchPosition)));
    }
}