コード例 #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
        //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
ファイル: 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;
            }
        }
コード例 #4
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;
        }
コード例 #5
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);
        }
コード例 #6
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);
        }
コード例 #7
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);
        }
コード例 #8
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);
        }
コード例 #9
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;
        }
コード例 #10
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;
        }
コード例 #11
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;
        }
コード例 #12
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);
        }
コード例 #13
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;
        }
コード例 #14
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;
        }