コード例 #1
0
        private void UpdatePhysics()
        {
            colony.Wander(new Random());

            if (timeSteps % 10 == 0 && doSpawn)
            {
                colony.ants.Add(new Ant(5, new Vector2(w / 2, h / 2), Misc.VecFromAng(r.NextDouble(), (float)(new Random().NextDouble()) * 2f)));
            }

            if (timeSteps % 3 == 0)
            {
                colony.UpdatePheromones();
            }

            colony.pathPheromonesQTree = new QTree(new Vector2(w / 2, h / 2), new Vector2(w, h), 1);
            colony.pathPheromonesQTree.Fill(colony.pathPheromones.ToList <Point>());

            colony.foodPheromonesQTree = new QTree(new Vector2(w / 2, h / 2), new Vector2(w, h), 1);
            colony.foodPheromonesQTree.Fill(colony.foodPheromones.ToList <Point>());

            foodQTree = new QTree(new Vector2(w / 2, h / 2), new Vector2(w, h), 1);
            foodQTree.Fill(food);

            colony.FollowPheromones();
            colony.SeekFood(foodQTree);
            colony.SeekHome(new Vector2(w / 2, h / 2));

            colony.AvoidBorders(30, w, h);
            colony.BounceFromBorders(w, h);
            colony.UpdateLocation();
        }
コード例 #2
0
ファイル: QTree.cs プロジェクト: iamwhoamianamy/AntColony
        void Subdivide()
        {
            if (!isDivided)
            {
                topleft  = new QTree(new Vector2(loc.X - dim.X / 4, loc.Y - dim.Y / 4), new Vector2(dim.X / 2, dim.Y / 2), capacity);
                topright = new QTree(new Vector2(loc.X + dim.X / 4, loc.Y - dim.Y / 4), new Vector2(dim.X / 2, dim.Y / 2), capacity);
                botleft  = new QTree(new Vector2(loc.X - dim.X / 4, loc.Y + dim.Y / 4), new Vector2(dim.X / 2, dim.Y / 2), capacity);
                botright = new QTree(new Vector2(loc.X + dim.X / 4, loc.Y + dim.Y / 4), new Vector2(dim.X / 2, dim.Y / 2), capacity);

                isDivided = true;
            }
        }
コード例 #3
0
        public void SeekFood(QTree foodQTree)
        {
            foreach (var ant in ants)
            {
                if (!ant.isCarryingFood)
                {
                    List <Neighbour> neibs = new List <Neighbour>();
                    foodQTree.Quarry(new Point(ant.loc + ant.vel.Normalized() * ant.size * 1.5f), ant.size * 3.0f, neibs);

                    if (neibs.Count != 0)
                    {
                        Neighbour minNeib = neibs[0];
                        float     minDist = Vector2.Distance(neibs[0].point.loc, ant.loc);

                        foreach (var n in neibs)
                        {
                            float dist = Vector2.Distance(n.point.loc, ant.loc);
                            if (dist < minDist)
                            {
                                minDist = dist;
                                minNeib = n;
                            }
                        }

                        if (minDist < ant.size)
                        {
                            ant.isCarryingFood = true;
                            ant.vel           *= -1;
                        }
                        else
                        {
                            ant.Steer(minNeib.point.loc);
                        }
                    }
                }
            }
        }