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:com.me.myverilogTown.LevelScreen.java

License:Open Source License

private void zoom_limit_to_border() {
    /* keeps any move on a zoomed region to the limit of the map */
    float camX;//from   w w  w . j a v  a 2s  .  com
    float camY;

    camX = camera.position.x;
    camY = camera.position.y;

    Vector2 camMin = new Vector2(camera.viewportWidth, camera.viewportHeight);
    camMin.scl(camera.zoom / 2); // bring to center and scale by the zoom
    // level
    Vector2 camMax = new Vector2(LEVEL_WIDTH, LEVEL_HEIGHT + SCORE_BAR_HEIGHT);
    camMax.sub(camMin); // bring to center

    /* keep camera within borders */
    camX = Math.min(camMax.x, Math.max(camX, camMin.x));
    camY = Math.min(camMax.y - (1 - camera.zoom) * 100, Math.max(camY, camMin.y));

    /* reset position */
    camera.position.set(camX, camY, camera.position.z);
}

From source file:com.me.startassault.world.Level.java

private void loadDemoLevel() {

    width = 10;/*w w  w  .  j av a2s  .c  o m*/
    height = 7;
    blocks = new Block[width][height];
    for (int col = 0; col < width; col++) {
        for (int row = 0; row < height; row++) {
            blocks[col][row] = null;
        }
    }

    for (int col = 0; col < 10; col++) {
        blocks[col][0] = new Block(new Vector2(col, 0));
        blocks[col][6] = new Block(new Vector2(col, 6));
        if (col > 2) {
            blocks[col][1] = new Block(new Vector2(col, 1));
        }
    }
    for (int i = 0; i < 6; i++) {
        blocks[0][i] = new Block(new Vector2(0, i));

    }
    blocks[9][2] = new Block(new Vector2(9, 2));
    blocks[9][3] = new Block(new Vector2(9, 3));
    blocks[9][4] = new Block(new Vector2(9, 4));
    blocks[9][5] = new Block(new Vector2(9, 5));

    blocks[6][3] = new Block(new Vector2(6, 3));
    blocks[6][4] = new Block(new Vector2(6, 4));
    blocks[6][5] = new Block(new Vector2(6, 5));
}

From source file:com.mjf.racetrack.util.TiledMapLoader.java

License:Apache License

/**
 * Reads a file describing the collision boundaries that should be set
 * per-tile and adds static bodies to the box2d world.
 *
 * @param collisionsFile/* w  w w  .  j  a v  a  2  s . c  o  m*/
 * @param world
 * @param pixelsPerMeter
 *            the pixels per meter scale used for this world
 */
public void loadCollisions(String collisionsFile, World world, float pixelsPerMeter) {
    /**
     * Detect the tiles and dynamically create a representation of the map
     * layout, for collision detection. Each tile has its own collision
     * rules stored in an associated file.
     *
     * The file contains lines in this format (one line per type of tile):
     * tileNumber XxY,XxY XxY,XxY
     *
     * Ex:
     *
     * 3 0x0,31x0 ... 4 0x0,29x0 29x0,29x31
     *
     * For a 32x32 tileset, the above describes one line segment for tile #3
     * and two for tile #4. Tile #3 has a line segment across the top. Tile
     * #1 has a line segment across most of the top and a line segment from
     * the top to the bottom, 30 pixels in.
     */

    if (map == null) {
        throw new GdxRuntimeException("TiledMap must be loaded before the collisions file can be loaded.");
    }

    mapProperties = map.getProperties();
    FileHandle fh = Gdx.files.internal(collisionsFile);
    String collisionFile = fh.readString();
    String lines[] = collisionFile.split("\\r?\\n");

    HashMap<Integer, ArrayList<LineSegment>> tileCollisionJoints = new HashMap<Integer, ArrayList<LineSegment>>();

    /**
     * Some locations on the map (perhaps most locations) are "undefined",
     * empty space, and will have the tile type 0. This code adds an empty
     * list of line segments for this "default" tile.
     */
    tileCollisionJoints.put(Integer.valueOf(0), new ArrayList<LineSegment>());

    for (int n = 0; n < lines.length; n++) {
        String cols[] = lines[n].split(" ");
        int tileNo = Integer.parseInt(cols[0]);

        ArrayList<LineSegment> tmp = new ArrayList<LineSegment>();

        for (int m = 1; m < cols.length; m++) {
            String coords[] = cols[m].split(",");

            String start[] = coords[0].split("x");
            String end[] = coords[1].split("x");

            tmp.add(new LineSegment(Integer.parseInt(start[0]), Integer.parseInt(start[1]),
                    Integer.parseInt(end[0]), Integer.parseInt(end[1])));
        }

        tileCollisionJoints.put(Integer.valueOf(tileNo), tmp);
    }

    ArrayList<LineSegment> collisionLineSegments = new ArrayList<LineSegment>();

    for (int y = 0; y < getHeight(); y++) {
        for (int x = 0; x < getWidth(); x++) {
            // TODO: fixme
            int tileType = 0;//map.getLayers().get(0).getObjects().get(((getHeight() - 1) - y) + x);

            for (int n = 0; n < tileCollisionJoints.get(Integer.valueOf(tileType)).size(); n++) {
                LineSegment lineSeg = tileCollisionJoints.get(Integer.valueOf(tileType)).get(n);

                addOrExtendCollisionLineSegment(x * getTileWidth() + lineSeg.start().x,
                        y * getTileHeight() - lineSeg.start().y + 32, x * getTileWidth() + lineSeg.end().x,
                        y * getTileHeight() - lineSeg.end().y + 32, collisionLineSegments);
            }
        }
    }

    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.type = BodyDef.BodyType.StaticBody;
    Body groundBody = world.createBody(groundBodyDef);
    for (LineSegment lineSegment : collisionLineSegments) {
        EdgeShape environmentShape = new EdgeShape();

        environmentShape.set(lineSegment.start().scl(1 / pixelsPerMeter),
                lineSegment.end().scl(1 / pixelsPerMeter));
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }

    /**
     * Drawing a boundary around the entire map. We can't use a box because
     * then the world objects would be inside and the physics engine would
     * try to push them out.
     */

    EdgeShape mapBounds = new EdgeShape();
    mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(getWidth() / pixelsPerMeter, 0.0f));
    groundBody.createFixture(mapBounds, 0);

    mapBounds.set(new Vector2(0.0f, getHeight() / pixelsPerMeter),
            new Vector2(getWidth() / pixelsPerMeter, getHeight() / pixelsPerMeter));
    groundBody.createFixture(mapBounds, 0);

    mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(0.0f, getHeight() / pixelsPerMeter));
    groundBody.createFixture(mapBounds, 0);

    mapBounds.set(new Vector2(getWidth() / pixelsPerMeter, 0.0f),
            new Vector2(getWidth() / pixelsPerMeter, getHeight() / pixelsPerMeter));
    groundBody.createFixture(mapBounds, 0);

    mapBounds.dispose();
}

From source file:com.mknsri.drunktoss.DrunkToss.java

License:Open Source License

@Override
public void resize(int width, int height) {
    // calculate new viewport
    float aspectRatio = (float) width / (float) height;
    scale = 1f;/*from  w w w . j  a  va  2  s .  c o  m*/
    Vector2 crop = new Vector2(0f, 0f);

    if (aspectRatio > ASPECT_RATIO) {
        scale = (float) height / (float) VIEWPORT_HEIGHT;
        crop.x = (width - VIEWPORT_WIDTH * scale) / 2f;
    } else if (aspectRatio < ASPECT_RATIO) {
        scale = (float) width / (float) VIEWPORT_WIDTH;
        crop.y = (height - VIEWPORT_HEIGHT * scale) / 2f;
    } else {
        scale = (float) width / (float) VIEWPORT_WIDTH;
    }

    float w = (float) VIEWPORT_WIDTH * scale;
    float h = (float) VIEWPORT_HEIGHT * scale;
    viewport = new Rectangle(crop.x, crop.y, w, h);
    cropX = crop.x;
}

From source file:com.models.Mario.java

public Mario(float x, float y, float width, float height) {
    super(x, y, width, height);
    state = State.STANDING;//from  w  w w .  j  a  v a 2  s.  c  o  m
    velocity = new Vector2(0, 0);
    acceleration = new Vector2(0, 0);
    isFacingLeft = false;
    stateTime = 0;
}

From source file:com.moosader.states.TitleState.java

public void setup() {
    m_nextState = NextState.MENU_SCREEN;
    m_isDone = false;//  w w  w .  j a  va 2s. c  o m
    m_lstSprites = new ArrayList<Sprite>();
    m_batch = new SpriteBatch();

    m_titleCoord1 = new Vector2(0, 360);
    m_titleCoord2 = new Vector2(0, -360 - m_panSpeed);

    m_titleSpr1 = new Sprite(new TextureRegion(GraphicsManager.txMenuTitlescreen1.texture, 0, 0,
            GraphicsManager.txMenuTitlescreen1.frameW(), GraphicsManager.txMenuTitlescreen1.frameH()));
    m_titleSpr1.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    m_titleSpr2 = new Sprite(new TextureRegion(GraphicsManager.txMenuTitlescreen2.texture, 0, 0,
            GraphicsManager.txMenuTitlescreen2.frameW(), GraphicsManager.txMenuTitlescreen2.frameH()));
    m_titleSpr2.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}

From source file:com.mygdx.combat.NormAttackSensor.java

public NormAttackSensor(SteerableEntity parent) {
    super(parent);

    //pshape.setAsBox(parent.getRange(), parent.getRange(), new Vector2(0,parent.getRange()), 0);
    //this.shape = pshape;

    cshape.setRadius(parent.getRange());
    cshape.setPosition(new Vector2(0, 0));
    this.shape = cshape;

}

From source file:com.mygdx.entities.DynamicEntities.enemies.EnemyEntity.java

@Override
public void init(World world) {
    super.init(world);

    seekWanderTarget = new SeekWanderTarget(new Vector2(pos.x, pos.y));
    EnvironmentManager.currentEnv.spawnEntity(seekWanderTarget);

    seekWanderSB = new Seek<Vector2>(this, seekWanderTarget);
}

From source file:com.mygdx.entities.DynamicEntities.enemies.En_Goober.java

public En_Goober(Vector2 pos) {
    super(pos, 30f * RATIO, 30f * RATIO);
    fd.restitution = 0.2f;/*  www  .  j av  a 2  s. c om*/

    float sscale = 0.38f * RATIO;

    moveSprite = new ImageSprite("goober_move", true);
    moveSprite.sprite.setScale(sscale);
    isprite = moveSprite;

    prepSprite = new ImageSprite("goober_prep", true);
    prepSprite.sprite.setScale(sscale);

    attackSprite = new ImageSprite("goober-attack2", false);
    attackSprite.sprite.setScale(sscale);

    bodyDamageSprite = new ImageSprite("goober-dmg", false);
    bodyDamageSprite.sprite.setScale(sscale);

    prepTime = 1.0f;
    attTime = 0.3f;
    recovTime = 3f;

    attackFC = new FrameCounter_Attack(prepTime, attTime, recovTime);

    //attack sensors
    attFd1 = new FixtureDef();
    PolygonShape vertBox = new PolygonShape();
    vertBox.setAsBox(width * 0.4f / PPM, (75f / PPM) * RATIO, new Vector2(0, (75f / PPM) * RATIO), 0);
    attFd1.shape = vertBox;
    attFd1.isSensor = true;
    attFd1.filter.categoryBits = BIT_EN;
    attFd1.filter.maskBits = BIT_PLAYER;

    attFd2 = new FixtureDef();
    PolygonShape horzBox = new PolygonShape();
    horzBox.setAsBox((75f / PPM) * RATIO, width * 0.4f / PPM, new Vector2((75f / PPM) * RATIO, 0), 0);
    attFd2.shape = horzBox;
    attFd2.isSensor = true;
    attFd2.filter.categoryBits = BIT_EN;
    attFd2.filter.maskBits = BIT_PLAYER;

    attFd3 = new FixtureDef();
    PolygonShape vertBoxSouth = new PolygonShape();
    vertBoxSouth.setAsBox(width * 0.4f / PPM, (75f / PPM) * RATIO, new Vector2(0, -(75f / PPM) * RATIO), 0);
    attFd3.shape = vertBoxSouth;
    attFd3.isSensor = true;
    attFd3.filter.categoryBits = BIT_EN;
    attFd3.filter.maskBits = BIT_PLAYER;

    attFd4 = new FixtureDef();
    PolygonShape horzBoxWest = new PolygonShape();
    horzBoxWest.setAsBox((75f / PPM) * RATIO, width * 0.4f / PPM, new Vector2(-(75f / PPM) * RATIO, 0), 0);
    attFd4.shape = horzBoxWest;
    attFd4.isSensor = true;
    attFd4.filter.categoryBits = BIT_EN;
    attFd4.filter.maskBits = BIT_PLAYER;

    //ai
    moveToSB = new Arrive<Vector2>(this, EnvironmentManager.player).setTimeToTarget(0.01f)
            .setArrivalTolerance(2f).setDecelerationRadius(10);

    wanderSB = new Wander<Vector2>(this).setFaceEnabled(false).setAlignTolerance(0.001f)
            .setDecelerationRadius(5).setTimeToTarget(0.1f).setWanderOffset(90).setWanderOrientation(10)
            .setWanderRadius(40f).setWanderRate(MathUtils.PI2 * 4);

    seekWanderSB = new Seek<Vector2>(this, seekWanderTarget);

    //ai
    this.maxLinearSpeed = 150f;
    this.maxLinearAcceleration = 500f;
    this.maxAngularSpeed = 30f;
    this.maxAngularAcceleration = 5f;

}

From source file:com.mygdx.entities.DynamicEntities.SteerableEntity.java

public SteerableEntity(Body b) {
    this(new Vector2(0, 0), 0f, 0f);

    this.body = b;
}