List of usage examples for com.badlogic.gdx.math Vector2 mulAdd
@Override
public Vector2 mulAdd(Vector2 vec, Vector2 mulVec)
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 a2 s . 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 w ww . ja v a 2 s . co 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); } }