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.gamestudio24.martianrun.utils.WorldUtils.java

License:Apache License

public static Body createEnemy(World world) {
    EnemyType enemyType = RandomUtils.getRandomEnemyType();
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.KinematicBody;
    bodyDef.position.set(new Vector2(enemyType.getX(), enemyType.getY()));
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(enemyType.getWidth() / 2, enemyType.getHeight() / 2);
    Body body = world.createBody(bodyDef);
    body.createFixture(shape, enemyType.getDensity());
    body.resetMassData();/*from w  w  w  .j  ava 2 s  . c  om*/
    EnemyUserData userData = new EnemyUserData(enemyType.getWidth(), enemyType.getHeight(),
            enemyType.getAnimationAssetId());
    body.setUserData(userData);
    shape.dispose();
    return body;
}

From source file:com.gdx.game.screens.PlayScreen.java

public PlayScreen(GdxGame game, AssetManager assetManager) {
    atlas = new TextureAtlas("Mario_and_Enemies.pack");

    this.game = game;
    this.assetManager = assetManager;
    this.gameCam = new OrthographicCamera();
    this.gamePort = new FitViewport(GdxGame.V_WIDTH / GdxGame.PPM, GdxGame.V_HEIGHT / GdxGame.PPM, gameCam);

    // create HUD
    this.hud = new Hud(game.getBatch());

    // load map and setup renderer
    this.mapLoader = new TmxMapLoader();
    this.map = mapLoader.load("level1.tmx");
    this.renderer = new OrthogonalTiledMapRenderer(this.map, 1 / GdxGame.PPM);

    this.gameCam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);

    this.world = new World(new Vector2(0, -10), true);
    this.box2DDebugRenderer = new Box2DDebugRenderer();

    new b2WorldCreator(world, map, this.assetManager);

    this.player = new Player(this.world, this);

    music = this.assetManager.get("audio/music/mario_music.ogg", Music.class);
    music.setLooping(true);/*from   w w  w . j a  v a2  s .  c  om*/
    music.play();

    world.setContactListener(new WolrdContactListener());

}

From source file:com.gdx.game.screens.PlayScreen.java

private void handleInput(float dt) {
    if (Gdx.input.isKeyJustPressed(Input.Keys.UP)) {
        player.b2body.applyLinearImpulse(new Vector2(0, 5f), player.b2body.getWorldCenter(), true);
    }/*from ww  w  . j a v  a2  s  .  c o  m*/
    if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x <= 2) {
        player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2) {
        player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);
    }

}

From source file:com.gdx.game.sprites.Player.java

private void definePlayer() {
    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(32 / GdxGame.PPM, 32 / GdxGame.PPM);
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    this.b2body = world.createBody(bodyDef);

    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(6 / GdxGame.PPM);/*from w  w  w .ja va  2 s  . co m*/
    fdef.filter.categoryBits = GdxGame.MARIO_BIT;
    fdef.filter.maskBits = GdxGame.DEFAULT_BIT | GdxGame.BRICK_BIT | GdxGame.COIN_BIT;

    fdef.shape = shape;
    b2body.createFixture(fdef);

    EdgeShape head = new EdgeShape();
    head.set(new Vector2(-2 / GdxGame.PPM, 6 / GdxGame.PPM), new Vector2(2 / GdxGame.PPM, 6 / GdxGame.PPM));
    fdef.shape = head;
    fdef.isSensor = true;

    b2body.createFixture(fdef).setUserData("head");
}

From source file:com.github.fauu.helix.core.MapRegion.java

License:Open Source License

public void create(Vector2 size, Tile[] tiles, Array<Object> objects, TextureAtlas textureAtlas,
        GeometrySet geometrySet) {/*w  w  w. java2  s  .c o m*/
    this.size = size;
    this.textureAtlas = textureAtlas;
    this.geometrySet = geometrySet;
    this.objects = objects;

    if (tiles != null) {
        this.tiles = tiles;
    } else {
        final int tilesLength = (int) (size.x * size.y);

        tiles = new Tile[tilesLength];
        for (int i = 0; i < tilesLength; i++) {
            final Tile.Builder tileBuilder = new Tile.Builder();
            final int tileX = i % (int) size.x;
            final int tileY = (int) Math.floor(i / (tilesLength / size.y));

            tiles[i] = tileBuilder.setNo(i).setPosition(new Vector2(tileX, tileY)).setElevation(0)
                    .setTextureId(0).setGeometryId(0).setFacing(Direction.SOUTH).build();
        }
    }

    this.tiles = tiles;

    mesh = new MapRegionMesh(tiles, geometrySet, textureAtlas);

    renderable = new Renderable();
    renderable.mesh = mesh;
    renderable.material = new Material(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE),
            new TextureAttribute(TextureAttribute.Diffuse, textureAtlas.getTextures().first()));
    renderable.meshPartOffset = 0;
    renderable.meshPartSize = mesh.getNumVertices();
    renderable.primitiveType = GL20.GL_TRIANGLES;
    renderable.worldTransform.idt();
}

From source file:com.github.fauu.helix.core.MapRegion.java

License:Open Source License

public Vector2 getCenterCoordinates() {
    final float centerX = (size.x / 2);
    final float centerY = (size.y / 2);

    return new Vector2(centerX, centerY);
}

From source file:com.github.fauu.helix.core.MapRegionLoader.java

License:Open Source License

protected MapRegion loadMapRegion(AssetManager manager, XmlReader.Element root, FileHandle hmrFile) {
    final int mapRegionWidth = root.getIntAttribute("width", 0);
    final int mapRegionHeight = root.getIntAttribute("height", 0);

    final Tile[] tiles = new Tile[mapRegionWidth * mapRegionHeight];
    final XmlReader.Element tilesElement = root.getChildByName("tiles");

    int tileCount = 0;
    for (XmlReader.Element tileElement : tilesElement.getChildrenByName("tile")) {
        final int tilePositionX = tileElement.getIntAttribute("x", 0);
        final int tilePositionY = tileElement.getIntAttribute("y", 0);
        final int tileElevation = tileElement.getIntAttribute("elevation", 0);
        final int tileTextureId = tileElement.getIntAttribute("textureid", 0);
        final int tileGeometryId = tileElement.getIntAttribute("geometryid", 0);
        final String tileFacingString = tileElement.getAttribute("facing", "south");
        final String tileTypeString = tileElement.getAttribute("type", "default_walkable");

        Direction tileFacing = Direction.SOUTH;
        if (tileFacingString.compareTo("north") == 0) {
            tileFacing = Direction.NORTH;
        } else if (tileFacingString.compareTo("east") == 0) {
            tileFacing = Direction.EAST;
        } else if (tileFacingString.compareTo("south") == 0) {
            tileFacing = Direction.SOUTH;
        } else if (tileFacingString.compareTo("west") == 0) {
            tileFacing = Direction.WEST;
        }//from  w w  w.j ava 2 s.c om

        Tile.Type tileType = Tile.Type.DEFAULT_WALKABLE;
        if (tileTypeString.compareTo("default_nonwalkable") == 0) {
            tileType = Tile.Type.DEFAULT_NONWALKABLE;
        } else if (tileTypeString.compareTo("default_walkable") == 0) {
            tileType = Tile.Type.DEFAULT_WALKABLE;
        }

        final Tile tile = new Tile.Builder().setNo(tileCount)
                .setPosition(new Vector2(tilePositionX, tilePositionY)).setElevation(tileElevation)
                .setTextureId(tileTextureId).setFacing(tileFacing).setGeometryId(tileGeometryId)
                .setType(tileType).build();

        tiles[tileCount++] = tile;
    }

    final Array<Object> objects = new Array<Object>();
    final XmlReader.Element objectsElement = root.getChildByName("objects");

    for (XmlReader.Element objectElement : objectsElement.getChildrenByName("object")) {
        final int objectPositionX = objectElement.getIntAttribute("x", 0);
        final int objectPositionY = objectElement.getIntAttribute("y", 0);
        final String objectModelName = objectElement.getAttribute("model");
        final Model objectModel = manager.get("assets/objects/" + objectModelName + ".g3db");
        final int objectElevation = objectElement.getIntAttribute("elevation", 0);
        final String objectFacingString = objectElement.getAttribute("facing", "south");

        Direction objectFacing = Direction.SOUTH;
        if (objectFacingString.compareTo("north") == 0) {
            objectFacing = Direction.NORTH;
        } else if (objectFacingString.compareTo("east") == 0) {
            objectFacing = Direction.EAST;
        } else if (objectFacingString.compareTo("south") == 0) {
            objectFacing = Direction.SOUTH;
        } else if (objectFacingString.compareTo("west") == 0) {
            objectFacing = Direction.WEST;
        }

        final Object object = new Object(new Vector2(objectPositionX, objectPositionY), objectModelName,
                objectModel, objectElevation, objectFacing);

        objects.add(object);
    }

    final int geometrySetId = root.getIntAttribute("geometrysetid");
    final int textureSetId = root.getIntAttribute("texturesetid");

    // FIXME: File paths shouldn't be handled like that
    mapRegion = new MapRegion(new Vector2(mapRegionWidth, mapRegionHeight), tiles, objects,
            manager.get("assets/texturesets/" + Integer.toString(textureSetId) + ".atlas", TextureAtlas.class),
            manager.get("assets/geometrysets/" + Integer.toString(geometrySetId) + ".hgs", GeometrySet.class));

    return mapRegion;
}

From source file:com.github.fauu.helix.core.MapRegionMesh.java

License:Open Source License

public void update() {
    ready = false;/*ww  w  . ja v a2s . c o m*/
    tileOffsets = new int[tiles.length];

    final float[] vertices = new float[numVertices];

    int vertexCount = 0;
    for (int i = 0; i < tiles.length; i++) {
        final Tile tile = tiles[i];
        final Model tileGeometryModel = geometrySet.getGeometry(tile.getGeometryId()).getModel();

        float[] geometryVertices = new float[(NUM_COMPONENTS - NUM_COLOR_COMPONENTS)
                * tileGeometryModel.meshes.first().getNumVertices()];

        final Texture texture = textureAtlas.getTextures().first();
        final TextureRegion tileTextureRegion = textureAtlas.getRegions().get(tile.getTextureId());

        final Vector2 tileTextureCoords = new Vector2(tileTextureRegion.getU(), tileTextureRegion.getV2());
        final Vector2 tileTextureScaling = new Vector2(
                (float) tileTextureRegion.getRegionWidth() / texture.getWidth(),
                (float) tileTextureRegion.getRegionHeight() / texture.getHeight());

        float rotationDegrees = 0;
        switch (tile.getFacing()) {
        case SOUTH:
            rotationDegrees = -90;
            break;
        case WEST:
            rotationDegrees = -180;
            break;
        case NORTH:
            rotationDegrees = 90;
            break;
        case EAST:
            rotationDegrees = 0;
            break;
        default:
        }

        final Matrix3 transformationMatrixUV = new Matrix3().translate(tileTextureCoords)
                .scale(1 * tileTextureScaling.x, -1 * tileTextureScaling.y).translate(0, -1);

        final Matrix4 transformationMatrix = new Matrix4().translate(
                new Vector3(0.5f + tile.getPosition().x, tile.getElevation(), 0.5f + tile.getPosition().y))
                .rotate(new Vector3(0, 1, 0), rotationDegrees);

        // Not sure if copying isn't slower than detransforming the original, but it fixes glitchy
        // edges
        final Mesh tileGeometryMesh = tileGeometryModel.meshes.first().copy(true);

        tileGeometryMesh.transform(transformationMatrix);
        tileGeometryMesh.transformUV(transformationMatrixUV);
        tileGeometryMesh.getVertices(geometryVertices);

        final float[] coloredVertices = new float[NUM_COMPONENTS
                * tileGeometryModel.meshes.get(0).getNumVertices()];

        final Color tileColor = tile.getColor();

        for (int j = 0, k = 0; j < coloredVertices.length; j++) {
            if ((j + 1) % 6 != 0) {
                coloredVertices[j] = geometryVertices[k++];
            } else {
                coloredVertices[j] = tileColor.toFloatBits();
            }
        }

        tileOffsets[i] = vertexCount;

        System.arraycopy(coloredVertices, 0, vertices, vertexCount, coloredVertices.length);

        vertexCount += coloredVertices.length;
    }

    setVertices(vertices, 0, vertexCount);
    ready = true;
}

From source file:com.github.fauu.helix.editor.EditorFrame.java

License:Open Source License

public void createMapRegion(int width, int height) {
    // FIXME: Needs to allow other texture sets and geometry sets
    mapRegion.create(new Vector2(width, height), null, null, mapRegion.getTextureAtlas(),
            mapRegion.getGeometrySet());

    camera.moveTo(mapRegion.getCenterCoordinates());
}

From source file:com.github.fauu.helix.editor.World.java

License:Open Source License

public void handleInput() {
    final Tile newMatchedTile = matchHoveredTile(Gdx.input.getX(), Gdx.input.getY());
    Object newMatchedObject;//  w w  w.  j a v a2 s  .  c o m

    if (((EditorMode) editorFrame.getSidebar().getEditorModePicker().getSelectedItem())
            .getType() == EditorMode.Type.OBJECT) {
        newMatchedObject = matchHoveredObject(Gdx.input.getX(), Gdx.input.getY());

        if (newMatchedObject != matchedObject) {
            if (matchedObject != null) {
                mapRegion.setObjectColor(matchedObject, Color.WHITE);
            }

            if (newMatchedObject != null) {
                mapRegion.setObjectColor(newMatchedObject, new Color(0.5f, 0.5f, 0.5f, 1));
            }

            matchedObject = newMatchedObject;
        }

        if (newMatchedObject != null) {
            if (Gdx.input.isButtonPressed(Input.Buttons.MIDDLE)) {
                mapRegion.deleteObject(newMatchedObject);
            }
        }
    }

    if (Gdx.input.isTouched()) {
        final EditorMode.Type editorMode = ((EditorMode) editorFrame.getSidebar().getEditorModePicker()
                .getSelectedItem()).getType();

        if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
            if (!currentTilePainted) {
                switch (editorMode) {
                case TILE_GENERAL:
                    paintTile(newMatchedTile);
                    break;
                case TILE_TYPE:
                    paintTileType(newMatchedTile);
                    break;
                case OBJECT:
                    paintObject(newMatchedTile);
                    break;
                default:
                }

                currentTilePainted = true;
            }
        } else if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)) {
            final int deltaX = Gdx.input.getDeltaX();
            final int deltaY = Gdx.input.getDeltaY();

            camera.move(new Vector3(-CAMERA_DRAG_FACTOR * deltaX, 0, -CAMERA_DRAG_FACTOR * deltaY));
        }
    }

    if (matchedTile != newMatchedTile) {
        if (matchedTile != null) {
            matchedTile.setHighlighted(false);

            mapRegion.getMesh().updateTileColor(matchedTile);
        }

        if (newMatchedTile != null) {
            newMatchedTile.setHighlighted(true);

            mapRegion.getMesh().updateTileColor(newMatchedTile);

            // TODO: Add more info to the status bar
            editorFrame.getStatusBar().setTileInfoText("Position: " + newMatchedTile.getPosition());
        } else {
            editorFrame.getStatusBar().setTileInfoText(StatusBar.TILE_INFO_DEFAULT_TEXT);
        }

        matchedTile = newMatchedTile;
        currentTilePainted = false;
    }

    if (Gdx.input.isKeyPressed(Input.Keys.A)) {
        camera.rotateYAround(
                new Vector2(mapRegion.getCenterCoordinates().x, mapRegion.getCenterCoordinates().y), -2);
    } else if (Gdx.input.isKeyPressed(Input.Keys.D)) {
        camera.rotateYAround(
                new Vector2(mapRegion.getCenterCoordinates().x, mapRegion.getCenterCoordinates().y), 2);
    } else if (Gdx.input.isKeyPressed(Input.Keys.S)) {
        camera.resetYRotationAround(
                new Vector2(mapRegion.getCenterCoordinates().x, mapRegion.getCenterCoordinates().y));
    }

    final EditorMode.Type editorMode = ((EditorMode) editorFrame.getSidebar().getEditorModePicker()
            .getSelectedItem()).getType();

    if (editorMode == EditorMode.Type.TILE_GENERAL || editorMode == EditorMode.Type.TILE_TYPE) {
        if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
            if (!mapRegion.areObjectsHidden()) {
                mapRegion.hideObjects();
            }
        } else {
            if (mapRegion.areObjectsHidden()) {
                mapRegion.showObjects();
            }
        }
    }
}