List of usage examples for com.badlogic.gdx.math Vector2 len
@Override public float len()
From source file:org.destinationsol.game.planet.Planet.java
License:Apache License
public void calcSpdAtPos(Vector2 spd, Vector2 pos) { Vector2 toPos = SolMath.distVec(myPos, pos); float fromPlanetAngle = SolMath.angle(toPos); float hSpdLen = SolMath.angleToArc(myRotSpd, toPos.len()); SolMath.free(toPos);//from ww w. ja va 2s .c o m SolMath.fromAl(spd, fromPlanetAngle + 90, hSpdLen); spd.add(mySpd); }
From source file:org.destinationsol.game.planet.PlanetManager.java
License:Apache License
private void applyGrav(SolGame game, SolSystem nearestSys) { float npGh = myNearestPlanet.getGroundHeight(); float npFh = myNearestPlanet.getFullHeight(); float npMinH = myNearestPlanet.getMinGroundHeight(); Vector2 npPos = myNearestPlanet.getPos(); Vector2 sysPos = nearestSys.getPos(); float npGravConst = myNearestPlanet.getGravConst(); List<SolObject> objs = game.getObjMan().getObjs(); for (int i = 0, objsSize = objs.size(); i < objsSize; i++) { SolObject obj = objs.get(i);/*from w ww .j a v a 2s . c o m*/ if (!obj.receivesGravity()) continue; Vector2 objPos = obj.getPosition(); float minDist; Vector2 srcPos; float gravConst; boolean onPlanet; float toNp = npPos.dst(objPos); float toSys = sysPos.dst(objPos); if (toNp < npFh) { if (recoverObj(obj, toNp, npMinH)) continue; minDist = npGh; srcPos = npPos; gravConst = npGravConst; onPlanet = true; } else if (toSys < Const.SUN_RADIUS) { minDist = SunSingleton.SUN_HOT_RAD; srcPos = sysPos; gravConst = SunSingleton.GRAV_CONST; onPlanet = false; } else { continue; } Vector2 grav = SolMath.getVec(srcPos); grav.sub(objPos); float len = grav.len(); grav.nor(); if (len < minDist) { len = minDist; } float g = gravConst / len / len; grav.scl(g); obj.receiveForce(grav, game, true); SolMath.free(grav); if (!onPlanet) { mySunSingleton.doDmg(game, obj, toSys); } } }
From source file:org.destinationsol.game.planet.PlanetManager.java
License:Apache License
private boolean recoverObj(SolObject obj, float toNp, float npMinH) { if (npMinH < toNp) return false; if (!(obj instanceof SolShip)) return false; SolShip ship = (SolShip) obj;// w w w .j ava 2s .co m Hull hull = ship.getHull(); if (hull.config.getType() == HullConfig.Type.STATION) return false; float fh = myNearestPlanet.getFullHeight(); Vector2 npPos = myNearestPlanet.getPos(); Vector2 toShip = SolMath.distVec(npPos, ship.getPosition()); float len = toShip.len(); if (len == 0) { toShip.set(0, fh); } else { toShip.scl(fh / len); } toShip.add(npPos); Body body = hull.getBody(); body.setTransform(toShip, 0); body.setLinearVelocity(Vector2.Zero); SolMath.free(toShip); return true; }
From source file:org.destinationsol.game.planet.PlanetObjectsBuilder.java
License:Apache License
public FarShip buildGroundShip(SolGame game, Planet planet, ShipConfig ge, TradeConfig tc, Faction faction, ConsumedAngles takenAngles, String mapHint) { Vector2 pos = game.getPlanetMan().findFlatPlace(game, planet, takenAngles, ge.hull.getApproxRadius()); boolean station = ge.hull.getType() == HullConfig.Type.STATION; String ic = ge.items;/*from ww w . jav a 2 s.c o m*/ boolean hasRepairer; hasRepairer = faction == Faction.LAANI; int money = ge.money; float height = pos.len(); float aboveGround; if (station) { aboveGround = ge.hull.getSize() * .75f - ge.hull.getOrigin().y; } else { aboveGround = ge.hull.getSize(); } pos.scl((height + aboveGround) / height); SolMath.toWorld(pos, pos, planet.getAngle(), planet.getPos(), false); Vector2 toPlanet = SolMath.getVec(planet.getPos()).sub(pos); float angle = SolMath.angle(toPlanet) - 180; if (station) angle += 90; Vector2 spd = new Vector2(toPlanet).nor(); SolMath.free(toPlanet); Pilot provider = new AiPilot(new StillGuard(pos, game, ge), false, faction, true, mapHint, Const.AI_DET_DIST); return game.getShipBuilder().buildNewFar(game, pos, spd, angle, 0, provider, ic, ge.hull, null, hasRepairer, money, tc, true); }
From source file:org.destinationsol.game.planet.SunSingleton.java
License:Apache License
public void draw(SolGame game, GameDrawer drawer) { Vector2 camPos = game.getCam().getPos(); SolSystem sys = game.getPlanetMan().getNearestSystem(camPos); Vector2 toCam = SolMath.getVec(camPos); toCam.sub(sys.getPos());/*from w w w. jav a 2s .c o m*/ float toCamLen = toCam.len(); if (toCamLen < Const.SUN_RADIUS) { float closeness = 1 - toCamLen / Const.SUN_RADIUS; myGradTint.a = SolMath.clamp(closeness * 4, 0, 1); myFillTint.a = SolMath.clamp((closeness - .25f) * 4, 0, 1); float sz = 2 * game.getCam().getViewDist(); float gradAngle = SolMath.angle(toCam) + 90; drawer.draw(myWhiteTex, sz * 2, sz * 2, sz, sz, camPos.x, camPos.y, 0, myFillTint); drawer.draw(myGradTex, sz * 2, sz * 2, sz, sz, camPos.x, camPos.y, gradAngle, myGradTint); } SolMath.free(toCam); }
From source file:org.destinationsol.game.screens.CollisionWarnDrawer.java
License:Apache License
public boolean shouldWarn(SolGame game) { myHero = game.getHero();/*from ww w . j a va 2s .c om*/ if (myHero == null) return false; Vector2 pos = myHero.getPosition(); Vector2 spd = myHero.getSpd(); float acc = myHero.getAcc(); float spdLen = spd.len(); float spdAngle = SolMath.angle(spd); if (acc <= 0 || spdLen < 2 * acc) return false; // t = v/a; // s = att/2 = vv/a/2; float breakWay = spdLen * spdLen / acc / 2; breakWay += 2 * spdLen; Vector2 finalPos = SolMath.getVec(0, 0); SolMath.fromAl(finalPos, spdAngle, breakWay); finalPos.add(pos); myWarnCallback.show = false; game.getObjMan().getWorld().rayCast(myWarnCallback, pos, finalPos); SolMath.free(finalPos); return myWarnCallback.show; }
From source file:org.destinationsol.game.ship.ForceBeacon.java
License:Apache License
public static SolShip pullShips(SolGame game, SolObject owner, Vector2 ownPos, Vector2 ownSpd, Faction faction, float maxPullDist) { SolShip res = null;/*from w w w . ja v a2 s .c o m*/ float minLen = Float.MAX_VALUE; List<SolObject> objs = game.getObjMan().getObjs(); for (int i = 0, objsSize = objs.size(); i < objsSize; i++) { SolObject o = objs.get(i); if (o == owner) continue; if (!(o instanceof SolShip)) continue; SolShip ship = (SolShip) o; Pilot pilot = ship.getPilot(); if (pilot.isUp() || pilot.isLeft() || pilot.isRight()) continue; if (game.getFactionMan().areEnemies(faction, pilot.getFaction())) continue; Vector2 toMe = SolMath.distVec(ship.getPosition(), ownPos); float toMeLen = toMe.len(); if (toMeLen < maxPullDist) { if (toMeLen > 1) toMe.scl(1 / toMeLen); if (ownSpd != null) toMe.add(ownSpd); ship.getHull().getBody().setLinearVelocity(toMe); game.getSoundMan().play(game, game.getSpecialSounds().forceBeaconWork, null, ship); if (toMeLen < minLen) { res = ship; minLen = toMeLen; } } SolMath.free(toMe); } return res; }
From source file:org.matheusdev.ror.controller.component.ComponentMovement.java
License:Open Source License
@Override public void apply(Entity entity) { Body body = entity.getBody();//from w w w.j a va2s . c om moving = false; // If trying to move (pressing buttons on Keyboard, steering with Gamepad) if (xsteer != 0f || ysteer != 0f) { moving = true; if (Math.abs(xsteer) > Math.abs(ysteer)) { if (xsteer < 0) { direction = Dir.LEFT; } else { direction = Dir.RIGHT; } } else { if (ysteer < 0) { direction = Dir.DOWN; } else { direction = Dir.UP; } } } Vector2 linVel = body.getLinearVelocity(); if (linVel.len() > maxspeed) { body.setLinearVelocity(linVel.cpy().nor().scl(maxspeed)); } body.applyForceToCenter(strength * xsteer, strength * ysteer); if (friction > 1f && !moving) { body.setLinearVelocity(linVel.div(friction)); } }
From source file:org.matheusdev.ror.controller.component.ComponentNetwork.java
License:Open Source License
@Override public void apply(Entity entity) { Vector2 entityPos = entity.getPos(); Vector2 posDiff = posDiffPool.set(remoteState.posX - entityPos.x, remoteState.posY - entityPos.y); float distance = posDiff.len(); if (distance > 0.5f) { entity.getBody().setTransform(remoteState.posX, remoteState.posY, remoteState.angle); System.out.println("Set position to: " + remoteState.posX + ", " + remoteState.posY); } else if (distance > 0.1f) { entity.getBody().setTransform(entityPos.add(posDiff.scl(0.1f)), remoteState.angle); }//from w ww . j a va2s .com entity.getBody().setLinearVelocity(remoteState.velX, remoteState.velY); }
From source file:org.matheusdev.ror.view.ViewWalking.java
License:Open Source License
@Override public void draw(SpriteBatch batch, Entity e, float delta) { Vector2 linVel = e.getBody().getLinearVelocity(); if (linVel.len() > 0.3f) direction = Dir.getDir(linVel.x, linVel.y); moving = linVel.len() > 0.1f;/*w ww . j a va 2 s .com*/ anims.setDirection(direction); anims.setMoving(moving); anims.setDeltaSpeed(delta); anims.apply(e); sprite.setRegion(anims.getKeyframe()); draw(batch, e, sprite, width, xoffset, yoffset); }