Exemplo n.º 1
0
 public override void DidMoveToView(SpriteKit.SKView view)
 {
     base.DidMoveToView(view);
     Player = new AgentNode(this, DefaultAgentRadius, new CGPoint(Frame.GetMidX(), Frame.GetMidY()));
     Player.Agent.Behavior = new GKBehavior();
     AgentSystem.AddComponent(Player.Agent);
     SeekGoal = GKGoal.GetGoalToSeekAgent(TrackingAgent);
 }
Exemplo n.º 2
0
        AgentNode AddFriend(CGPoint point)
        {
            var friend = new AgentNode(this, DefaultAgentRadius, point)
            {
                Color = SKColor.Cyan
            };

            AgentSystem.AddComponent(friend.Agent);
            return(friend);
        }
Exemplo n.º 3
0
 public Agent(AgentSystem agSys, Point3d position, Vector3d velocity)
 {
     this.agSys    = agSys;
     this.position = position;
     this.velocity = velocity;
     acceleration  = Vector3d.Zero;
     MaxSpeed      = 1.5f;
     PheroStrength = 0.1f;
     visAng        = agSys.sensAng;
     neighbours    = new List <int>();
 }
Exemplo n.º 4
0
        public override void DidMoveToView(SpriteKit.SKView view)
        {
            base.DidMoveToView(view);
            var wanderer = new AgentNode(this, DefaultAgentRadius, new CGPoint(Frame.GetMidX(), Frame.GetMidY()))
            {
                Color = SKColor.Cyan
            };

            wanderer.Agent.Behavior = GKBehavior.FromGoal(GKGoal.GetGoalToWander(10), 100f);
            AgentSystem.AddComponent(wanderer.Agent);
        }
Exemplo n.º 5
0
        public override void Update(double currentTime)
        {
            if (lastUpdateTime == 0)
            {
                lastUpdateTime = currentTime;
            }

            var delta = currentTime - lastUpdateTime;

            lastUpdateTime = currentTime;
            AgentSystem.Update(delta);
        }
Exemplo n.º 6
0
    /// <summary>
    /// This procedure contains the user code. Input parameters are provided as regular arguments,
    /// Output parameters as ref arguments. You don't have to assign output parameters,
    /// they will have a default value.
    /// </summary>
    private void RunScript(bool reset, bool go, int mode, List <Point3d> P, List <Vector3d> V, double nR, double coS, double alS, double seS, double seR, ref object Ap, ref object Av)
    {
        // <Custom code>
        GH_Point[]  ptsOut;
        GH_Vector[] vecOut;

        if (reset || AgSys == null)
        {
            AgSys = new AgentSystem(P, V);
        }

        if (go)
        {
            AgSys.NeighborhoodRadius = nR;
            AgSys.CohesionStrength   = coS;
            AgSys.AlignmentStrength  = alS;
            AgSys.SeparationStrength = seS;
            AgSys.SeparationRadius   = seR;

            /*
             * if(condition)
             * {}
             * else
             * {}
             */
            if (AgSys.Agents.Count < 600)
            {
                AgSys.Update();
            }
            else
            {
                switch (mode)
                {
                case 0:
                    AgSys.UpdateParallel();
                    break;

                case 1:
                    AgSys.UpdateRTree();
                    break;
                }
            }

            Component.ExpireSolution(true);
        }

        AgSys.GetPtsVecs(out ptsOut, out vecOut);
        Ap = ptsOut;
        Av = vecOut;

        // </Custom code>
    }
Exemplo n.º 7
0
        public override void DidMoveToView(SKView view)
        {
            base.DidMoveToView(view);

            var obstacles = new [] {
                AddObstacle(new CGPoint(Frame.GetMidX(), Frame.GetMidY() + 150f)),
                AddObstacle(new CGPoint(Frame.GetMidX() - 200f, Frame.GetMidY() - 150f)),
                AddObstacle(new CGPoint(Frame.GetMidX() + 200f, Frame.GetMidY() - 150f))
            };

            Player = new AgentNode(this, DefaultAgentRadius, new CGPoint(Frame.GetMidX(), Frame.GetMidY()));
            Player.Agent.Behavior = new GKBehavior();
            AgentSystem.AddComponent(Player.Agent);
            SeekGoal = GKGoal.GetGoalToSeekAgent(TrackingAgent);
            Player.Agent.Behavior.SetWeight(100, GKGoal.GetGoalToAvoidObstacles(obstacles, 1));
        }
Exemplo n.º 8
0
    /// <summary>
    /// This procedure contains the user code. Input parameters are provided as regular arguments,
    /// Output parameters as ref arguments. You don't have to assign output parameters,
    /// they will have a default value.
    /// </summary>
    private void RunScript(bool reset, bool go, List <Point3d> P, List <Vector3d> V, double nR, double coS, double alS, double seS, double seR, ref object Ap, ref object Av, ref object At)
    {
        // <Custom code>

        #region initialization
        // initialization
        if (reset || AgSys == null)
        {
            AgSys = new AgentSystem(P, V);
        }
        #endregion

        // update
        if (go)
        {
            // parameters update
            AgSys.NeighborhoodRadius = nR;
            AgSys.CohesionStrength   = coS;
            AgSys.AlignmentStrength  = alS;
            AgSys.SeparationStrength = seS;
            AgSys.SeparationRadius   = seR;


            // system update
            //AgSys.Update();
            AgSys.UpdateRTree();
            // update solution
            Component.ExpireSolution(true);
        }

        // output
        GH_Point[]  ptOut;
        GH_Vector[] velOut;


        AgSys.GetPtsVecs(out ptOut, out velOut);

        Ap = ptOut;
        Av = velOut;
        At = AgSys.GetAgentsTrails();


        // </Custom code>
    }
Exemplo n.º 9
0
        public override void DidMoveToView(SKView view)
        {
            base.DidMoveToView(view);

            var       agents       = new List <GKAgent2D> (20);
            const int agentsPerRow = 4;

            for (int i = 0; i < agentsPerRow * agentsPerRow; i++)
            {
                var x    = Frame.GetMidX() + i % agentsPerRow * 20;
                var y    = Frame.GetMidY() + i / agentsPerRow * 20;
                var boid = new AgentNode(this, 10, new CGPoint(x, y));
                AgentSystem.AddComponent(boid.Agent);
                agents.Add(boid.Agent);
                boid.DrawsTail = false;
            }

            const float separationRadius = 0.553f * 50;
            const float separationAngle  = (float)(3 * Math.PI / 4.0f);
            const float separationWeight = 10.0f;

            const float alignmentRadius = 0.83333f * 50;
            const float alignmentAngle  = (float)(Math.PI / 4.0f);
            const float alignmentWeight = 12.66f;

            const float cohesionRadius = 1.0f * 100;
            const float cohesionAngle  = (float)(Math.PI / 2.0f);
            const float cohesionWeight = 8.66f;

            // Separation, alignment, and cohesion goals combined cause the flock to move as a group.
            var behavior = new GKBehavior();

            behavior.SetWeight(separationWeight, GKGoal.GetGoalToSeparate(agents.ToArray(), separationRadius, separationAngle));
            behavior.SetWeight(alignmentWeight, GKGoal.GetGoalToAlign(agents.ToArray(), alignmentRadius, alignmentAngle));
            behavior.SetWeight(cohesionWeight, GKGoal.GetGoalToCohere(agents.ToArray(), cohesionRadius, cohesionAngle));

            foreach (GKAgent2D agent in agents)
            {
                agent.Behavior = behavior;
            }

            SeekGoal = GKGoal.GetGoalToSeekAgent(TrackingAgent);
        }
Exemplo n.º 10
0
        public override void DidMoveToView(SKView view)
        {
            base.DidMoveToView(view);

            var follower = new AgentNode(this, DefaultAgentRadius, new CGPoint(Frame.GetMidX(), Frame.GetMidY()))
            {
                Color = SKColor.Cyan
            };

            var center = new Vector2((float)Frame.GetMidX(), (float)Frame.GetMidY());
            var points = new [] {
                new Vector2(center.X, center.Y + 50),
                new Vector2(center.X + 50, center.Y + 150),
                new Vector2(center.X + 100, center.Y + 150),
                new Vector2(center.X + 200, center.Y + 200),
                new Vector2(center.X + 350, center.Y + 150),
                new Vector2(center.X + 300, center.Y),
                new Vector2(center.X, center.Y - 200),
                new Vector2(center.X - 200, center.Y - 100),
                new Vector2(center.X - 200, center.Y),
                new Vector2(center.X - 100, center.Y + 50),
            };

            var path = GKPath.FromPoints(points, DefaultAgentRadius, true);

            follower.Agent.Behavior = GKBehavior.FromGoal(GKGoal.GetGoalToFollowPath(path, 1.5, true), 1);
            AgentSystem.AddComponent(follower.Agent);

            var cgPoints = new CGPoint[11];

            for (var i = 0; i < 10; i++)
            {
                cgPoints [i] = new CGPoint(points [i].X, points [i].Y);
            }

            cgPoints [10] = cgPoints [0];
            var pathShape = SKShapeNode.FromPoints(ref cgPoints [0], 11);

            pathShape.LineWidth   = 2;
            pathShape.StrokeColor = SKColor.Magenta;
            AddChild(pathShape);
        }
Exemplo n.º 11
0
        public override void DidMoveToView(SpriteKit.SKView view)
        {
            base.DidMoveToView(view);
            Player = new AgentNode(this, DefaultAgentRadius, new CGPoint(Frame.GetMidX(), Frame.GetMidY()));
            Player.Agent.Behavior = new GKBehavior();
            AgentSystem.AddComponent(Player.Agent);
            Player.Agent.MaxSpeed *= 1.2f;

            SeekGoal = GKGoal.GetGoalToSeekAgent(TrackingAgent);

            Friends = new [] {
                AddFriend(new CGPoint(Frame.GetMidX() - 150f, Frame.GetMidY())),
                AddFriend(new CGPoint(Frame.GetMidX() + 150f, Frame.GetMidY()))
            };
            SeparateGoal = GKGoal.GetGoalToSeparate(new [] { Player.Agent }, 100f, (float)(Math.PI * 2));
            var behavior = GKBehavior.FromGoal(SeparateGoal, 100f);

            foreach (var friend in Friends)
            {
                friend.Agent.Behavior = behavior;
            }
        }
Exemplo n.º 12
0
    /// <summary>
    /// This procedure contains the user code. Input parameters are provided as regular arguments,
    /// Output parameters as ref arguments. You don't have to assign output parameters,
    /// they will have a default value.
    /// </summary>
    private void RunScript(bool reset, bool go, bool debug, int method, Mesh M, List <Point3d> P, double sR, double sI, double dR, double eR, double sensDist, double sensAng, ref object points, ref object vectors, ref object outMCol, ref object neigh, ref object neighCol, ref object sensors)
    {
        // <Custom code>
        // ............................................................................
        // Stigmergy system

        /*
         * notes on code refactoring
         *
         * 2 main classes:
         * . Particle System (and a class Particle)
         * . Environment (Mesh with scalar and optional vector field)
         *
         * RunScript function structure:
         *
         * . check inputs & eventually bypass execution
         * . initialise Environment
         * . initialise Particle System
         * . Update live variables
         * . Update Particle System
         * . Update Environment
         * . Extract Output Geometries and Data
         *
         */

        // return on null input
        if (M == null || P == null)
        {
            return;
        }


        // initialize on first execution or mesh change
        if (AS == null || M.Vertices.Count != ME.scalarField.Length)
        {
            // initialize particle system
            ME = new MeshEnvironment(M, dR, eR);
            AS = new AgentSystem(P, ME);
        }

        // restore initial values on reset
        if (reset)
        {
            // initialize particle system
            ME.RestoreScalarField();
            AS = new AgentSystem(P, ME);
        }

        if (go)
        {
            // update runtime variables
            AS.seekRadius      = sR;
            AS.seekIntensity   = sI;
            AS.sensDist        = sensDist; // old value 3.0
            AS.sensAng         = sensAng;
            ME.diffusionRate   = (float)dR;
            ME.evaporationRate = (float)eR;

            // update simulation
            switch (method)
            {
            case 0:
                AS.UpdateRTree();
                break;

            case 1:
                AS.UpdateJones();
                break;
            }

            // update environment (diffusion + evaporation)
            ME.Update();

            // update component
            Component.ExpireSolution(true);
        }

        // . . . . . . .  extract geometries

        // particles positions and velocites
        AS.GetPointsVectors(out pts, out vecs);
        points  = pts;
        vectors = vecs;

        // colored mesh
        outMCol = ME.GetColoredMesh();

        // debug mode
        if (debug)
        {
            switch (method)
            {
            case 0:
                neigh    = AS.GetNeighPts();
                neighCol = AS.GetNeighBrightness();
                break;

            case 1:
                sensors = AS.SensorsOut();
                break;
            }
        }

        // ............................................................................
        // </Custom code>
    }