コード例 #1
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
        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
ファイル: Strategy.cs プロジェクト: pengare/flocking
        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;
        }
コード例 #3
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
        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);
        }
コード例 #4
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
        private Vector2 GetNewWanderPoint(Boid me)
        {
            Vector2 mForward = me.GetVelocity();

            if (mForward.Length() <= 0.0f)
            {
                return(Vector2.Zero);
            }

            mForward.Normalize();

            Vector2 circleCenter = me.GetPosition() + mForward * 10.0f;

            Random rand   = new Random();
            float  angle  = rand.Next(360);
            float  radius = 10.0f;

            Vector2 circlePoint = new Vector2((float)Math.Cos(MathHelper.ToRadians(angle) * radius),
                                              (float)Math.Sin(MathHelper.ToRadians(angle) * radius));

            return(circleCenter + circlePoint);
        }
コード例 #5
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);
                }
            }
        }
コード例 #6
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
 public override Vector2 UpdateAI(GameTime gameTime, Boid me)
 {
     return(Flock(me));
     //return base.UpdateAI(gameTime, boidList);
 }
コード例 #7
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
 //don't do anything.
 public override Vector2 UpdateAI(GameTime gameTime, Boid me)
 {
     me.Stamina = me.Stamina + (float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.01f;
     return(Vector2.Zero);
     //return base.UpdateAI(gameTime, me, boidList);
 }
コード例 #8
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
 public virtual Vector2 UpdateAI(GameTime gameTime, Boid me)
 {
     return(Vector2.Zero);
 }
コード例 #9
0
ファイル: SimWorld.cs プロジェクト: pengare/flocking
 public void AddBoid(Boid newBoid)
 {
     mBoidList.Add(newBoid);
 }
コード例 #10
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);
                }
            }
        }
コード例 #11
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
 //don't do anything.
 public override Vector2 UpdateAI(GameTime gameTime, Boid me)
 {
     me.Stamina = me.Stamina + (float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.01f;
     return Vector2.Zero;
     //return base.UpdateAI(gameTime, me, boidList);
 }
コード例 #12
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
 public override Vector2 UpdateAI(GameTime gameTime, Boid me)
 {
     return Flock(me);
     //return base.UpdateAI(gameTime, boidList);
 }
コード例 #13
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
        //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;
        }
コード例 #14
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
        private Vector2 GetNewWanderPoint( Boid me )
        {
            Vector2 mForward = me.GetVelocity();
            if (mForward.Length() <= 0.0f)
            {
                return Vector2.Zero;
            }

            mForward.Normalize();

            Vector2 circleCenter = me.GetPosition() + mForward * 10.0f;

            Random rand = new Random();
            float angle = rand.Next(360);
            float radius = 10.0f;

            Vector2 circlePoint = new Vector2((float)Math.Cos(MathHelper.ToRadians(angle) * radius),
                                              (float)Math.Sin(MathHelper.ToRadians(angle) * radius));

            return circleCenter + circlePoint;
        }
コード例 #15
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
        public override Vector2 UpdateAI(GameTime gameTime, Boid me)
        {
            //get distance between me and the wander point.
            Vector2 wanderPoint = GetNewWanderPoint(me);

            Vector2 diff = wanderPoint - me.GetPosition();
            diff += KeepApart(me);
            diff += CollisionTest(me);

            return diff * 0.01f;
             //return base.UpdateAI(gameTime, boidList);
        }
コード例 #16
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
        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;
        }
コード例 #17
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
 public virtual Vector2 UpdateAI(GameTime gameTime, Boid me)
 {
     return Vector2.Zero;
 }
コード例 #18
0
ファイル: SimWorld.cs プロジェクト: pengare/flocking
 public void AddBoid(Boid newBoid)
 {
     mBoidList.Add(newBoid);
 }
コード例 #19
0
ファイル: Strategy.cs プロジェクト: pengare/flocking
        //find the center of the screen and go there.
        private Vector2 FollowMouse(Boid me)
        {
            MouseState mouse = Mouse.GetState();

            Vector2 mousePos = new Vector2(mouse.X, mouse.Y);

            return( ( mousePos - me.GetPosition() ) * 0.05f);
            //return Vector2.Zero;
        }