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

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

Introduction

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

Prototype

@Override
    public Vector2 set(Vector2 v) 

Source Link

Usage

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

License:Apache License

public Vector2 getPlayerSpawnPos(PtmGame game) {
    Vector2 pos = new Vector2(Const.SUN_RADIUS * 2, 0);

    if ("planet".equals(DebugOptions.SPAWN_PLACE)) {
        Planet p = game.getPlanetMan().getPlanets().get(0);
        pos.set(p.getPos());
        pos.x += p.getFullHeight();/*w  w w. j  a va 2 s  .  c  om*/
    } else if (DebugOptions.SPAWN_PLACE.isEmpty() && myMainStationPos != null) {
        PtmMath.fromAl(pos, 90, myMainStationHc.getSize() / 2);
        pos.add(myMainStationPos);
    } else if ("maze".equals(DebugOptions.SPAWN_PLACE)) {
        Maze m = game.getPlanetMan().getMazes().get(0);
        pos.set(m.getPos());
        pos.x += m.getRadius();
    } else if ("trader".equals(DebugOptions.SPAWN_PLACE)) {
        HullConfig cfg = game.getHullConfigs().getConfig(new ResourceUrn("core:bus"));
        for (FarObjData fod : game.getObjMan().getFarObjs()) {
            FarObj fo = fod.fo;
            if (!(fo instanceof FarShip)) {
                continue;
            }
            if (((FarShip) fo).getHullConfig() != cfg) {
                continue;
            }
            pos.set(fo.getPos());
            pos.add(cfg.getApproxRadius() * 2, 0);
            break;
        }

    }
    return pos;
}

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

License:Apache License

/**
 * converts relPos (a position in a relative coordinate system defined by baseAngle and basePos) to the absolute position (which is written to pos)
 *///from  www .j  a  v  a  2 s  . c  om
public static void toWorld(Vector2 pos, Vector2 relPos, float baseAngle, Vector2 basePos, boolean precise) {
    pos.set(relPos);
    rotate(pos, baseAngle, precise);
    pos.add(basePos);
}

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

License:Apache License

/**
 * converts pos (a position in an absolute coordinate system) to the position in the relative system of coordinates
 * (defined by baseAngle and basePos) (which is written to relPos)
 *///from w w  w  .  jav  a2 s .  c  om
public static void toRel(Vector2 pos, Vector2 relPos, float baseAngle, Vector2 basePos) {
    relPos.set(pos);
    relPos.sub(basePos);
    rotate(relPos, -baseAngle);
}

From source file:com.tnf.ptm.entities.planet.FlatPlaceFinder.java

License:Apache License

public Vector2 find(PtmGame game, Planet p, ConsumedAngles takenAngles, float objHalfWidth) {
    Vector2 pPos = p.getPos();/*from  w  ww .  j  ava2 s  .c o  m*/

    Vector2 res = new Vector2(pPos);
    float minDeviation = 90;
    float resAngle = 0;
    float objAngularHalfWidth = PtmMath.angularWidthOfSphere(objHalfWidth, p.getGroundHeight());

    for (int i = 0; i < 20; i++) {
        float angle = PtmMath.rnd(180);
        if (takenAngles != null && takenAngles.isConsumed(angle, objAngularHalfWidth)) {
            continue;
        }
        myDeviation = angle;
        PtmMath.fromAl(myVec, angle, p.getFullHeight());
        myVec.add(pPos);
        game.getObjMan().getWorld().rayCast(myRayBack, myVec, pPos);
        if (myDeviation < minDeviation) {
            res.set(myVec);
            minDeviation = myDeviation;
            resAngle = angle;
        }
    }

    if (takenAngles != null) {
        takenAngles.add(resAngle, objAngularHalfWidth);
    }
    res.sub(pPos);
    PtmMath.rotate(res, -p.getAngle());
    return res;
}

From source file:com.tnf.ptm.entities.planet.Planet.java

License:Apache License

@Bound
public Vector2 getAdjustedEffectSpd(Vector2 pos, Vector2 spd) {
    Vector2 r = PtmMath.getVec(spd);
    if (myConfig.skyConfig == null) {
        return r;
    }//from   w  w w.ja v a  2 s .  c o  m
    Vector2 up = PtmMath.distVec(myPos, pos);
    float dst = up.len();
    if (dst == 0 || getFullHeight() < dst) {
        PtmMath.free(up);
        return r;
    }
    float smokeConst = 1.2f * myGravConst;
    if (dst < myGroundHeight) {
        up.scl(smokeConst / dst / myGroundHeight / myGroundHeight);
        r.set(up);
        PtmMath.free(up);
        return r;
    }
    float spdPerc = (dst - myGroundHeight) / Const.ATM_HEIGHT;
    r.scl(spdPerc);
    up.scl(smokeConst / dst / dst / dst);
    r.add(up);
    PtmMath.free(up);
    return r;
}

From source file:com.tnf.ptm.entities.planet.PlanetObjectsBuilder.java

License:Apache License

private void addDeco0(PtmGame game, float groundHeight, Vector2 planetPos, Map<Vector2, List<Dra>> collector,
        DecoConfig dc) {/*w  w w  .j  a va2 s . c  o  m*/
    World w = game.getObjMan().getWorld();
    ConsumedAngles consumed = new ConsumedAngles();

    final Vector2 rayCasted = new Vector2();
    RayCastCallback rcc = new RayCastCallback() {
        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
            if (!(fixture.getBody().getUserData() instanceof TileObject)) {
                return -1;
            }
            rayCasted.set(point);
            return fraction;
        }
    };

    int decoCount = (int) (2 * PtmMath.PI * groundHeight * dc.density);
    for (int i = 0; i < decoCount; i++) {
        float decoSz = PtmMath.rnd(dc.szMin, dc.szMax);
        float angularHalfWidth = PtmMath.angularWidthOfSphere(decoSz / 2, groundHeight);

        float decoAngle = 0;
        for (int j = 0; j < 5; j++) {
            decoAngle = PtmMath.rnd(180);
            if (!consumed.isConsumed(decoAngle, angularHalfWidth)) {
                consumed.add(decoAngle, angularHalfWidth);
                break;
            }
        }

        PtmMath.fromAl(rayCasted, decoAngle, groundHeight, true);
        rayCasted.add(planetPos);
        w.rayCast(rcc, rayCasted, planetPos);
        float decoDist = rayCasted.dst(planetPos);

        float baseAngle = PtmMath.windowCenter(decoAngle, DECO_PACK_ANGULAR_WIDTH);
        float baseDist = PtmMath.windowCenter(decoDist, DECO_PACK_SZ);
        Vector2 basePos = PtmMath.fromAl(baseAngle, baseDist).add(planetPos);
        Vector2 decoRelPos = new Vector2(rayCasted).sub(basePos);
        PtmMath.rotate(decoRelPos, -baseAngle - 90, true);
        float decoRelAngle = decoAngle - baseAngle;

        TextureAtlas.AtlasRegion decoTex = PtmMath.elemRnd(dc.texs);
        if (dc.allowFlip && PtmMath.test(.5f)) {
            decoTex = game.getTexMan().getFlipped(decoTex);
        }

        RectSprite s = new RectSprite(decoTex, decoSz, dc.orig.x, dc.orig.y, decoRelPos, DraLevel.DECO,
                decoRelAngle, 0, PtmColor.WHITE, false);
        List<Dra> ss = collector.get(basePos);
        if (ss == null) {
            ss = new ArrayList<Dra>();
            collector.put(new Vector2(basePos), ss);
        }
        ss.add(s);
        PtmMath.free(basePos);
    }
}

From source file:com.tnf.ptm.handler.BeaconHandler.java

License:Apache License

private boolean maybeUpdateTargetPos(PtmGame game) {
    updateTarget(game);/*from   w ww.j a  v  a2s  . c  o m*/
    if (myTargetPilot == null) {
        return false;
    }
    Vector2 beaconPos = getPos0();
    if (myTarget != null) {
        PtmMath.toWorld(beaconPos, myTargetRelPos, myTarget.getAngle(), myTarget.getPosition(), false);
        mySpd.set(myTarget.getSpd());
    } else {
        beaconPos.set(myFarTarget.getPos());
    }
    return true;
}

From source file:com.tnf.ptm.handler.input.AiPilot.java

License:Apache License

@Override
public void updateFar(PtmGame game, FarShip farShip) {
    Vector2 shipPos = farShip.getPos();
    HullConfig hullConfig = farShip.getHullConfig();
    float maxIdleDist = getMaxIdleDist(hullConfig);
    myDestProvider.update(game, shipPos, maxIdleDist, hullConfig, null);
    Vector2 dest = myDestProvider.getDest();

    Vector2 spd = farShip.getSpd();
    float angle = farShip.getAngle();
    Engine engine = farShip.getEngine();
    float ts = game.getTimeStep();
    if (dest == null || engine == null) {
        if (myPlanetBind == null) {
            if (myBindAwait > 0) {
                myBindAwait -= ts;//  www.  j  av  a 2s .com
            } else {
                myPlanetBind = PlanetBind.tryBind(game, shipPos, angle);
                myBindAwait = MAX_BIND_AWAIT;
            }
        }
        if (myPlanetBind != null) {
            myPlanetBind.setDiff(spd, shipPos, false);
            spd.scl(1 / ts);
            angle = myPlanetBind.getDesiredAngle();
        }
    } else {
        float toDestLen = shipPos.dst(dest);
        float desiredAngle;
        float maxIdleDistHack = .05f; // to avoid StillGuards from getting stuck inside ground
        if (myDestProvider.shouldStopNearDest() && toDestLen < maxIdleDistHack) {
            spd.set(myDestProvider.getDestSpd());
            desiredAngle = angle; // can be improved
        } else {
            desiredAngle = PtmMath.angle(shipPos, dest);
            if (myDestProvider.shouldAvoidBigObjs()) {
                desiredAngle = myMover.getBigObjAvoider().avoid(game, shipPos, dest, desiredAngle);
            }
            float desiredSpdLen = myDestProvider.getDesiredSpdLen();
            float spdLenDiff = engine.getAcc() * ts;
            float spdLen = PtmMath.approach(spd.len(), desiredSpdLen, spdLenDiff);
            if (toDestLen < spdLen) {
                spdLen = toDestLen;
            }
            PtmMath.fromAl(spd, desiredAngle, spdLen);
        }
        angle = PtmMath.approachAngle(angle, desiredAngle, engine.getMaxRotSpd() * ts);
    }

    farShip.setSpd(spd);
    farShip.setAngle(angle);

    Vector2 newPos = PtmMath.getVec(spd);
    newPos.scl(ts);
    newPos.add(shipPos);
    farShip.setPos(newPos);
    PtmMath.free(newPos);
}

From source file:com.torrosoft.sopistan.SopistanMain.java

License:Open Source License

void drawDebug() {
    Array<Vector2> input = swipe.getInput();

    // draw the raw input
    shapes.begin(ShapeType.Line);
    shapes.setColor(Color.GRAY);//from  www . j  av a 2  s  .  co  m
    for (int i = 0; i < input.size - 1; i++) {
        Vector2 p = input.get(i);
        Vector2 p2 = input.get(i + 1);
        shapes.line(p.x, p.y, p2.x, p2.y);
    }
    shapes.end();

    // draw the smoothed and simplified path
    shapes.begin(ShapeType.Line);
    shapes.setColor(Color.RED);

    Array<Vector2> out = swipe.getPath();
    for (int i = 0; i < out.size - 1; i++) {
        Vector2 p = out.get(i);
        Vector2 p2 = out.get(i + 1);
        shapes.line(p.x, p.y, p2.x, p2.y);
    }

    shapes.end();

    // render our perpendiculars
    shapes.begin(ShapeType.Line);
    Vector2 perp = new Vector2();

    for (int i = 1; i < input.size - 1; i++) {
        Vector2 p = input.get(i);
        Vector2 p2 = input.get(i + 1);

        shapes.setColor(Color.LIGHT_GRAY);
        perp.set(p).sub(p2).nor();
        perp.set(perp.y, -perp.x);
        perp.scl(10f);
        shapes.line(p.x, p.y, p.x + perp.x, p.y + perp.y);
        perp.scl(-1f);
        shapes.setColor(Color.BLUE);
        shapes.line(p.x, p.y, p.x + perp.x, p.y + perp.y);
    }

    shapes.end();
}

From source file:com.vlaaad.dice.game.world.view.visualizers.ChainLightningVisualizer.java

License:Open Source License

@Override
public IFuture<Void> visualize(final ChainLightningResult result) {
    final Future<Void> future = new Future<Void>();
    final WorldObjectView casterView = visualizer.viewController.getView(result.caster);
    //        Logger.log(Config.findRegions("animation/chain-lightning"));
    final AnimationSubView appear = new AnimationSubView(0.05f, Config.findRegions("animation/chain-lightning"),
            Animation.PlayMode.NORMAL);/*  w w w. j  a v  a2s .co m*/
    visualizer.viewController.scroller.centerOn(result.chain.first());
    casterView.addSubView(appear);
    casterView.addListener(new AnimationListener() {
        @Override
        protected void onAnimationEvent(AnimationEvent event) {
            casterView.removeListener(this);
            Array<Vector2> fullChain = new Array<Vector2>(result.chain.size + 1);
            fullChain.add(pointFor(result.caster, 19 / 24f, 23 / 24f));
            for (Creature target : result.chain) {
                fullChain.add(pointFor(target));
            }

            Array<Vector2> lightningPoints = new Array<Vector2>();
            lightningPoints.add(fullChain.first());
            for (int i = 1; i < fullChain.size; i++) {
                Vector2 from = fullChain.get(i - 1);
                Vector2 to = fullChain.get(i);
                int jogs = MathUtils.ceil(from.dst(to) * 1.3f / ViewController.CELL_SIZE);
                //add (from, to]
                if (jogs <= 0) {
                    lightningPoints.add(to);
                    continue;
                }
                Vector2 prev = from;
                for (int j = 1; j <= jogs; j++) {
                    float alpha = (float) j / (jogs + 1f);
                    Vector2 step = new Vector2(from).lerp(to, alpha);
                    float stepLength = tmp.set(prev).dst(step);
                    Vector2 target = tmp.set(to).sub(from).rotate(MathUtils.randomBoolean() ? -90 : 90).nor()
                            .scl(MathUtils.random(2f, stepLength / 2f)).add(step);
                    step.set(target);
                    lightningPoints.add(step);
                    prev = step;
                }
                lightningPoints.add(to);
            }
            for (Creature creature : result.chain) {
                final ParticleActor particles = new ParticleActor(
                        Config.particles.get("ability-chain-lightning-hit").obtain());
                particles.addListener(new ChangeListener() {
                    @Override
                    public void changed(ChangeEvent event, Actor actor) {
                        particles.effect.free();
                        particles.remove();
                    }
                });
                particles.setPosition((creature.getX() + 0.5f) * ViewController.CELL_SIZE,
                        (creature.getY() + 0.5f) * ViewController.CELL_SIZE);
                visualizer.viewController.effectLayer.addActor(particles);

                visualizer.viewController.visualize(new Defence(creature, AttackType.weapon));
            }
            final CountDown countDown = new CountDown(result.killed.size + 1, new Runnable() {
                @Override
                public void run() {
                    future.happen();
                }
            });
            for (Creature creature : result.killed) {
                if (creature == result.caster && result.addedExp.containsKey(result.caster))
                    continue;

                visualizer.viewController.visualize(new Death(result.caster, creature)).addListener(countDown);
            }
            SoundManager.instance.playSound("chain-lightning");
            for (int i = 1; i < lightningPoints.size; i++) {
                Vector2 from = lightningPoints.get(i - 1);
                Vector2 to = lightningPoints.get(i);
                SegmentActor ray = new SegmentActor(tmp.set(to).sub(from), "effect-chain-lightning-segment");
                ray.setPosition(from.x, from.y);
                visualizer.viewController.effectLayer.addActor(ray);
                ray.addAction(
                        Actions.sequence(Actions.delay(0.5f), Actions.alpha(0, 0.5f), Actions.removeActor()));
            }
            visualizer.viewController.effectLayer
                    .addAction(Actions.sequence(Actions.delay(0.5f), Actions.run(new Runnable() {
                        @Override
                        public void run() {
                            casterView.removeSubView(appear);
                            Array<TextureAtlas.AtlasRegion> appearRegions = Config
                                    .findRegions("animation/chain-lightning");
                            Array<TextureAtlas.AtlasRegion> disappearRegions = new Array<TextureAtlas.AtlasRegion>(
                                    appearRegions);
                            disappearRegions.removeIndex(1);
                            disappearRegions.reverse();
                            final AnimationSubView disappear = new AnimationSubView(0.1f, disappearRegions,
                                    Animation.PlayMode.NORMAL);
                            casterView.addSubView(disappear);
                            casterView.addListener(new AnimationListener() {
                                @Override
                                protected void onAnimationEvent(AnimationEvent event) {
                                    casterView.removeSubView(disappear);
                                    casterView.removeListener(this);
                                }
                            });
                        }
                    }), Actions.delay(0.5f), Actions.run(new Runnable() {
                        @Override
                        public void run() {
                            if (result.addedExp.size == 0) {
                                countDown.tick();
                                return;
                            }
                            //exp
                            GiveExpResult[] expResults = new GiveExpResult[result.addedExp.size];
                            int i = 0;
                            for (Creature creature : result.addedExp.keys()) {
                                expResults[i] = new GiveExpResult(creature, result.addedExp.get(creature, 0));
                                i++;
                            }
                            visualizer.visualize(new SequenceResult(expResults))
                                    .addListener(new IFutureListener<Void>() {
                                        @Override
                                        public void onHappened(Void aVoid) {
                                            countDown.tick();
                                            if (result.killed.contains(result.caster, true)
                                                    && result.addedExp.containsKey(result.caster)) {

                                                visualizer.viewController
                                                        .visualize(new Death(result.caster, result.caster))
                                                        .addListener(countDown);
                                            }
                                        }
                                    });
                        }
                    })));

        }
    });

    return future;
}