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(float x, float y) 

Source Link

Document

Constructs a vector with the given components

Usage

From source file:at.juggle.games.counting.CountingGameModel.java

License:Apache License

public InputResult handleTouch(Vector3 p) {
    Vector2 t = new Vector2(p.x, p.y);
    layout.setText(buttonFont, Integer.toString(numberOfSprites));

    if (goodBallons.length < 1)
        return InputResult.Change;
    if (t.y < posButtonsY + layout.height && !answerIsGiven) { // it's a button press
        // check for the answers:
        for (int i = 0; i < answers.length; i++) {
            String answer = Integer.toString(answers[i]);
            layout.setText(buttonFont, answer);
            float posX = CountingGame.GAME_WIDTH / 4 * (i + 1) - layout.width / 2;
            if (Math.abs(posX - t.x) < layout.width) { // got it!
                if (answers[i] == numberOfSprites)
                    answerIsGiven = true;
            }//from w w  w . j a  v a  2  s. c  o  m
        }
        return answerIsGiven ? InputResult.Button : InputResult.Nothing;
    } else if (answerIsGiven) { // check for the balloons ...
        int toRemove = -1;
        float dist = Float.MAX_VALUE;
        for (int i = 0; i < goodBallons.length; i++) {
            Sprite sp = goodBallons[i];
            Vector2 s = new Vector2(sp.getX() + sp.getWidth() / 2, sp.getY() + sp.getHeight() / 2);

            // take the nearest one to remove ...
            if (t.dst(s) < Math.min(goodBallons[i].getWidth(), dist)) { // it's a collision
                toRemove = i;
                dist = t.dst(s);
            }
        }
        if (toRemove > -1) {
            Balloon[] temp = new Balloon[goodBallons.length - 1];
            int count = 0;
            for (int i = 0; i < goodBallons.length; i++) {
                if (i != toRemove)
                    temp[count++] = goodBallons[i];
            }
            goodBallons = temp;
            dirty = true;
            return InputResult.Pop;
        }
    } else {
        return InputResult.Nothing;
    }
    return InputResult.Nothing;
}

From source file:at.juggle.games.counting.gameobjects.Balloon.java

License:Apache License

public Vector2 dist(Balloon b) {
    float x = getX() - b.getX();
    float y = getY() - b.getY();
    return new Vector2(x, y);
}

From source file:at.juggle.games.counting.SortingGameModel.java

License:Apache License

public InputResult handleTouch(Vector3 p) {
    Vector2 t = new Vector2(p.x, p.y);
    layout.setText(buttonFont, Integer.toString(numberOfSprites));

    if (goodBallons.length < 1)
        return InputResult.Change;
    if (true) { // check for the balloons ...
        int toRemove = -1;
        float dist = Float.MAX_VALUE;
        for (int i = 0; i < goodBallons.length; i++) {
            Sprite sp = goodBallons[i];//from www . ja v  a  2s.  c o m
            Vector2 s = new Vector2(sp.getX() + sp.getWidth() / 2, sp.getY() + sp.getHeight() / 2);

            // take the nearest one to remove ...
            if (t.dst(s) < Math.min(goodBallons[i].getWidth(), dist)) { // it's a collision
                if (currentAnswer == goodBallons[i].getNumber()) {
                    toRemove = i;
                    dist = t.dst(s);
                    currentAnswer++;
                }
            }
        }
        if (toRemove > -1) {
            Balloon[] temp = new Balloon[goodBallons.length - 1];
            int count = 0;
            for (int i = 0; i < goodBallons.length; i++) {
                if (i != toRemove)
                    temp[count++] = goodBallons[i];
            }
            goodBallons = temp;
            dirty = true;
            return InputResult.Pop;
        }
    } else {
        return InputResult.Nothing;
    }
    return InputResult.Nothing;
}

From source file:at.therefactory.jewelthief.actors.Enemy.java

License:Open Source License

protected Enemy(String spriteId, float speed) {
    super(spriteId);
    this.speed = Utils.randomWithin(speed - .3f, speed + 1);
    this.movementInverter = new Vector2(Utils.randomSignum(), Utils.randomSignum());
}

From source file:aurelienribon.texturepackergui.Canvas.java

License:Apache License

public Vector2 screenToWorld(int x, int y) {
    Vector3 v3 = new Vector3(x, y, 0);
    camera.unproject(v3);/*from   w ww  .  jav a  2  s . co  m*/
    return new Vector2(v3.x, v3.y);
}

From source file:be.ac.ucl.lfsab1509.bouboule.game.ai.MapNode.java

License:Open Source License

public MapNode(final float px, final float py, final float weight) {

    this.vector = new Vector2(px, py);
    this.weight = weight;
}

From source file:be.ac.ucl.lfsab1509.bouboule.game.body.Arena.java

License:Open Source License

/**
 * Constructor for a Arena object.//  w ww . j  a  va2s. c  o  m
 * @param radius       : radius of Arena for a shape like body
 * @param Bodytype       : Dynamic or Static 
 * @param density       : Mass in [kg] of Arena 
 * @param elasticity   : define the elastical property of Arena [0..1]f
 * @param px/py         : initial posistion
 * @param angle         : initil rotation
 * @param texRegionPath : Path to the image file
 * @param jsonFile      : Path to the jsonFile if needed ( "" else)
 * @param jsonName      : jsonName of the object ( must match the json file attribute )
 *
 * public Arena(float radius, BodyType bodyType,float density,
 *    float elasticity,float px,float py, float angle,String texRegionPath,
 *  String jsonFile, String jsonName)
 */
public Arena(final float radius, final float px, final float py, final float angle, final String texRegionPath,
        final String jsonFile, final String jsonName) {

    super();

    Vector2 pos = new Vector2(px, py);

    this.texture = new TextureRegion(new Texture(texRegionPath));

    this.sprite = new Sprite(texture);

    MakeBody(0, 0, radius, BodyType.StaticBody, 0, 0, true, pos, angle, jsonFile, jsonName,
            GraphicManager.convertToGame(texture.getRegionWidth()));

    this.entity = new Entity(Entity.SCENERY);
    body.setUserData(this.entity);
}

From source file:be.ac.ucl.lfsab1509.bouboule.game.body.Bonus.java

License:Open Source License

/**
 * Constructor for a Bonus object /*  w  w w. j av a2 s .  com*/
 * @param px/py         : initial position
 * @param angle         : initial rotation
 * @param texRegionPath : Path to the image file
 * @param jsonFile      : Path to the jsonFile if needed ( "" else)
 * @param jsonName      : jsonName of the object ( must match the json file attribute )
 *
 * public Bonus( final float px, final float py, 
 *      final float angle, final String texRegionPath, 
 *      final String jsonFile, final String jsonName, final short bonusType)
 */
public Bonus(final float angle, final String texRegionPath, final String jsonFile, final String jsonName,
        final Entity.BonusType bonusType) {

    super();

    int size = GlobalSettings.ARENAWAYPOINTALLOW.size();
    MapNode node = GlobalSettings.ARENAWAYPOINTALLOW.get(random.nextInt(size));

    Vector2 pos = new Vector2(node.xToPixel() - 32, node.yToPixel() - 32);
    Vector2 radius = new Vector2(node.weightToPixel() * random.nextFloat(), 0);
    radius.rotate(random.nextInt(359));
    pos.add(radius);

    this.texture = new TextureRegion(new Texture(texRegionPath));

    this.sprite = new Sprite(texture);

    MakeBody(0, 0, 0, BodyType.StaticBody, 0, 0, true, pos, angle, jsonFile, jsonName,
            GraphicManager.convertToGame(texture.getRegionWidth()));

    //Ensure that the object don't rotate.
    body.setFixedRotation(true);

    //Create the userData of type Bonus and bonusType
    this.entity = new Entity(Entity.BONUS, true, bonusType);

    body.setUserData(this.entity);

    //Ensure that the body image position is set on the origin defined by 
    //the jsonFile
    if (origin != null) {
        pos = positionVector.cpy();
        pos = pos.sub(origin);
        sprite.setPosition(pos.x, pos.y);
        sprite.setOrigin(origin.x, origin.y);
        sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
    }

    // removed the bonus after a delay
    Timer.Task task = new Timer.Task() {
        @Override
        public void run() {
            entity.setAlive(false);
        }
    };
    Timer timer = new Timer();
    timer.scheduleTask(task, 10f);
}

From source file:be.ac.ucl.lfsab1509.bouboule.game.body.Bouboule.java

License:Open Source License

/**
 * Constructor for a Bouboule object /* www . ja va  2  s  .com*/
 * @param radius       : radius of Bouboule for a shape like body
 * @param Bodytype       : Dynamic or Static 
 * @param density       : Mass in [kg] of Bouboule 
 * @param elasticity    : define the elastical property of Bouboul [0..1]f
 * @param px/py         : initial posistion
 * @param angle         : initil rotation
 * @param texRegionPath : Path to the image file
 * @param jsonFile      : Path to the jsonFile if needed ( "" else)
 * @param jsonName      : jsonName of the object ( must match the json file attribute )
 *
 * public Bouboule(float radius, BodyType bodyType,float density,
 *    float elasticity,float px,float py, float angle,String texRegionPath,
 *  String jsonFile, String jsonName)
 */
public Bouboule(final float radius, final BodyType bodyType, final float density, final float elasticity,
        final float px, final float py, final float angle, final String texRegionPath, final String jsonFile,
        final String jsonName, final short type, final int AILevel) {

    super();

    this.AILevel = AILevel;
    Vector2 pos = new Vector2(px, py);

    this.texture = new TextureRegion(new Texture(texRegionPath));
    this.sprite = new Sprite(texture);

    MakeBody(0, 0, radius, bodyType, density, elasticity, false, pos, angle, jsonFile, jsonName,
            GraphicManager.convertToGame(texture.getRegionWidth()));

    // added sprite to the fixture (to modify it somewhere else
    body.getFixtureList().get(0).setUserData(sprite);

    //Ensure that the object don't rotate.
    body.setFixedRotation(GlobalSettings.FIXED_ROTATION);

    this.entity = new Entity(type, true);

    body.setUserData(this.entity);
}

From source file:be.ac.ucl.lfsab1509.bouboule.game.body.Obstacle.java

License:Open Source License

/**
 * Constructor for a Obstacle object ./*  w w  w  . j a  v  a  2 s . c  o  m*/
 * @param Bodytype    : Dynamic or Static 
 * @param density    : Mass in [kg] of the obstacle 
 * @param elasticity : define the elastical property of Bouboul [0..1]f
 * @param px/py      : initial position
 * @param angle      : initial rotation
 * @param texRegionPath : Path to the image file
 * @param jsonFile   : Path to the jsonFile if needed ( "" else)
 * @param jsonName   : jsonName of the object ( must match the json file attribute )
 *
 * public Obstacle( final BodyType bodyType, final float density,
 *    final float elasticity, final float px, final float py,
 *  final float angle, final String texRegionPath, 
 *  final String jsonFile, final String jsonName)
 */
public Obstacle(final BodyType bodyType, final float density, final float elasticity, final float px,
        final float py, final float angle, final String texRegionPath, final String jsonFile,
        final String jsonName, final float initAccX, final float initAccY) {

    super();

    this.initialPos = new Vector2(px, py);

    this.texture = new TextureRegion(new Texture(texRegionPath));

    this.sprite = new Sprite(texture);

    MakeBody(0, 0, 0, bodyType, density, elasticity, false, initialPos, angle, jsonFile, jsonName,
            GraphicManager.convertToGame(texture.getRegionWidth()));

    //Ensure that the object don't rotate.
    //body.setFixedRotation(true);

    //Add Initail Velocity

    body.applyForceToCenter(new Vector2(initAccX, initAccY), true);

    this.entity = new Entity(Entity.OBSTACLE, true);
    body.setUserData(this.entity);
}