//---------------------- CalculateDithered ----------------------------
        //
        //  this method sums up the active behaviors by assigning a probabilty
        //  of being calculated to each behavior. It then tests the first priority
        //  to see if it should be calcukated this simulation-step. If so, it
        //  calculates the steering force resulting from this behavior. If it is
        //  more than zero it returns the force. If zero, or if the behavior is
        //  skipped it continues onto the next priority, and so on.
        //
        //  NOTE: Not all of the behaviors have been implemented in this method,
        //        just a few, so you get the general idea
        //------------------------------------------------------------------------
        public Vector2D CalculateDithered()
        {
            //reset the steering force
            m_vSteeringForce.Zero();

            if (On(behavior_type.wall_avoidance) && Utils.RandFloat() < SteerParams.Instance.PrWallAvoidance)
            {
                m_vSteeringForce = WallAvoidance(GameWorld.Instance.Walls) *
                                     m_dWeightWallAvoidance / SteerParams.Instance.PrWallAvoidance;

                if (!m_vSteeringForce.isZero())
                {
                    m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                    return m_vSteeringForce;
                }
            }

            if (On(behavior_type.obstacle_avoidance) && Utils.RandFloat() < SteerParams.Instance.PrObstacleAvoidance)
            {
                m_vSteeringForce += ObstacleAvoidance(GameWorld.Instance.Obstacles) *
                        m_dWeightObstacleAvoidance / SteerParams.Instance.PrObstacleAvoidance;

                if (!m_vSteeringForce.isZero())
                {
                    m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                    return m_vSteeringForce;
                }
            }

            if (!GameWorld.Instance.SpacePartitioningOn)
            {
                if (On(behavior_type.separation) && Utils.RandFloat() < SteerParams.Instance.PrSeparation)
                {
                    m_vSteeringForce += Separation(GameWorld.Instance.Agents) *
                                        m_dWeightSeparation / SteerParams.Instance.PrSeparation;

                    if (!m_vSteeringForce.isZero())
                    {
                        m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                        return m_vSteeringForce;
                    }
                }
            }

            else
            {
                if (On(behavior_type.separation) && Utils.RandFloat() < SteerParams.Instance.PrSeparation)
                {
                    m_vSteeringForce += SeparationPlus(GameWorld.Instance.Agents) *
                                        m_dWeightSeparation / SteerParams.Instance.PrSeparation;

                    if (!m_vSteeringForce.isZero())
                    {
                        m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                        return m_vSteeringForce;
                    }
                }
            }


            if (On(behavior_type.flee) && Utils.RandFloat() < SteerParams.Instance.PrFlee)
            {
                Debug.Assert(!Vector2D.IsNull(GameWorld.Instance.TargetPos), "TargetPos not assigned");
                m_vSteeringForce += Flee(GameWorld.Instance.TargetPos) * m_dWeightFlee / SteerParams.Instance.PrFlee;

                if (!m_vSteeringForce.isZero())
                {
                    m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                    return m_vSteeringForce;
                }
            }

            if (On(behavior_type.evade) && Utils.RandFloat() < SteerParams.Instance.PrEvade)
            {
                Debug.Assert(m_pTargetAgent1 != null, "Evade target not assigned");

                m_vSteeringForce += Evade(m_pTargetAgent1) * m_dWeightEvade / SteerParams.Instance.PrEvade;

                if (!m_vSteeringForce.isZero())
                {
                    m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                    return m_vSteeringForce;
                }
            }


            if (!GameWorld.Instance.SpacePartitioningOn)
            {
                if (On(behavior_type.allignment) && Utils.RandFloat() < SteerParams.Instance.PrAlignment)
                {
                    m_vSteeringForce += Alignment(GameWorld.Instance.Agents) *
                                        m_dWeightAlignment / SteerParams.Instance.PrAlignment;

                    if (!m_vSteeringForce.isZero())
                    {
                        m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                        return m_vSteeringForce;
                    }
                }

                if (On(behavior_type.cohesion) && Utils.RandFloat() < SteerParams.Instance.PrCohesion)
                {
                    m_vSteeringForce += Cohesion(GameWorld.Instance.Agents) *
                                        m_dWeightCohesion / SteerParams.Instance.PrCohesion;

                    if (!m_vSteeringForce.isZero())
                    {
                        m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                        return m_vSteeringForce;
                    }
                }
            }
            else
            {
                if (On(behavior_type.allignment) && Utils.RandFloat() < SteerParams.Instance.PrAlignment)
                {
                    m_vSteeringForce += AlignmentPlus(GameWorld.Instance.Agents) *
                                        m_dWeightAlignment / SteerParams.Instance.PrAlignment;

                    if (!m_vSteeringForce.isZero())
                    {
                        m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                        return m_vSteeringForce;
                    }
                }

                if (On(behavior_type.cohesion) && Utils.RandFloat() < SteerParams.Instance.PrCohesion)
                {
                    m_vSteeringForce += CohesionPlus(GameWorld.Instance.Agents) *
                                        m_dWeightCohesion / SteerParams.Instance.PrCohesion;

                    if (!m_vSteeringForce.isZero())
                    {
                        m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                        return m_vSteeringForce;
                    }
                }
            }

            if (On(behavior_type.wander) && Utils.RandFloat() < SteerParams.Instance.PrWander)
            {
                m_vSteeringForce += Wander() * m_dWeightWander / SteerParams.Instance.PrWander;

                if (!m_vSteeringForce.isZero())
                {
                    m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                    return m_vSteeringForce;
                }
            }

            if (On(behavior_type.seek) && Utils.RandFloat() < SteerParams.Instance.PrSeek)
            {
                Debug.Assert(!Vector2D.IsNull(GameWorld.Instance.TargetPos), "TargetPos not assigned");
                m_vSteeringForce += Seek(GameWorld.Instance.TargetPos) * m_dWeightSeek / SteerParams.Instance.PrSeek;

                if (!m_vSteeringForce.isZero())
                {
                    m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                    return m_vSteeringForce;
                }
            }

            if (On(behavior_type.arrive) && Utils.RandFloat() < SteerParams.Instance.PrArrive)
            {
                Debug.Assert(!Vector2D.IsNull(GameWorld.Instance.TargetPos), "TargetPos not assigned");
                m_vSteeringForce += Arrive(GameWorld.Instance.TargetPos, (int)m_Deceleration) *
                                    m_dWeightArrive / SteerParams.Instance.PrArrive;

                if (!m_vSteeringForce.isZero())
                {
                    m_vSteeringForce.Truncate(m_parentMovingEntity.MaxForce);

                    return m_vSteeringForce;
                }
            }

            return m_vSteeringForce;
        }
Beispiel #2
0
        public void Update(double time_elapsed) 
        {
            //keep a record of its old position so we can update its cell later in this method
            m_OldPos = Pos;

            Vector2D SteeringForce;

            //calculate the combined force from each steering behavior in the vehicle's list
            SteeringForce = m_pSteering.Calculate();

            //Acceleration = Force/Mass
            Vector2D acceleration = SteeringForce / m_dMass;

            //update velocity
            m_vVelocity += acceleration * time_elapsed;

            //make sure vehicle does not exceed maximum velocity
            m_vVelocity.Truncate(m_dMaxSpeed);

            //update the position
            m_vPos += m_vVelocity * time_elapsed;

            //update the heading if the vehicle has a non zero velocity
            if (m_vVelocity.LengthSq() > 0.00000001)
            {
                m_vHeading = Vector2D.Vec2DNormalize(m_vVelocity);
                m_vSide = m_vHeading.Perp();
            }
        }