Example usage for com.badlogic.gdx.math Vector2 add

List of usage examples for com.badlogic.gdx.math Vector2 add

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Vector2 add.

Prototype

@Override
    public Vector2 add(Vector2 v) 

Source Link

Usage

From source file:com.punchables.rainbowdad.entity.Enemy.java

public void steerTo(Coord target, int radius, boolean avoidTiles) {
    //System.out.println(new Vector2(pos.x - getPos().x, pos.y - getPos().y));
    //setAccel(new Vector2(pos.x - getPos().x, pos.y - getPos().y).scl(200));

    boolean withinRadius = abs(target.x - getPos().x) <= radius && abs(target.y - getPos().y) <= radius;

    if (!withinRadius) {
        Vector2 desiredVel = new Vector2(target.x - getPos().x, target.y - getPos().y).nor().scl(getMaxVel());
        Vector2 steeringVel = desiredVel.sub(getVel());
        //System.out.println(desiredVel + " " + steeringVel);

        if (avoidTiles) {
            int maxAvoidForce = 64;
            ArrayList<MapTile> collidableTiles = refreshCollidableTiles(128);
            MapTile closestTile = null;/* w w  w.  j av  a  2  s  .com*/
            int closestTileDist = Coord.getDistanceSquared(new Coord(getPos()),
                    collidableTiles.get(0).getPos());
            for (MapTile tile : collidableTiles) {
                //float[] collideArray = Collider.checkCollision(new Circle(ahead, 1), tile, GameScreen.tileSize, true);
                int distSquared = Coord.getDistanceSquared(new Coord(getPos()), tile.getPos());
                if (distSquared < closestTileDist) {
                    closestTileDist = distSquared;
                    closestTile = tile;
                }
            }

            Vector2 avoidanceVelocity = new Vector2();
            if (closestTile != null) {
                avoidanceVelocity = new Vector2(closestTile.getPos().x - getPos().x,
                        closestTile.getPos().y - getPos().y).nor();
                avoidanceVelocity.scl(maxAvoidForce);
            } else {
                avoidanceVelocity = new Vector2();
                System.out.println("no close tiles");
            }

            steeringVel.add(avoidanceVelocity.scl(-1));

        }

        getVel().add(steeringVel);
    }

}

From source file:com.redtoorange.game.components.PlayerGunComponent.java

License:Open Source License

/** Fire a bullet from the gun and create the muzzle flash. */
private void fireBullet() {
    //set timers/*from   w w  w  .  ja v a  2 s  .c  o  m*/
    fireBullet = false;
    timeTillFire += coolDown;

    //grab the next available bullet
    Bullet b = bulletPool.get(bulletIndex);
    bulletIndex++;
    if (bulletIndex == MAX_BULLETS)
        bulletIndex = 0;

    //set the bullet's position and velocity.
    Vector2 bulletPosition = sc.getCenter();
    bulletPosition.add(new Vector2(0.35f, -0.3f).rotate(player.getRotation()));

    Vector2 velocity = new Vector2(in.getMousePosition().x - bulletPosition.x,
            in.getMousePosition().y - bulletPosition.y).nor();
    velocity.scl(speed);

    b.fire(bulletPosition, velocity, player.getRotation());

    //Fire off the muzzle flash
    muzzelFlash.setPosition(bulletPosition);
    muzzelFlash.setColor(Color.FIREBRICK);
    muzzelFlash.setActive(true);
    muzzleFlashTimer = muzzelFlashDwell;
}

From source file:com.redtoorange.game.states.MissionState.java

License:Open Source License

/** Render the lighting elements in the game, including lights and muzzle flashes. */
private void renderLighting() {
    if (!running)
        return;/*from   ww w .  j av  a  2 s.c  o m*/

    if (player != null && flashLight != null && playerLight != null) {
        Vector2 flashlightPoisition = ((SpriteComponent) player.getComponent(SpriteComponent.class))
                .getCenter();
        flashlightPoisition.add(new Vector2(-0.2f, 0.3f).rotate(player.getRotation()));

        flashLight.setDirection(player.getRotation());
        flashLight.setPosition(flashlightPoisition);
        playerLight.setPosition(player.getTransform().getPosition());

        lightingSystem.draw(camera);
    }
}

From source file:com.saltosion.gladiator.systems.RenderingSystem.java

License:Open Source License

private void renderGUINode(GUINode node, Vector2 position) {
    if (!node.isVisible()) {
        return;/*ww  w  . ja va 2  s . com*/
    }
    position.add(node.getPosition());
    if (node instanceof ImageProperty) {
        Sprite s = ((ImageProperty) node).getImage();
        s.setPosition(position.x * AppUtil.VPHEIGHT_CONST * aspectratio - s.getWidth() / 2 + camera.position.x,
                position.y * AppUtil.VPHEIGHT_CONST - s.getHeight() / 2 + camera.position.y);
        s.draw(batch);
    }
    if (node instanceof TextNode) {
        drawString(((TextProperty) node).getText(),
                new Vector2(position.x * AppUtil.VPHEIGHT_CONST * aspectratio + camera.position.x,
                        position.y * AppUtil.VPHEIGHT_CONST + camera.position.y));
    }
    for (GUINode child : node.getChildren()) {
        renderGUINode(child, new Vector2(position));
    }
}

From source file:com.sertaogames.cactus2d.components.TileMapPhysics.java

License:Open Source License

private void createBodyFromObject(Vector2 pos, Vector2 size) {
    Body rigidbody;//from   w  w  w.ja  va2 s .c  om
    PolygonShape groundPoly = new PolygonShape();

    size.mul(Cactus2DApplication.INV_PHYSICS_SCALE * 0.5f);
    groundPoly.setAsBox(size.x, size.y);

    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.fixedRotation = true;
    groundBodyDef.type = BodyType.StaticBody;
    rigidbody = gameObject.world.createBody(groundBodyDef);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = groundPoly;
    fixtureDef.filter.groupIndex = 0;
    fixtureDef.filter.categoryBits = 0x4;
    rigidbody.createFixture(fixtureDef);
    groundPoly.dispose();

    pos.add(transform.getPosition());
    pos.mul(Cactus2DApplication.INV_PHYSICS_SCALE);
    rigidbody.setTransform(pos, transform.getAngle());
    rigidbody.setUserData(gameObject);
    bodies.add(rigidbody);
}

From source file:com.sertaogames.cactus2d.components.TileMapPhysics.java

License:Open Source License

private void createBodyFromBlocks(int begin, int last, int i) {
    Vector2 tileSize = new Vector2(tm.tilemap.tileWidth, tm.tilemap.tileHeight);

    Body rigidbody;//from  w ww .ja va  2  s  .  c o  m
    PolygonShape groundPoly = new PolygonShape();
    int width = last - begin + 1;
    float height = tileSize.y;
    width *= tileSize.x;
    // System.out.println("i: "+i+" width: "+width);
    Vector2 temp = new Vector2(width, tileSize.y);
    temp.mul(Cactus2DApplication.INV_PHYSICS_SCALE * 0.5f);
    groundPoly.setAsBox(temp.x, temp.y);

    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.fixedRotation = true;
    groundBodyDef.type = BodyType.StaticBody;
    rigidbody = gameObject.world.createBody(groundBodyDef);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = groundPoly;
    fixtureDef.filter.groupIndex = 0;
    fixtureDef.filter.categoryBits = 0x4;
    rigidbody.createFixture(fixtureDef);
    groundPoly.dispose();

    temp.set(
            new Vector2(begin * tileSize.x + width / 2, (tm.tilemap.height - i) * tileSize.y - tileSize.y / 2));
    temp.add(transform.getPosition());
    temp.mul(Cactus2DApplication.INV_PHYSICS_SCALE);
    rigidbody.setTransform(temp, transform.getAngle());
    rigidbody.setUserData(gameObject);
    bodies.add(rigidbody);
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.math.GeometryUtils.java

License:Apache License

/** rotates a {@code point} around {@code center}
 *  @param point the point to rotate/*from  w  w w. ja va 2s. com*/
 *  @param origin the point around which to rotate {@code point}
 *  @param radians the rotation
 *  @return the given {@code point} rotated around {@code center} by {@code radians} */
public static Vector2 rotate(Vector2 point, Vector2 origin, float radians) {
    if (point.equals(origin))
        return point;
    return point.add(origin).rotateRad(radians).sub(origin);
}

From source file:com.tnf.ptm.common.CommonDrawer.java

License:Apache License

public void drawCircle(TextureRegion tex, Vector2 center, float radius, Color col, float width, float vh) {
    float relRad = radius / vh;
    int pointCount = (int) (160 * relRad);
    Vector2 pos = PtmMath.getVec();
    if (pointCount < 8) {
        pointCount = 8;//from   ww  w  . j a va2  s .co m
    }
    float lineLen = radius * PtmMath.PI * 2 / pointCount;
    float angleStep = 360f / pointCount;
    float angleStepH = angleStep / 2;
    for (int i = 0; i < pointCount; i++) {
        float angle = angleStep * i;
        PtmMath.fromAl(pos, angle, radius);
        pos.add(center);
        draw(tex, width, lineLen, (float) 0, (float) 0, pos.x, pos.y, angle + angleStepH, col);
    }
    PtmMath.free(pos);
}

From source file:com.tnf.ptm.common.GalaxyFiller.java

License:Apache License

private Vector2 getPosForStation(PtmSystem sys, boolean mainStation, ConsumedAngles angles) {
    Planet p;/* w w w.  jav a  2 s .  com*/
    ArrayList<Planet> planets = sys.getPlanets();
    float angleToSun;
    if (mainStation) {
        p = planets.get(planets.size() - 2);
        angleToSun = p.getAngleToSys() + 20 * PtmMath.toInt(p.getToSysRotSpd() > 0);
    } else {
        int pIdx = PtmMath.intRnd(planets.size() - 1);
        p = planets.get(pIdx);
        angleToSun = 0;
        for (int i = 0; i < 10; i++) {
            angleToSun = PtmMath.rnd(180);
            if (!angles.isConsumed(angleToSun, STATION_CONSUME_SECTOR)) {
                break;
            }
        }
    }
    angles.add(angleToSun, STATION_CONSUME_SECTOR);
    float stationDist = p.getDist() + p.getFullHeight() + Const.PLANET_GAP;
    Vector2 stationPos = new Vector2();
    PtmMath.fromAl(stationPos, angleToSun, stationDist);
    stationPos.add(p.getSys().getPos());
    return stationPos;
}

From source file:com.tnf.ptm.common.GalaxyFiller.java

License:Apache License

private Vector2 getEmptySpace(PtmGame game, PtmSystem s) {
    Vector2 res = new Vector2();
    Vector2 sPos = s.getPos();/*from   w w  w .jav  a  2s.  c o  m*/
    float sRadius = s.getConfig().hard ? s.getRadius() : s.getInnerRad();

    for (int i = 0; i < 100; i++) {
        PtmMath.fromAl(res, PtmMath.rnd(180), PtmMath.rnd(sRadius));
        res.add(sPos);
        if (game.isPlaceEmpty(res, true)) {
            return res;
        }
    }
    throw new AssertionError("could not generate ship position");
}