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() 

Source Link

Document

Constructs a new vector at (0,0)

Usage

From source file:com.dongbat.game.util.factory.UnitFactory.java

/**
 * Create unit and add to artemis world/*from  w  w  w .j  a v  a 2s  .c o m*/
 *
 * @param world artemis world
 * @param unitType type of unit in String
 * @param position spawn position
 * @param args optional arguments
 * @return Unit that was just created
 */
public static Entity createUnit(World world, String unitType, Vector2 position, Object... args) {
    Entity e = world.createEntity(UUID.randomUUID());

    UnitInfo unitInfo = get(unitType);
    setUnitData(world, e, args);

    CollisionComponent collision = new CollisionComponent();

    Stats stats = new Stats();
    stats.setBaseRateSpeed(unitInfo.getBaseSpeedRate());

    Physics physics = new Physics();
    e.edit().add(physics);
    physics.setBody(
            PhysicsUtil.createBody(PhysicsUtil.getPhysicsWorld(world), position, unitInfo.getRadius(), e));
    physics.getBody().setUserData(UuidUtil.getUuid(e));
    UnitMovement movement = new UnitMovement();
    movement.setDirectionVelocity(new Vector2());

    AiControl aiControl = new AiControl(unitInfo.getDefinitionPath());
    Display display = new Display();
    e.edit().add(new BuffComponent()).add(aiControl).add(new Player()).add(new Detection()).add(stats)
            .add(movement).add(display).add(collision);

    display.setPosition(PhysicsUtil.getPosition(world, e));
    display.setRadius(PhysicsUtil.getRadius(world, e));
    display.setRotation(EntityUtil.getComponent(world, e, UnitMovement.class).getDirectionVelocity().angle());
    display.setDefaultAnimation(AnimationUtil.getUnitAnimation());
    BuffUtil.addBuff(world, e, e, "FeedSmaller", -1, 1);

    return e;
}

From source file:com.dongbat.game.util.PlaneMathCalculator.java

public static Vector2 giaiPtBacNhat2An(float a1, float b1, float c1, float a2, float b2, float c2) {
    float d = a1 * b2 - b1 * a2;
    float dX = c1 * b2 - b1 * c2;
    float dY = a1 * c2 - c1 * a2;
    if (d == 0) {
        //vo nghiem hoac vo so nghiem, ma ke no
        return null;
    }//from ww  w.  ja  v a2  s.  co  m
    Vector2 newVector = new Vector2();
    if (d != 0) {
        newVector.x = dX / d;
        newVector.y = dY / d;
    }
    return newVector;
}

From source file:com.dongbat.invasion.enemy.tasks.BossFindTargetTask.java

@Override
public void run(Entity enemy) {
    Enemy enemyComponent = EntityUtil.getComponent(enemy, Enemy.class);
    Vector2 position = PhysicsUtil.getPosition(enemy);

    Vector2 target = new Vector2();
    target.y = position.y;// w  w w . ja  v  a  2 s . co  m

    if (position.x >= Constants.ENEMY.BOSS_RIGHT_INTENT_X) {
        enemyComponent.setTurnedAround(true);
    } else if (position.x <= Constants.ENEMY.BOSS_LEFT_INTENT_X) {
        enemyComponent.setTurnedAround(false);
    }

    target.x = enemyComponent.hasTurnedAround() ? Constants.ENEMY.BOSS_LEFT_INTENT_X
            : Constants.ENEMY.BOSS_RIGHT_INTENT_X;

    MovementUtil.setTarget(enemy, target);
    success();
}

From source file:com.dongbat.invasion.system.EnemyMovementSystem.java

@Override
protected void process(Entity entity) {
    Delay delay = delayMapper.get(entity);
    if (delay != null && delay.getDuration() > 0) {
        long timePassed = TimeUtil.getGameTimeSince(delay.getStartTime());
        if (timePassed <= delay.getDuration()) {
            return;
        }/*from w ww . ja  v  a 2s .c  om*/
    }

    //    Vector2 prevVelocity = PhysicsUtil.getVelocity(entity).cpy();
    Vector2 position = PhysicsUtil.getPosition(entity);
    EnemyMovement movement = EntityUtil.getComponent(entity, EnemyMovement.class);
    PhysicsUtil.setVelocity(entity, new Vector2());
    Vector2 intent = movement.getIntent().cpy();
    Vector2 travel;
    MovementUtil.moveEnemyTo(entity, intent);
    // TODO check free enemy
    intent.y = position.y;
    // TODO free movement unit use moveTo
    Vector2 distance = intent.cpy().sub(position);
    travel = PhysicsUtil.getVelocity(entity).cpy().scl(world.delta);
    if (travel.dot(distance) > 0 && travel.len2() - distance.len2() > 0) {
        PhysicsUtil.setPosition(entity, intent);
        PhysicsUtil.setVelocity(entity, new Vector2());
    }

    if (TimeUtil.getGameTimeSince(movement.getStartDrag()) >= movement.getDragDuration()) {
        movement.setDraggedTo(null);
    }

    if (movement.getDraggedTo() == null) {
        return;
    }

    float max = movement.getDraggedTo().cpy().sub(movement.getDraggedFrom()).len2();
    float actual = position.cpy().sub(movement.getDraggedFrom()).len2();
    if (actual >= max) {
        return;
    }

    Vector2 drag = movement.getDraggedTo().cpy().sub(position);
    float dragLen2 = drag.len2();
    if (dragLen2 > movement.getDragForce() * movement.getDragForce()) {
        drag.nor().scl(movement.getDragForce());
    }
    PhysicsUtil.applyImpulseOnEnemy(entity, drag);

    // TODO fix this similar to above
    travel = PhysicsUtil.getVelocity(entity).cpy().scl(world.delta);
    if (travel.sub(drag).len2() <= Constants.GAME.EPSILON_SQUARED) {
        PhysicsUtil.setPosition(entity, movement.getDraggedTo());
        PhysicsUtil.applyImpulseOnEnemy(entity, drag.scl(-1));
    }
}

From source file:com.ethereal.rm.game.objects.AbstractGameObject.java

public AbstractGameObject() {
    position = new Vector2();
    dimension = new Vector2(1, 1);
    origin = new Vector2();
    scale = new Vector2(1, 1);
    rotation = 0;/*from w  w  w.j  av  a2s  .  c  om*/

}

From source file:com.evoluzion.Organismo.java

License:Open Source License

public Organismo(Genoma adn, Vector2 posicion, StringBuffer nombre, Mundo m) {

    this.m = m;//from  w w w . java 2 s .com

    fechaNacimiento = "" + m.minutos2 + ":" + m.segundos;

    aObjetos = new Array<Vector2>();
    aPredadores = new Array<Vector2>();

    this.adn = adn;
    this.posicion = posicion;
    this.nombre = nombre;

    direccion = new Vector2();
    borde = new Rectangle();

    translate();

    segundos = 0;
    setTime();
    setEdad();
    setDelta();

    identificador = new String(Identificar(m));

    if (capacidad <= 0) {
        capacidad = 1;
        morir();
    }

    //System.out.println("identificador"+identificador);

}

From source file:com.evoluzion.Qenergia.java

License:Open Source License

public Qenergia(Vector2 posicion, boolean mover, Mundo m) {

    this.m = m;/*from   w w  w. j a va2  s  . c om*/
    masa = m.Qbiomasa;
    this.posicion = posicion;
    direccion = new Vector2();

    ancho = ancho / m.zoom;
    alto = alto / m.zoom;

    speed = 30;
    borde = new Rectangle();

    borde.height = alto;
    borde.width = ancho;

    borde.x = posicion.x;
    borde.y = posicion.y;

    if (mover == true) {
        direccion.y = -1;
    }
    direccion.x = 0;

    imagen = new Sprite(m.textuRA_ENER.getRegions().get(1));
    imagen.setPosition(this.posicion.x, this.posicion.y);
    imagen.setSize(ancho, alto);

}

From source file:com.evoluzion.Senergia.java

License:Open Source License

public Senergia(Vector2 posicion, Mundo m) {

    this.m = m;// w  ww .  j a va2  s.  com
    energia = m.Senergia;
    this.posicion = posicion;
    direccion = new Vector2();

    ancho = ancho / m.zoom;
    alto = alto / m.zoom;

    speed = 30;
    borde = new Rectangle();

    borde.height = alto;
    borde.width = ancho;

    borde.x = posicion.x;
    borde.y = posicion.y;

    direccion.y = -1;
    direccion.x = 0;

    imagen = new Sprite(m.textuRA_ENER.getRegions().get(0));
    imagen.setPosition(this.posicion.x, this.posicion.y);
    imagen.setSize(ancho, alto);

}

From source file:com.flatfisk.gnomp.math.GeometryUtils.java

License:Apache License

/**
 * @param floats the float[] to convert to a Vector2[]
 * @return the Vector2[] converted from the given float[]
 *///from  ww  w.  j a  va 2 s . c om
public static Vector2[] toVector2Array(float[] floats, Vector2[] output) {
    if (floats.length % 2 != 0)
        throw new IllegalArgumentException(
                "the float array's length is not dividable by two, so it won't make up a Vector2 array: "
                        + floats.length);

    if (output == null || output.length != floats.length / 2) {
        output = new Vector2[floats.length / 2];
        for (int i = 0; i < output.length; i++)
            output[i] = new Vector2();
    }

    for (int i = 0, fi = -1; i < output.length; i++)
        output[i].set(floats[++fi], floats[++fi]);

    return output;
}

From source file:com.gamejolt.mikykr5.ceidecpong.ecs.entities.PongEntityInitializer.java

License:Open Source License

@Override
public void setLoadableAssets(PooledEngine engine) throws IllegalStateException {
    if (!entitiesCreated)
        throw new IllegalStateException("Entities have not been created before setting assets.");

    // Some variables used to initialize the ball.
    Vector2 randomVector = new Vector2().set(Vector2.X).setAngle(MathUtils.random(-60, 60));
    int randomSign = MathUtils.random(-1, 1) >= 0 ? 1 : -1;

    // Fetch the assets.
    TextureAtlas atlas = loader.getAsset("data/gfx/textures/pong_atlas.atlas", TextureAtlas.class);
    Texture bckg = loader.getAsset("data/gfx/textures/bckg.png", Texture.class);

    // Add the sound effects to the entities.
    Mappers.soundMapper.get(victorySound).path = "data/sfx/oh_yeah_wav_cut.ogg";
    Mappers.soundMapper.get(defeatSound).path = "data/sfx/atari_boom.ogg";

    // Set up the background.
    Mappers.spriteMapper.get(background).sprite = new Sprite(bckg);
    Mappers.positionMapper.get(background).setXY(-(ProjectConstants.FB_WIDTH / 2.0f),
            -(ProjectConstants.FB_HEIGHT / 2.0f));

    // Set up the ball.
    Mappers.spriteMapper.get(ball).sprite = atlas.createSprite("ball");
    Mappers.positionMapper.get(ball).setXY(-(Mappers.spriteMapper.get(ball).sprite.getWidth() / 2),
            -(Mappers.spriteMapper.get(ball).sprite.getHeight() / 2));
    Mappers.velocityMapper.get(ball).setXY(randomVector.x * 475.0f * randomSign,
            randomVector.y * 475.0f * randomSign);
    Mappers.bboxMapper.get(ball).bbox.set(Mappers.spriteMapper.get(ball).sprite.getBoundingRectangle());
    Mappers.soundMapper.get(ball).path = "data/sfx/BounceYoFrankie.ogg";

    // Set up the human player.
    Mappers.spriteMapper.get(paddleUser).sprite = atlas.createSprite("glasspaddle2");
    Mappers.positionMapper.get(paddleUser).setXY(-(ProjectConstants.FB_WIDTH / 2) + 100,
            -(Mappers.spriteMapper.get(paddleUser).sprite.getHeight() / 2));
    Mappers.bboxMapper.get(paddleUser).bbox
            .set(Mappers.spriteMapper.get(paddleUser).sprite.getBoundingRectangle());
    Mappers.playerMapper.get(paddleUser).id = PlayerComponent.HUMAN_PLAYER;

    // Set up the computer player.
    Mappers.spriteMapper.get(paddleComp).sprite = atlas.createSprite("paddle");
    Mappers.positionMapper.get(paddleComp)
            .setXY(((ProjectConstants.FB_WIDTH / 2) - 1) - 100
                    - Mappers.spriteMapper.get(paddleComp).sprite.getWidth(),
                    -(Mappers.spriteMapper.get(paddleComp).sprite.getHeight() / 2));
    Mappers.bboxMapper.get(paddleComp).bbox
            .set(Mappers.spriteMapper.get(paddleComp).sprite.getBoundingRectangle());
    Mappers.playerMapper.get(paddleComp).id = PlayerComponent.COMPUTER_PLAYER;

    // Release the assets loader instance and mark the flag.
    AsyncAssetLoader.freeInstance();//from w w  w . ja  va  2  s.  c  o m
    assetsLoaded = true;
}