/// <summary> /// Desire to move toward members of a group represented as a vector /// </summary> public static Vector3 CohesionForce(IAgent agent, IAgent otherAgent, SteerParameters steerParams) { Vector3 cohesion = agent.Position == otherAgent.Position ? Vector3.Zero : Vector3.Normalize(otherAgent.Position - agent.Position); return(cohesion * steerParams.CohesionWeight); }
/// <summary> /// Desire to maintain distance from other agents represented as a vector /// </summary> public static Vector3 SeparationForce(IAgent agent, IAgent otherAgent, SteerParameters steerParams, float agentSeparation) { Vector3 separation = (agent.Position - otherAgent.Position) / agentSeparation; float magnitude = 1 - agentSeparation / steerParams.SeparationRange; return(separation * magnitude * steerParams.SeparationWeight); }
/// <summary> /// Desire to randomly adjust current direction /// </summary> public static Vector3 WanderForce(IAgent agent, ref SteerParameters steerParams) { Vector3 wanderFocus = agent.Direction * 1.4f; // the current angle of the wander should be offset slightly steerParams.WanderCurrentAngle += (float)(rand.NextDouble() - 0.5f) * 2 * steerParams.TurningWeight; Vector3 offset = new Vector3((float)Math.Cos(steerParams.WanderCurrentAngle), 0, (float)Math.Sin(steerParams.WanderCurrentAngle)); return Vector3.Normalize(wanderFocus + offset) * steerParams.WanderWeight; }
/// <summary> /// Desire to maintain distance from other agents represented as a vector /// </summary> public static Vector3 SeparationForce(IAgent agent, IAgent otherAgent, SteerParameters steerParams) { return SeparationForce(agent, otherAgent, steerParams, Vector3.Distance(agent.Position, otherAgent.Position)); }
/// <summary> /// Keeps agents inside the world and above the surface of the terrain /// </summary> public static Vector3 WorldBoundsForce(IAgent agent, SteerParameters steerParams) { Vector3 force = Vector3.Zero; float minY = steerParams.World.Terrain.CalculateHeight(agent.Position.X, agent.Position.Z) + 120; float maxY = minY * 4; float minX = 0; float maxX = steerParams.World.Terrain.Width; float minZ = 0; float maxZ = steerParams.World.Terrain.Length; if (agent.Position.X > maxX) force += Vector3.UnitX * (maxX - agent.Position.X) / steerParams.SeparationRange; else if (agent.Position.X < minX) force += Vector3.UnitX * (minX - agent.Position.X) / steerParams.SeparationRange; if (agent.Position.Y > maxY) force += Vector3.UnitY * (maxY - agent.Position.Y) / steerParams.SeparationRange; else if (agent.Position.Y < minY) force += Vector3.UnitY * (minY - agent.Position.Y) / steerParams.SeparationRange; if (agent.Position.Z > maxZ) force += Vector3.UnitZ * (maxZ - agent.Position.Z) / steerParams.SeparationRange; else if (agent.Position.Z < minZ) force += Vector3.UnitZ * (minZ - agent.Position.Z) / steerParams.SeparationRange; return force; }
/// <summary> /// Desire to move toward a specific point /// </summary> public static Vector3 GoalForce(IAgent agent, SteerParameters steerParams) { return (steerParams.GoalPosition - agent.Position) * steerParams.GoalWeight; }
/// <summary> /// Desire to align direction with another agent /// </summary> public static Vector3 AlignmentForce(IAgent otherAgent, SteerParameters steerParams) { return otherAgent.Direction * steerParams.AlignmentWeight; }
/// <summary> /// Desire to move toward members of a group represented as a vector /// </summary> public static Vector3 CohesionForce(IAgent agent, IAgent otherAgent, SteerParameters steerParams) { Vector3 cohesion = agent.Position == otherAgent.Position ? Vector3.Zero : Vector3.Normalize(otherAgent.Position - agent.Position); return cohesion * steerParams.CohesionWeight; }
/// <summary> /// Desire to maintain distance from other agents represented as a vector /// </summary> public static Vector3 SeparationForce(IAgent agent, IAgent otherAgent, SteerParameters steerParams, float agentSeparation) { Vector3 separation = (agent.Position - otherAgent.Position) / agentSeparation; float magnitude = 1 - agentSeparation / steerParams.SeparationRange; return separation * magnitude * steerParams.SeparationWeight; }