private void StateCheck() { if (VectorMath.DistanceBetweenPositions(hobgoblin.Pos, hobgoblin.Target.Pos) >= hobgoblin.CommandRadius || !VectorMath.LineOfSight(hobgoblin.world, hobgoblin.Pos, hobgoblin.Target.Pos)) { hobgoblin.setState(hobgoblin.hunting); } }
private void StateCheck() { if (VectorMath.DistanceBetweenPositions(goblin.Pos, goblin.Target.Pos) < goblin.PassiveDistance && VectorMath.LineOfSight(goblin.world, goblin.Pos, goblin.Target.Pos)) { goblin.setState(goblin.hunting); } }
public Vector2D GetRendezvousPoint() { Vector2D currentPosition = new Vector2D(Pos.X, Pos.Y); Vector2D goalPosition = new Vector2D(Target.Pos.X, Target.Pos.Y); double segmentDistance = 15; Vector2D step = (goalPosition - Pos).Normalize() * segmentDistance; int segmentCount = 0; while (VectorMath.DistanceBetweenPositions(currentPosition, goalPosition) > segmentDistance) { currentPosition += step; segmentCount++; int defenseSegment = (int)VectorMath.DistanceBetweenPositions(Pos, goalPosition) / 25; if (defenseSegment > 8) { defenseSegment = 8; } if (segmentCount == defenseSegment && defenseSegment > 1) { return(currentPosition); } } return(currentPosition); }
private void StateCheck() { // CommandRadius should be PassiveDistance. if (VectorMath.DistanceBetweenPositions(hobgoblin.Pos, hobgoblin.Target.Pos) < hobgoblin.CommandRadius && VectorMath.LineOfSight(hobgoblin.world, hobgoblin.Pos, hobgoblin.Target.Pos)) { hobgoblin.setState(hobgoblin.command); } }
private void StateCheck() { if (VectorMath.DistanceBetweenPositions(hobgoblin.Pos, hobgoblin.Target.Pos) < hobgoblin.PassiveDistance && VectorMath.LineOfSight(hobgoblin.world, hobgoblin.Pos, hobgoblin.Target.Pos)) { hobgoblin.setState(hobgoblin.hunting); } else if (VectorMath.DistanceBetweenPositions(hobgoblin.Pos, hobgoblin.Target.Pos) >= hobgoblin.PassiveDistance && VectorMath.LineOfSight(hobgoblin.world, hobgoblin.Pos, hobgoblin.Target.Pos)) { hobgoblin.setState(hobgoblin.command); } }
public static Vector2D Flee(Vector2D targetPos, IMover me, double panicDistance) { var distance = me.Pos - targetPos; if (VectorMath.DistanceBetweenPositions(me.Pos, targetPos) > panicDistance || distance.LenghtIsZero()) { return(new Vector2D(0, 0)); } var desiredVelocity = distance.Normalize() * me.MaxSpeed; var neededForce = desiredVelocity - me.Velocity; return(neededForce.Truncate(me.MaxForce)); }
// Calls for goblins within range to start obeying. public void CallOut() { int debugCount = 0; foreach (Goblin goblin in hobgoblin.world.getGoblins()) { if (VectorMath.DistanceBetweenPositions(hobgoblin.Pos, goblin.Pos) < hobgoblin.CommandRadius) { debugCount++; hobgoblin.AddDebugText("I have [" + debugCount + "] minion(s).", 2); goblin.Obey(hobgoblin); } } }
//public Vector2D Calculate() //{ // Vector2D force; // My force will be stored here // Vector2D pos = _me.Pos; // Position of the agent // // For each wall // for (int j = 0; j < _me.Walls.Count(); j++) // { // var wall = _me.Walls[j]; // var x = (wall.Center.X + _me.Pos.X) / 2; // var y = (wall.Center.Y + _me.Pos.Y) / 2; // Vector2D distance = new Vector2D(x, y); // // If the wall is visible, calculate the force to apply // double dotProduct = distance * partsList[j]->normal(); // if (dotProduct < 0) // { // force += partsList[j]->normal() / (distance.length() * distance.length() + 1); // } // } // // Returned the calculated force // return force; //} public IWall GetClosestWall(IWallAvoider ME) { IWall mostThreatening = null; for (int i = 0; i < ME.Walls.Count(); i++) { IWall wall = ME.Walls[i]; bool collision = findSensorCollision(wall); if (collision && (mostThreatening == null || VectorMath.DistanceBetweenPositions(ME.Pos, wall.Center) < VectorMath.DistanceBetweenPositions(ME.Pos, mostThreatening.Center))) { mostThreatening = wall; } } return(mostThreatening); }
public void Act(float timeElapsed) { timeElapsedSinceLastAttack += timeElapsed; if (VectorMath.DistanceBetweenPositions(goblin.Pos, goblin.world.Hero.Pos) >= goblin.AttackRange) { Vector2D steeringForce = new Vector2D(0, 0); if (goblin._SB != null) { steeringForce += goblin._SB.Calculate() * 4; } if (goblin._FlockB != null) { steeringForce += goblin._FlockB.Calculate() * 0.5; } if (goblin._OA != null) { steeringForce += goblin._OA.Calculate(); } if (goblin._WA != null) { steeringForce += goblin._WA.Calculate(); } steeringForce.Truncate(goblin.MaxForce); Vector2D acceleration = steeringForce / goblin.Mass; goblin.Velocity += (acceleration * timeElapsed); goblin.Velocity = goblin.Velocity.Truncate(goblin.MaxSpeed); goblin.OldPos = goblin.Pos; goblin.Pos += (goblin.Velocity * timeElapsed); if (goblin.Velocity.LengthSquared() > 0.00000001) { goblin.Heading = goblin.Velocity.Normalize(); goblin.Side = goblin.Heading.Perp(); } goblin.WrapAround(); goblin.world.rePosGoblin(goblin.Key, goblin.OldPos, goblin.Pos); } else if (AttackAvailable()) { //AttackPlayer(); } // Checks wether a state change is in order. StateCheck(); }
public Hobgoblin GetClosestHobgoblin() { Hobgoblin closestHobgoblin = null; double closestDistance = int.MaxValue; foreach (Hobgoblin hobgoblin in world.getHobgoblins()) { double distance = VectorMath.DistanceBetweenPositions(Pos, hobgoblin.Pos); if (distance < closestDistance) { closestHobgoblin = hobgoblin; closestDistance = distance; } } return(closestHobgoblin); }
private IObstacle findMostThreateningObstacle(IObstacleAvoider me) { IObstacle mostThreatening = null; for (int i = 0; i < me.Obstacles.Count(); i++) { IObstacle obstacle = me.Obstacles[i]; bool collision = lineIntersectsCircleAhead(obstacle); if (collision && (mostThreatening == null || VectorMath.DistanceBetweenPositions(me.Pos, obstacle.Center) < VectorMath.DistanceBetweenPositions(me.Pos, mostThreatening.Center))) { mostThreatening = obstacle; } } return(mostThreatening); }
public override void Render(Graphics g) { double leftCorner = Pos.X - Scale; double rightCorner = Pos.Y - Scale; double size = Scale * 2; Pen p = new Pen(VColor, 2); Pen r = new Pen(Color.Red, 2); Pen n = new Pen(Color.Green, 2); if (world.TriangleModeActive) { // Draws triangle. // Left lat g.DrawLine(p, (int)(Pos.X + (Side.X - Heading.X) * (size / 2)), (int)(Pos.Y + (Side.Y - Heading.Y) * (size / 2)), (int)(Pos.X) + (int)(Heading.X * (size / 2)), (int)Pos.Y + (int)(Heading.Y * (size / 2))); // Right lat g.DrawLine(p, (int)(Pos.X + ((Side.X * -1) - Heading.X) * (size / 2)), (int)(Pos.Y + ((Side.Y * -1) - Heading.Y) * (size / 2)), (int)Pos.X + (int)(Heading.X * (size / 2)), (int)Pos.Y + (int)(Heading.Y * (size / 2))); // Bottom lat g.DrawLine(p, (int)(Pos.X + ((Side.X * -1) - Heading.X) * (size / 2)), (int)(Pos.Y + ((Side.Y * -1) - Heading.Y) * (size / 2)), (int)(Pos.X + (Side.X - Heading.X) * (size / 2)), (int)(Pos.Y + (Side.Y - Heading.Y) * (size / 2))); } else { // Draws circle. g.DrawEllipse(p, new Rectangle((int)leftCorner, (int)rightCorner, (int)size, (int)size)); } if (world.VelocityVisible) { g.DrawLine(r, (int)Pos.X, (int)Pos.Y, (int)Pos.X + (int)(Velocity.X * 2), (int)Pos.Y + (int)(Velocity.Y * 2)); } if (world.DebugMode) { Brush brush = new SolidBrush(Color.Black); g.DrawString(DebugText, SystemFonts.DefaultFont, brush, (float)(Pos.X + size), (float)(Pos.Y - size / 2), new StringFormat()); // RendezvousPoint. Vector2D debugRendezvousPoint = GetRendezvousPoint(); g.DrawEllipse(n, new Rectangle((int)debugRendezvousPoint.X - 15, (int)debugRendezvousPoint.Y - 15, 30, 30)); // Command circle. g.DrawEllipse(r, new Rectangle((int)leftCorner - CommandRadius + (int)(size / 2), (int)rightCorner - CommandRadius + (int)(size / 2), CommandRadius * 2, CommandRadius * 2)); Vector2D currentPosition = new Vector2D(Pos.X, Pos.Y); Vector2D goalPosition = new Vector2D(world.Hero.Pos.X, world.Hero.Pos.Y); double segmentDistance = 15; var toTarget = goalPosition - currentPosition; Vector2D step = (goalPosition - Pos).Normalize() * segmentDistance; bool lineOfSightBlocked = false; while (VectorMath.DistanceBetweenPositions(currentPosition, goalPosition) > segmentDistance) { currentPosition += step; foreach (IObstacle obstacle in world.getObstacles()) { if (VectorMath.DistanceBetweenPositions(currentPosition, obstacle.Center) <= obstacle.Radius) { lineOfSightBlocked = true; break; } } foreach (IWall wall in world.getWalls()) { if (VectorMath.PointInWall(currentPosition, wall)) { lineOfSightBlocked = true; break; } } if (!lineOfSightBlocked) { g.DrawEllipse(r, new Rectangle((int)currentPosition.X, (int)currentPosition.Y, 1, 1)); } else { break; } } if (lineOfSightBlocked) { AddDebugText("No line of sight.", 2); } else { AddDebugText("Line of sight!", 2); } } }
public override void Render(Graphics g) { double leftCorner = Pos.X - Scale; double rightCorner = Pos.Y - Scale; double size = Scale * 2; Pen p = new Pen(VColor, 2); Pen r = new Pen(Color.Red, 2); if (world.TriangleModeActive) { // Draws triangle. // Left lat g.DrawLine(p, (int)(Pos.X + (Side.X - Heading.X) * (size / 2)), (int)(Pos.Y + (Side.Y - Heading.Y) * (size / 2)), (int)(Pos.X) + (int)(Heading.X * (size / 2)), (int)Pos.Y + (int)(Heading.Y * (size / 2))); // Right lat g.DrawLine(p, (int)(Pos.X + ((Side.X * -1) - Heading.X) * (size / 2)), (int)(Pos.Y + ((Side.Y * -1) - Heading.Y) * (size / 2)), (int)Pos.X + (int)(Heading.X * (size / 2)), (int)Pos.Y + (int)(Heading.Y * (size / 2))); // Bottom lat g.DrawLine(p, (int)(Pos.X + ((Side.X * -1) - Heading.X) * (size / 2)), (int)(Pos.Y + ((Side.Y * -1) - Heading.Y) * (size / 2)), (int)(Pos.X + (Side.X - Heading.X) * (size / 2)), (int)(Pos.Y + (Side.Y - Heading.Y) * (size / 2))); } else { // Draws circle. g.DrawEllipse(p, new Rectangle((int)leftCorner, (int)rightCorner, (int)size, (int)size)); } if (world.VelocityVisible) { // Wall avoidance lines. double MAX_SEE_AHEAD = 15; Vector2D center = Pos + Heading * MAX_SEE_AHEAD; Vector2D leftSensor = new Vector2D(Pos.X + ((Side.X - Heading.X) * -MAX_SEE_AHEAD / 2), Pos.Y + ((Side.Y - Heading.Y) * -MAX_SEE_AHEAD / 2)); Vector2D rightSensor = new Vector2D(Pos.X + ((Side.X - Heading.X * -1) * MAX_SEE_AHEAD / 2), Pos.Y + ((Side.Y - Heading.Y * -1) * MAX_SEE_AHEAD / 2)); g.DrawLine(r, (int)Pos.X, (int)Pos.Y, (int)center.X, (int)center.Y); g.DrawLine(r, (int)Pos.X, (int)Pos.Y, (int)leftSensor.X, (int)leftSensor.Y); g.DrawLine(r, (int)Pos.X, (int)Pos.Y, (int)rightSensor.X, (int)rightSensor.Y); // Velocity g.DrawLine(r, (int)Pos.X, (int)Pos.Y, (int)Pos.X + (int)(Velocity.X * 2), (int)Pos.Y + (int)(Velocity.Y * 2)); } if (world.DebugMode) { Brush brush = new SolidBrush(Color.Black); g.DrawString(DebugText, SystemFonts.DefaultFont, brush, (float)(Pos.X + size), (float)(Pos.Y - size / 2), new StringFormat()); Vector2D currentPosition = new Vector2D(Pos.X, Pos.Y); Vector2D goalPosition = new Vector2D(world.Hero.Pos.X, world.Hero.Pos.Y); double segmentDistance = 15; var toTarget = goalPosition - currentPosition; Vector2D step = (goalPosition - Pos).Normalize() * segmentDistance; bool lineOfSightBlocked = false; while (VectorMath.DistanceBetweenPositions(currentPosition, goalPosition) > segmentDistance) { currentPosition += step; foreach (IObstacle obstacle in world.getObstacles()) { #region Localspace try //if (obstacle.Name.Equals("obstacle4")) //{ // var angle = VectorMath.AngleBetweenPositions(Pos, obstacle.Center); // Console.WriteLine("Angle: " + angle); // double relativeX = obstacle.Center.X - Pos.X; // double relativeY = obstacle.Center.Y - Pos.Y; // double rotatedX = Math.Cos(-angle) * relativeX - Math.Sin(-angle) * relativeY; // double rotatedY = Math.Cos(-angle) * relativeY + Math.Sin(-angle) * relativeX; // Console.WriteLine("Obstacle location in local space: " + new Vector2D(rotatedX, rotatedY)); //} #endregion if (VectorMath.DistanceBetweenPositions(currentPosition, obstacle.Center) <= obstacle.Radius) { lineOfSightBlocked = true; break; } } foreach (IWall wall in world.getWalls()) { if (VectorMath.PointInWall(currentPosition, wall)) { lineOfSightBlocked = true; break; } } if (!lineOfSightBlocked) { g.DrawEllipse(r, new Rectangle((int)currentPosition.X, (int)currentPosition.Y, 1, 1)); } else { break; } } if (lineOfSightBlocked) { AddDebugText("No line of sight.", 2); } else { AddDebugText("Line of sight!", 2); } } }
public bool CollisionFound(Vector2D pos) { return(VectorMath.DistanceBetweenPositions(pos, Center) <= Radius); }
private bool lineIntersectsCircleAhead(IObstacle obstacle) { return(VectorMath.DistanceBetweenPositions(obstacle.Center, _ahead) <= obstacle.Radius || VectorMath.DistanceBetweenPositions(obstacle.Center, _ahead2) <= obstacle.Radius); }