List of usage examples for com.badlogic.gdx.math Vector2 dst
@Override public float dst(Vector2 v)
From source file:org.destinationsol.game.SolGame.java
License:Apache License
public boolean isPlaceEmpty(Vector2 pos, boolean considerPlanets) { Planet np = myPlanetManager.getNearestPlanet(pos); if (considerPlanets) { boolean inPlanet = np.getPos().dst(pos) < np.getFullHeight(); if (inPlanet) return false; }//from w w w .ja v a 2 s.co m SolSystem ns = myPlanetManager.getNearestSystem(pos); if (ns.getPos().dst(pos) < SunSingleton.SUN_HOT_RAD) return false; List<SolObject> objs = myObjectManager.getObjs(); for (int i = 0, objsSize = objs.size(); i < objsSize; i++) { SolObject o = objs.get(i); if (!o.hasBody()) continue; if (pos.dst(o.getPosition()) < myObjectManager.getRadius(o)) { return false; } } List<FarObjData> farObjs = myObjectManager.getFarObjs(); for (int i = 0, farObjsSize = farObjs.size(); i < farObjsSize; i++) { FarObjData fod = farObjs.get(i); FarObj o = fod.fo; if (!o.hasBody()) continue; if (pos.dst(o.getPos()) < o.getRadius()) { return false; } } return true; }
From source file:org.destinationsol.game.sound.SoundManager.java
License:Apache License
/** * Plays a sound. Either pos or source must not be null. * @param pos position of a sound. If null, source.getPosition() will be used * @param source bearer of a sound. Must not be null for looped sounds * @param volMul multiplier for sound volume *///ww w. j a v a2s . c o m public void play(SolGame game, SolSound sound, @Nullable Vector2 pos, @Nullable SolObject source, float volMul) { if (source == null && pos == null) throw new AssertionError("pass either pos or source"); if (source == null && sound.loopTime > 0) throw new AssertionError("looped sound without source object: " + sound.dir); if (sound == null) return; float globalVolMul = game.getCmp().getOptions().volMul; if (globalVolMul == 0) return; if (pos == null) pos = source.getPosition(); // vol Vector2 camPos = game.getCam().getPos(); float airPerc = 0; Planet np = game.getPlanetMan().getNearestPlanet(); if (np.getConfig().skyConfig != null) { float camToAtmDst = camPos.dst(np.getPos()) - np.getGroundHeight() - Const.ATM_HEIGHT / 2; airPerc = SolMath.clamp(1 - camToAtmDst / (Const.ATM_HEIGHT / 2)); } if (DebugOptions.SOUND_IN_SPACE) airPerc = 1; float maxSoundDist = 1 + 1.5f * Const.CAM_VIEW_DIST_GROUND * airPerc; SolShip hero = game.getHero(); float fullSoundRad = hero == null ? 0 : hero.getHull().config.getApproxRadius(); float dst = pos.dst(camPos) - fullSoundRad; float distMul = SolMath.clamp(1 - dst / maxSoundDist); float vol = sound.baseVolume * volMul * distMul * globalVolMul; if (vol <= 0) return; //pitch float pitch = SolMath.rnd(.97f, 1.03f) * game.getTimeFactor() * sound.basePitch; if (skipLooped(source, sound, game.getTime())) return; if (DebugOptions.SOUND_INFO) { myHintDrawer.add(source, pos, sound.getDebugString()); } if (sound.sounds.isEmpty()) return; Sound sound0 = SolMath.elemRnd(sound.sounds); sound0.play(vol, pitch, 0); }
From source file:us.thirdmillenium.strategicassaultsimulator.agents.ConeAgent.java
License:Apache License
/** * Computes distance and what is seen for each degree of viewing angle. * *///from w ww. j a v a 2s. c o m private double[] computeVision() { double[] item = new double[this.degreesOfView]; double[] distance = new double[this.degreesOfView]; float startDeg = this.rotation + (this.degreesOfView / 2); float degree = 0; // Store the first point for the vision Polygon this.visionPolygonVertices[0] = this.position.x; this.visionPolygonVertices[1] = this.position.y; // Consider every degree angle coming from Agent for (int i = 0; i < this.degreesOfView; i++) { // Set values for this degree and arrays degree = startDeg - i; item[i] = Params.ConeVisEmpty; distance[i] = this.visionDepth; // Variables to hold intermediate calcs Vector2 intersection = new Vector2(); float distToObject; boolean seenAgent = false; // Calculate the direction for the Agent at this angle degree Vector2 direction = new Vector2(0, 1); direction.rotate(degree); // Calculate the furthest point Agent can see for this degree Vector2 endPoint = this.position.cpy(); endPoint.mulAdd(direction, this.visionDepth); // The boundRect is used for Sprite collision calcs //Rectangle agentBoundRect = new Rectangle(0, 0, Params.AgentTileSize, Params.AgentTileSize); //Rectangle bulletBoundRect = new Rectangle(0, 0, 5, 8); // Hard coded!!! Gah!! // Detect Bullets? // Detect Enemy Agents Iterator<AgentModel> enemyItr = this.enemyTracker.iterator(); AgentModel enemy; while (enemyItr.hasNext()) { enemy = enemyItr.next(); // If segment intersects circle if (Intersector.intersectSegmentCircle(this.position, endPoint, enemy.getPosition(), Params.AgentRadiusSquared)) { distToObject = this.position.dst(enemy.getPosition()) - (Params.AgentTileSize / 2.2f); // Check if Agents are within Vision Depth if (distToObject < distance[i]) { item[i] = Params.ConeVisEnemy; distance[i] = distToObject; seenAgent = true; } } } // Detect Friendly Agents Iterator<AgentModel> teamItr = this.teamTracker.iterator(); AgentModel team; while (teamItr.hasNext()) { team = teamItr.next(); // If segment intersects circle if (team != this && Intersector.intersectSegmentCircle(this.position, endPoint, team.getPosition(), Params.AgentRadiusSquared)) { distToObject = this.position.dst(team.getPosition()) - (Params.AgentTileSize / 2); // Check if Agents are within Vision Depth if (distToObject < distance[i]) { item[i] = Params.ConeVisTeam; distance[i] = distToObject; seenAgent = true; } } } // Detect Collision with Walls or Boundary Iterator<Line> lineItr = this.collisionLines.iterator(); Line line; while (lineItr.hasNext()) { line = lineItr.next(); if (Intersector.intersectSegments(this.position, endPoint, line.start, line.end, intersection)) { distToObject = intersection.dst(this.position); if (distToObject < distance[i]) { item[i] = Params.ConeVisWall; distance[i] = distToObject; seenAgent = false; // if true, then Agent on other side of wall } } } // Detect Path ONLY when an Agent hasn't been seen on this degree line. if (!seenAgent) { TileNode node; for (int index = this.preferredPathIndex; index < this.preferredPath.getCount(); index++) { node = this.preferredPath.get(index); // Place radius 5 circle over preferred path node if (Intersector.intersectSegmentCircle(this.position, endPoint, node.getPixelVector2(), 25)) { distToObject = this.position.dst(node.getPixelVector2()); // Check if Agents are within Vision Depth if (distToObject < distance[i]) { item[i] = Params.ConeVisPath; distance[i] = distToObject; } } } } // Store the x,y point for this degree line this.visionPolygonVertices[2 + (i * 2)] = this.position.x + (direction.x * (float) distance[i]); this.visionPolygonVertices[3 + (i * 2)] = this.position.y + (direction.y * (float) distance[i]); // Normalize distance to (agent edge -> 1) if (distance[i] < Params.AgentCircleRadius) { distance[i] = Params.AgentCircleRadius; } distance[i] = distance[i] / (float) this.visionDepth; } return GraphicsHelpers.interleaveDoubleArrays(item, distance); }
From source file:us.thirdmillenium.strategicassaultsimulator.agents.ConePuppetAgent.java
License:Apache License
/** * Computes distance and what is seen for each degree of viewing angle. * *//*from www . j av a2 s .c o m*/ //private double[] computeVision() { private void computeVision() { double[] item = new double[this.degreesOfView]; double[] distance = new double[this.degreesOfView]; float startDeg = this.rotation + (this.degreesOfView / 2); float degree = 0; boolean seenEnemyAgent = false; float degreeSeenEnemyAgent = -1; // Store the first point for the vision Polygon this.visionPolygonVertices[0] = this.position.x; this.visionPolygonVertices[1] = this.position.y; // Consider every degree angle coming from Agent for (int i = 0; i < this.degreesOfView; i++) { // Set values for this degree and arrays degree = startDeg - i; item[i] = Params.ConeVisEmpty; distance[i] = this.visionDepth; // Variables to hold intermediate calcs Vector2 intersection = new Vector2(); float distToObject; boolean seenAgent = false; // Calculate the direction for the Agent at this angle degree Vector2 direction = new Vector2(0, 1); direction.rotate(degree); // Calculate the furthest point Agent can see for this degree Vector2 endPoint = this.position.cpy(); endPoint.mulAdd(direction, this.visionDepth); // The boundRect is used for Sprite collision calcs //Rectangle agentBoundRect = new Rectangle(0, 0, Params.AgentTileSize, Params.AgentTileSize); //Rectangle bulletBoundRect = new Rectangle(0, 0, 5, 8); // Hard coded!!! Gah!! // Detect Bullets? // Detect Enemy Agents Iterator<AgentModel> enemyItr = this.enemyTracker.iterator(); AgentModel enemy; while (enemyItr.hasNext()) { enemy = enemyItr.next(); // If segment intersects circle if (Intersector.intersectSegmentCircle(this.position, endPoint, enemy.getPosition(), Params.AgentRadiusSquared)) { distToObject = this.position.dst(enemy.getPosition()) - (Params.AgentTileSize / 2.2f); // Check if Agents are within Vision Depth if (distToObject < distance[i]) { item[i] = Params.ConeVisEnemy; distance[i] = distToObject; seenAgent = true; seenEnemyAgent = true; degreeSeenEnemyAgent = degree; } } } // Detect Friendly Agents Iterator<AgentModel> teamItr = this.teamTracker.iterator(); AgentModel team; while (teamItr.hasNext()) { team = teamItr.next(); // If segment intersects circle if (team != this && Intersector.intersectSegmentCircle(this.position, endPoint, team.getPosition(), Params.AgentRadiusSquared)) { distToObject = this.position.dst(team.getPosition()) - (Params.AgentTileSize / 2); // Check if Agents are within Vision Depth if (distToObject < distance[i]) { item[i] = Params.ConeVisTeam; distance[i] = distToObject; seenAgent = true; } } } // Detect Collision with Walls or Boundary Iterator<Line> lineItr = this.collisionLines.iterator(); Line line; while (lineItr.hasNext()) { line = lineItr.next(); if (Intersector.intersectSegments(this.position, endPoint, line.start, line.end, intersection)) { distToObject = intersection.dst(this.position); //float tempAngle = this.position.angle(intersection); if (distToObject < distance[i]) { item[i] = Params.ConeVisWall; distance[i] = distToObject; seenAgent = false; // if true, then Agent on other side of wall } } } // Detect Path ONLY when an Agent hasn't been seen on this degree line. if (!seenAgent) { TileNode node; for (int index = this.preferredPathIndex; index < this.preferredPath.getCount(); index++) { node = this.preferredPath.get(index); // Place radius 5 circle over preferred path node if (Intersector.intersectSegmentCircle(this.position, endPoint, node.getPixelVector2(), 25)) { distToObject = this.position.dst(node.getPixelVector2()); // Check if Agents are within Vision Depth if (distToObject < distance[i]) { item[i] = Params.ConeVisPath; distance[i] = distToObject; } } } } // Store the x,y point for this degree line this.visionPolygonVertices[2 + (i * 2)] = this.position.x + (direction.x * (float) distance[i]); this.visionPolygonVertices[3 + (i * 2)] = this.position.y + (direction.y * (float) distance[i]); // Normalize distance to (agent edge -> 1) if (distance[i] < Params.AgentCircleRadius) { distance[i] = Params.AgentCircleRadius; } distance[i] = distance[i] / (float) this.visionDepth; } //return GraphicsHelpers.interleaveDoubleArrays(item, distance); this.input = GraphicsHelpers.interleaveDoubleArrays(item, distance); this.output = new double[3]; // Check Danger Information if ((this.dangerCoolDown--) < 1) { this.dangerOverride = false; } // Update Position if (this.dangerOverride && !seenEnemyAgent) { // Being shot and don't see enemy Agent this.output[0] = 0.5; // no rotate this.output[1] = 1; // Move fast as hell this.output[2] = 0; // Don't shoot } else { puppetCrunch(seenEnemyAgent, degreeSeenEnemyAgent, distance, item); } }
From source file:us.thirdmillenium.strategicassaultsimulator.ai.tile.TileHeuristic.java
License:Apache License
@Override public float estimate(TileNode node, TileNode endNode) { // Straight Line Vector2 start = new Vector2(node.getCellX(), node.getCellY()); Vector2 end = new Vector2(endNode.getCellX(), endNode.getCellY()); return start.dst(end); }