示例#1
0
        private Vector2 FleeFromPredator(Boid me)
        {
            if (me.ToString() == "BoidSimulation.Predator")
            {
                return(Vector2.Zero);
            }
            else
            {
                Vector2 FleeVel = Vector2.Zero;
                int     PredCnt = 0;
                float   percent = 0.01f;

                List <Boid> boidList = SimWorld.GetInstance().GetBoidList();
                foreach (Boid b in boidList)
                {
                    if (b.ToString() == "BoidSimulation.Predator")
                    {
                        Vector2 diff = b.GetPosition() - me.GetPosition();
                        if (diff.Length() <= me.StayAwayRadius)
                        {
                            FleeVel -= diff;
                            PredCnt++;
                        }
                    }
                }

                if (PredCnt > 0)
                {
                    FleeVel /= PredCnt;
                    return(FleeVel * percent);
                }

                return(Vector2.Zero);
            }
        }
示例#2
0
        //can we merge this with a general collision avoidance algorithm?
        private Vector2 KeepApart(Boid me)
        {
            Vector2 KeepApartVel = Vector2.Zero;

            List <Boid> boidList = SimWorld.GetInstance().GetBoidList();

            foreach (Boid b in boidList)
            {
                if (me == b)
                {
                    continue;
                }
                else
                {
                    //test to see if we are smaller than a certain range (circle test)
                    Vector2 diff = b.GetPosition() - me.GetPosition();
                    if (diff.Length() <= me.Radius)
                    {
                        KeepApartVel -= diff;
                    }
                }
            }

            return(KeepApartVel);
        }
示例#3
0
        private Vector2 Flock(Boid me)
        {
            Vector2 CenterOfMassVel = Vector2.Zero;
            Vector2 KeepApartVel    = Vector2.Zero;
            Vector2 MatchBoidVel    = Vector2.Zero;

            List <Boid> boidList = SimWorld.GetInstance().GetBoidList();

            foreach (Boid b in boidList)
            {
                if (me == b)
                {
                    continue;
                }
                else
                {
                    //sum up the positions
                    CenterOfMassVel += b.GetPosition();

                    //sum up the velocities.
                    MatchBoidVel += b.GetVelocity();

                    //test to see if we are smaller than a certain range (circle test)
                    Vector2 diff = b.GetPosition() - me.GetPosition();
                    if (diff.Length() <= me.Radius)
                    {
                        KeepApartVel -= diff;
                    }
                }
            }

            CenterOfMassVel /= boidList.Count - 1;
            CenterOfMassVel *= 0.01f;

            MatchBoidVel /= boidList.Count - 1;
            MatchBoidVel *= me.Strength;

            MatchBoidVel *= 0.01f;

            Vector2 totalVel = (CenterOfMassVel + MatchBoidVel + KeepApartVel + FollowMouse(me)) * 0.001f;

            totalVel += FleeFromPredator(me) * 0.05f;
            totalVel += CollisionTest(me);

            return(totalVel);
        }
示例#4
0
        protected Vector2 CollisionTest(Boid me)
        {
            List <Plane> collisionList = SimWorld.GetInstance().GetCollisionList();

            Vector4 pos = new Vector4(me.GetPosition(), 0.0f, 1.0f);
            Vector3 avoidCollisionVel = Vector3.Zero;

            foreach (Plane p in collisionList)
            {
                float distance = p.Dot(pos) / p.Normal.Length();

                if (distance <= 5.0f)
                {
                    avoidCollisionVel += p.Normal * (me.Radius - distance);
                }
            }

            return(new Vector2(avoidCollisionVel.X, avoidCollisionVel.Y) * 0.001f);
        }
示例#5
0
文件: Boid.cs 项目: pengare/flocking
        public virtual bool DetectNearbyPredators()
        {
            if (this.ToString() == "BoidSimulation.Predator")
            {
                return(false);
            }

            List <Boid> boidList = SimWorld.GetInstance().GetBoidList();

            foreach (Boid b in boidList)
            {
                if (b.ToString() == "BoidSimulation.Predator")
                {
                    Vector2 diff = b.GetPosition() - this.GetPosition();
                    if (diff.Length() <= mStayAwayRadius)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
示例#6
0
文件: Game1.cs 项目: pengare/flocking
        /// <summary>
        /// Load your graphics content.  If loadAllContent is true, you should
        /// load content from both ResourceManagementMode pools.  Otherwise, just
        /// load ResourceManagementMode.Manual content.
        /// </summary>
        /// <param name="loadAllContent">Which type of content to load.</param>
        protected override void LoadContent()
        {
            //if (loadAllContent)
            {
                mSimWorldInstance = SimWorld.GetInstance();
                mViewport         = graphics.GraphicsDevice.Viewport;

                //set up the collision detection planes
                mSimWorldInstance.AddCollisionPlane(new Plane(new Vector3(1.0f, 0.0f, 0.0f), 0.0f));
                mSimWorldInstance.AddCollisionPlane(new Plane(new Vector3(-1.0f, 0.0f, 0.0f), mViewport.Width));
                mSimWorldInstance.AddCollisionPlane(new Plane(new Vector3(0.0f, 1.0f, 0.0f), 0.0f));
                mSimWorldInstance.AddCollisionPlane(new Plane(new Vector3(0.0f, 1.0f, 0.0f), mViewport.Height));

                //mouse cursor sprite.
                mMouseCursor = content.Load <Texture2D>("Images/mouse");

                //set up the boid sprites.
                mSpriteBatch = new SpriteBatch(graphics.GraphicsDevice);

                //create boids.
                Random rand = new Random();
                for (int i = 0; i < NUMBOIDS; ++i)
                {
                    Boid newBoid = new Boid(rand.Next(mViewport.Width), rand.Next(mViewport.Height), new FlockStrategy());
                    newBoid.LoadGraphicAsset(content);
                    mSimWorldInstance.AddBoid(newBoid);
                }

                for (int j = 0; j < 10; ++j)
                {
                    Predator newPredator = new Predator(rand.Next(mViewport.Width), rand.Next(mViewport.Height), new WanderStrategy());
                    newPredator.LoadGraphicAsset(content);
                    mSimWorldInstance.AddBoid(newPredator);
                }
            }
        }