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.bladecoder.engineeditor.scneditor.ScnWidgetInputListener.java

License:Apache License

@Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
    // EditorLogger.debug("Touch Dragged - X: " + Gdx.input.getX() + " Y: "
    // + Gdx.input.getY());

    super.touchDragged(event, x, y, pointer);

    if (Gdx.input.isButtonPressed(Buttons.LEFT)) {
        Scene scn = scnWidget.getScene();

        if (scn == null)
            return;

        Vector2 d = new Vector2(Gdx.input.getX(), Gdx.input.getY());
        scnWidget.screenToWorldCoords(d);

        d.sub(org);//from w  w w . j a v a 2 s.c om
        org.add(d);

        if (draggingMode == DraggingModes.DRAGGING_ACTOR) {

            selActor.setPosition(selActor.getX() + d.x, selActor.getY() + d.y);
            Ctx.project.setModified(this, Project.POSITION_PROPERTY, null, selActor);

        } else if (draggingMode == DraggingModes.DRAGGING_BBOX_POINT) {
            Polygon poly = selActor.getBBox();

            float verts[] = poly.getVertices();
            verts[vertIndex] += d.x;
            verts[vertIndex + 1] += d.y;
            poly.dirty();

            Ctx.project.setModified();
        } else if (draggingMode == DraggingModes.DRAGGING_WALKZONE_POINT) {
            Polygon poly = scn.getPolygonalNavGraph().getWalkZone();

            float verts[] = poly.getVertices();
            verts[vertIndex] += d.x;
            verts[vertIndex + 1] += d.y;
            poly.dirty();

            Ctx.project.setModified();
        } else if (draggingMode == DraggingModes.DRAGGING_WALKZONE) {
            Polygon poly = scn.getPolygonalNavGraph().getWalkZone();
            poly.translate(d.x, d.y);
        } else if (draggingMode == DraggingModes.DRAGGING_MARKER_0) {
            Vector2 depthVector = scnWidget.getScene().getDepthVector();

            depthVector.x += d.y;
            Ctx.project.setModified();
            updateFakeDepth();
        } else if (draggingMode == DraggingModes.DRAGGING_MARKER_100) {
            Vector2 depthVector = scnWidget.getScene().getDepthVector();

            depthVector.y += d.y;
            Ctx.project.setModified();
            updateFakeDepth();
        }

    } else if (Gdx.input.isButtonPressed(Buttons.RIGHT) || Gdx.input.isButtonPressed(Buttons.MIDDLE)) {

        Vector2 p = new Vector2(Gdx.input.getX(), Gdx.input.getY());
        scnWidget.screenToWorldCoords(p);
        p.sub(org);

        scnWidget.translate(p);
    }
}

From source file:com.bladecoder.engineeditor.scneditor.SpriteDrawer.java

License:Apache License

public void setActor(BaseActor a) {
    if (renderer != null) {
        renderer.dispose();/*from  w w  w .j av  a2 s. c  o m*/
        renderer = null;
    }

    if (a instanceof SpriteActor) {
        ActorRenderer r = ((SpriteActor) a).getRenderer();

        if (r instanceof Sprite3DRenderer) {
            renderer = new Sprite3DRenderer();
            ((Sprite3DRenderer) renderer).setSpriteSize(new Vector2(r.getWidth(), r.getHeight()));
        } else if (r instanceof SpineRenderer) {
            renderer = new SpineRenderer();
            ((SpineRenderer) renderer).enableEvents(false);
        } else if (r instanceof ImageRenderer) {
            renderer = new ImageRenderer();
        } else {
            renderer = new AtlasRenderer();
        }
    }
}

From source file:com.bss.game.GameScreen.java

License:Apache License

private void updateRunning(float deltaTime) {
    if (Gdx.input.justTouched()) {
        scaleVP.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));

        if (pauseBounds.contains(touchPoint.x, touchPoint.y)) {
            Assets.playSound(Assets.clickSound);
            state = GAME_PAUSED;// w w  w  . ja v a2s  . c  o  m
            pauseStartTime = TimeUtils.millis();
            return;
        } else if (touchPoint.x > 150f && !world.hook.hookingNow) {
            //world.makeHook(touchPoint.x, touchPoint.y);
            //calculate the sin or whatever angle and send it to world
            //and maybe pass it a true
            touch = new Vector2(touchPoint.x, touchPoint.y);
            world.castHook(touchPoint.x, touchPoint.y, touch);
        } else if (touchPoint.x < 97f && !world.hook.hookingNow) {
            //touch = new Vector2(touchPoint.x, touchPoint.y);
            world.moveSlappy(touchPoint.y);
        }
    }

    world.update(deltaTime);

    totalTimeEnd = TimeUtils.millis();
    totalTime = (totalTimeEnd - totalTimeStart) - totalPauseTime;
    if (totalTime > Assets.getHighScore()) {
        Assets.setHighScore(totalTime);
    }

    if (world.state == World.WORLD_STATE_GAME_OVER) {
        state = GAME_OVER;
    }
}

From source file:com.bss.game.World.java

License:Apache License

public void moveSlappy(float y) {

    Vector2 move = new Vector2(80f, y);
    hook.movetotal = 0f;//from   w  w w.ja v  a 2 s  .c  o  m
    hook.moveD = (float) Math.sqrt(Math.pow(y - hook.position.y, 2));
    hook.direction.set(move).sub(hook.position).nor();
    if ((hook.position.y > 725f && y < 725f) || (hook.position.y < 150f && y > 150f)) {
        hook.canMove = true;
    }
    hook.state = 1;
}

From source file:com.cafeitvn.myballgame.screen.Box2DMapObjectParser.java

License:Apache License

/**
 * creates a {@link Fixture} from a {@link MapObject}
 * @param mapObject the {@link MapObject} which to parse
 * @return the parsed {@link Fixture}/*  ww w  . j  a va 2  s .  co m*/
 */
public Fixture createFixture(MapObject mapObject) {
    MapProperties properties = mapObject.getProperties();

    String type = properties.get("type", String.class);

    Body body = bodies.get(
            type.equals(aliases.object) ? mapObject.getName() : properties.get(aliases.body, String.class));

    if (!type.equals(aliases.fixture) && !type.equals(aliases.object))
        throw new IllegalArgumentException("type of " + mapObject + " is  \"" + type + "\" instead of \""
                + aliases.fixture + "\" or \"" + aliases.object + "\"");

    FixtureDef fixtureDef = new FixtureDef();
    Shape shape = null;

    if (mapObject instanceof RectangleMapObject) {
        shape = new PolygonShape();
        Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
        rectangle.x *= unitScale;
        rectangle.y *= unitScale;
        rectangle.width *= unitScale;
        rectangle.height *= unitScale;
        ((PolygonShape) shape).setAsBox(rectangle.width / 2, rectangle.height / 2,
                new Vector2(rectangle.x - body.getPosition().x + rectangle.width / 2,
                        rectangle.y - body.getPosition().y + rectangle.height / 2),
                body.getAngle());
    } else if (mapObject instanceof PolygonMapObject) {
        shape = new PolygonShape();
        Polygon polygon = ((PolygonMapObject) mapObject).getPolygon();
        polygon.setPosition(polygon.getX() * unitScale - body.getPosition().x,
                polygon.getY() * unitScale - body.getPosition().y);
        polygon.setScale(unitScale, unitScale);
        ((PolygonShape) shape).set(polygon.getTransformedVertices());
    } else if (mapObject instanceof PolylineMapObject) {
        shape = new ChainShape();
        Polyline polyline = ((PolylineMapObject) mapObject).getPolyline();
        polyline.setPosition(polyline.getX() * unitScale - body.getPosition().x,
                polyline.getY() * unitScale - body.getPosition().y);
        polyline.setScale(unitScale, unitScale);
        ((ChainShape) shape).createChain(polyline.getTransformedVertices());
    } else if (mapObject instanceof CircleMapObject) {
        shape = new CircleShape();
        Circle circle = ((CircleMapObject) mapObject).getCircle();
        circle.setPosition(circle.x * unitScale - body.getPosition().x,
                circle.y * unitScale - body.getPosition().y);
        circle.radius *= unitScale;
        ((CircleShape) shape).setPosition(new Vector2(circle.x, circle.y));
        ((CircleShape) shape).setRadius(circle.radius);
    } else if (mapObject instanceof EllipseMapObject) {
        Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();

        if (ellipse.width == ellipse.height) {
            CircleMapObject circleMapObject = new CircleMapObject(ellipse.x, ellipse.y, ellipse.width / 2);
            circleMapObject.setName(mapObject.getName());
            circleMapObject.getProperties().putAll(mapObject.getProperties());
            circleMapObject.setColor(mapObject.getColor());
            circleMapObject.setVisible(mapObject.isVisible());
            circleMapObject.setOpacity(mapObject.getOpacity());
            return createFixture(circleMapObject);
        }

        IllegalArgumentException exception = new IllegalArgumentException(
                "Cannot parse " + mapObject.getName() + " because " + mapObject.getClass().getSimpleName()
                        + "s that are not circles are not supported");
        Gdx.app.error(getClass().getSimpleName(), exception.getMessage(), exception);
        throw exception;
    } else if (mapObject instanceof TextureMapObject) {
        IllegalArgumentException exception = new IllegalArgumentException("Cannot parse " + mapObject.getName()
                + " because " + mapObject.getClass().getSimpleName() + "s are not supported");
        Gdx.app.error(getClass().getSimpleName(), exception.getMessage(), exception);
        throw exception;
    } else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();

    fixtureDef.shape = shape;
    fixtureDef.density = (Float) getProperty(properties, aliases.density, fixtureDef.density, Float.class);
    fixtureDef.filter.categoryBits = (Short) getProperty(properties, aliases.categoryBits,
            fixtureDef.filter.categoryBits, Short.class);
    fixtureDef.filter.groupIndex = (Short) getProperty(properties, aliases.groupIndex,
            fixtureDef.filter.groupIndex, Short.class);
    fixtureDef.filter.maskBits = (Short) getProperty(properties, aliases.maskBits, fixtureDef.filter.maskBits,
            Short.class);
    fixtureDef.friction = (Float) getProperty(properties, aliases.friciton, fixtureDef.friction, Float.class);
    fixtureDef.isSensor = (Boolean) getProperty(properties, aliases.isSensor, fixtureDef.isSensor,
            Boolean.class);
    fixtureDef.restitution = (Float) getProperty(properties, aliases.restitution, fixtureDef.restitution,
            Float.class);

    Fixture fixture = body.createFixture(fixtureDef);

    shape.dispose();

    String name = mapObject.getName();
    if (fixtures.containsKey(name)) {
        int duplicate = 1;
        while (fixtures.containsKey(name + duplicate))
            duplicate++;
        name += duplicate;
    }

    fixtures.put(name, fixture);

    return fixture;
}

From source file:com.car.utils.TiledMapHelper.java

License:Apache License

private void createCarPosition(TiledObject object) {
    int position = Integer.parseInt(object.properties.get(Constants.CAR_POSITION_KEY));
    float angle = Integer.parseInt(object.properties.get(Constants.CAR_ANGLE_KEY));

    float coordX = getWorldXFromMapX(object.x);
    float coordY = getWorldYFromMapY(object.y);

    Vector2 worldPos = new Vector2(coordX, coordY);
    System.out.println("Position " + position + " loaded at " + worldPos + " angle " + angle);
    racePositions.put(position, new CarPosition(position, coordX, coordY, angle));
}

From source file:com.car.utils.TiledMapHelper.java

License:Apache License

private List<Vector2> buildWorldLineFromTiledObject(TiledObject object) {
    List<Vector2> line = new ArrayList<Vector2>();
    float iniX = object.x;
    float iniY = object.y;

    String[] points = object.polyline.split(" ");
    for (String point : points) {
        String[] coords = point.split(",");
        float x = Float.parseFloat(coords[0]) + iniX;
        float y = Float.parseFloat(coords[1]) + iniY;

        Vector2 worldVertex = new Vector2(getWorldXFromMapX(x), getWorldYFromMapY(y));
        //System.out.println(worldVertex);
        line.add(worldVertex);/* w  w  w .  j av  a 2  s. c  om*/
    }

    return line;
}

From source file:com.cmein.tilemap.utils.TileSetLayout.java

License:Apache License

/**
 * Constructs a Tile Set layout. The tile set image contained in the baseDir should be the original tile set images before
 * being processed by {@link TiledMapPacker} (the ones actually read by Tiled).
 * @param tileSet the tile set to process
 * @param baseDir the directory in which the tile set image is stored
 * */// www .j a  v  a2s.co  m
TileSetLayout(TileSet tileSet, FileHandle baseDir) throws IOException {
    this.tileSet = tileSet;
    image = ImageIO.read(baseDir.child(tileSet.imageName).read());

    imageTilePositions = new IntMap<Vector2>();

    // fill the tile regions
    int x, y, tile = 0;
    numRows = 0;
    numCols = 0;
    for (y = tileSet.margin; y < image.getHeight() - tileSet.margin; y += tileSet.tileHeight
            + tileSet.spacing) {
        for (x = tileSet.margin; x < image.getWidth() - tileSet.margin; x += tileSet.tileWidth
                + tileSet.spacing) {
            if (y == tileSet.margin)
                numCols++;
            imageTilePositions.put(tile, new Vector2(x, y));
            tile++;
        }
        numRows++;
    }

    numTiles = numRows * numCols;
}

From source file:com.codefiddler.libgdx.tetris.domain.ActivePlayBoard.java

License:Apache License

public void drawActiveShapeInSettledBoardAndGetNextShape() {
    activePiece.drawIn(settledPlayboard);
    addScore(activePiece);/*from   w  ww .  j a va  2  s. com*/
    activePiece = nextShapes.pop();
    if (nextShapes.isEmpty()) {
        generateNextShapes();
    }
    activePiece.setPosition(new Vector2(0, 0));
    setInitialPosition(activePiece);
    int cleanUpPoints = settledPlayboard.cleanUp();
    if (cleanUpPoints > 0) {
        addScore(cleanUpPoints);
    }
    if (!activePiece.tick(settledPlayboard)) {
        gameRunning = false;
    }
}

From source file:com.codefiddler.libgdx.tetris.domain.ActivePlayBoard.java

License:Apache License

private void setInitialPosition(Tetrimino activePiece) {
    Vector2 position = new Vector2(settledPlayboard.getWidth() / 2, settledPlayboard.getHeight() - 1);
    int halfWidthOfPiece = activePiece.getWidth() / 2;
    position.x -= halfWidthOfPiece;//from  w w w.  ja v  a 2  s  . c  om
    activePiece.setPosition(position);
}