コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
0
        /// <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;
        }
コード例 #4
0
 /// <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));
 }
コード例 #5
0
        /// <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;
        }
コード例 #6
0
 /// <summary>
 /// Desire to move toward a specific point
 /// </summary>
 public static Vector3 GoalForce(IAgent agent, SteerParameters steerParams)
 {
     return (steerParams.GoalPosition - agent.Position) * steerParams.GoalWeight;
 }
コード例 #7
0
 /// <summary>
 /// Desire to align direction with another agent
 /// </summary>
 public static Vector3 AlignmentForce(IAgent otherAgent, SteerParameters steerParams)
 {
     return otherAgent.Direction * steerParams.AlignmentWeight;
 }
コード例 #8
0
        /// <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;
        }
コード例 #9
0
        /// <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;
        }