示例#1
0
        void DrawLine(Muscle muscle)
        {
            Vector2 edge = muscle.PosC - muscle.PosP;
            // calculate angle to rotate line
            float angle = (float)Math.Atan2(edge.Y, edge.X);

            var texture = _textures.Muscle(muscle.Strength);

            _spriteBatch.Draw(texture,
                              new Rectangle(// rectangle defines shape of line and position of start of line
                                  (int)muscle.PosP.X,
                                  (int)muscle.PosP.Y,
                                  (int)edge.Length(),                     //sb will strech the texture to fill this rectangle
                                  1),                                     //width of line, change this to make thicker line
                              null,
                              new Color((255 - muscle.Strength), 10, 10), //colour of line
                              angle,                                      //angle of line (calulated above)
                              new Vector2(0, 0),                          // point in line about which to rotate
                              SpriteEffects.None,
                              0);
        }
示例#2
0
 private float ComputeDelta(Vector2 a, Muscle m)
 {
     return(m.Length - a.Length());
 }
示例#3
0
        public Animal(Chromosome chromosome, Vector2 startingPosition, int id)
        {
            ID         = id;
            Chromosome = chromosome;
            Fitness    = 0;
            int length = Chromosome.Length - 4;

            Retard = false;

            for (short i = 0; i < length; i += 4)
            {
                if (Helper.ReadGene(Chromosome[i]) == Feature.Node)
                {
                    var subDna = Chromosome.Slice(i, 4);
                    var node   = new Node()
                    {
                        ID       = (short)Nodes.Count,
                        Weight   = Helper.NonZero(subDna[1], 10),
                        Position = startingPosition - new Vector2(Helper.Scale(subDna[2]), Helper.Scale(subDna[3]))
                    };
                    Nodes.Add(node);
                }
            }
            if (Nodes.Count == 0)
            {
                Retard = true;
                Muscles.Clear();
                return;
            }
            for (short i = 0; i < length; i += 4)
            {
                if (Helper.ReadGene(Chromosome[i]) == Feature.Muscle)
                {
                    var subDna = Chromosome.Slice(i, 4);
                    var muscle = new Muscle()
                    {
                        ID          = (short)Muscles.Count,
                        Strength    = Helper.NonZero(subDna[1], 10),
                        Length      = Helper.NonZero(Helper.Scale(subDna[2]), 10),
                        LengthAlpha = Helper.Scale(subDna[2]),
                        NodeP       = (byte)(subDna[3] % Nodes.Count),
                        NodeC       = (byte)(Muscles.Count % Nodes.Count)
                    };
                    Muscles.Add(muscle);
                }
            }
            if (Muscles.Count == 0)
            {
                Retard = true;
                Nodes.Clear();
                return;
            }
            for (short i = 0; i < length; i += 4)
            {
                if (Helper.ReadGene(Chromosome[i]) == Feature.NodeSpeed)
                {
                    var subDna = Chromosome.Slice(i, 4);
                    var node   = subDna[1] % Nodes.Count;
                    var x      = (float)subDna[2] / 100;
                    var y      = (float)subDna[3] / 100;
                    Nodes[node].Velocity.X = x;
                    Nodes[node].Velocity.Y = y;
                }
                if (Helper.ReadGene(Chromosome[i]) == Feature.MuscleOscillation)
                {
                    var subDna = Chromosome.Slice(i, 4);
                    var muscle = subDna[1] % Muscles.Count;
                    Muscles[muscle].OscRange = Helper.NonZero(Helper.Scale(subDna[2]), 10);
                    Muscles[muscle].OscSpeed = (float)subDna[3] / 300;
                }
            }
            foreach (var m in Muscles)
            {
                Nodes[m.NodeC].Muscles.Add(m);
                Nodes[m.NodeP].Muscles.Add(m);
            }
            foreach (var n in Nodes)
            {
                if (n.Muscles.Count == 0)
                {
                    Retard = true;
                }
            }
        }