Пример #1
0
        public void GenerateFromBlueprint(bool[,] blueprint)
        {
            this.blueprint = blueprint;

            width = blueprint.GetLength(0);
            height = blueprint.GetLength(1);

            lattice = new LatticePoint[width, height];

            int x, y;

            for (x = 0; x < width; x++)
            {
                for (y = 0; y < height; y++)
                {
                    lattice[x, y] = new LatticePoint();
                    lattice[x,y].index = new Point(x,y);

                    if (blueprint[x, y] == true)
                    {
                        // Generate particle
                        Particle p = new Particle();
                        lattice[x, y].particle = p;
                        p.body = this;
                        p.x0 = new Vector2(spacing.X * x, spacing.Y * y);
                        p.x = offset + p.x0;
                        p.latticePoint = lattice[x, y];
                        p.goal = p.x;
                        particles.Add(p);
                    }
                    else
                    {
                    }
                }
            }

            // Set up the neighbors
            for (x = 0; x < width; x++)
            {
                for (y = 0; y < height; y++)
                {
                    if (blueprint[x, y] == true)
                    {
                        if (InBounds(x + 1, y) && blueprint[x + 1, y] == true)
                        {
                            lattice[x, y].particle.xPos = lattice[x + 1, y].particle;
                            lattice[x + 1, y].particle.xNeg = lattice[x, y].particle;
                        }
                        if (InBounds(x, y + 1) && blueprint[x, y + 1] == true)
                        {
                            lattice[x, y].particle.yPos = lattice[x, y + 1].particle;
                            lattice[x, y + 1].particle.yNeg = lattice[x, y].particle;
                        }
                    }
                }
            }

            // Create the smoothing regions and have them generate themselves
            foreach (Particle p in particles)
            {
                SmoothingRegion s = new SmoothingRegion(w);
                s.body = this;
                s.latticePoint = p.latticePoint;
                s.latticePoint.smoothingRegion = s;
                s.RegenerateRegion();
                smoothingRegions.Add(s);
            }

            // Create one chunk with all the particles in it
            Chunk c = new Chunk();
            foreach (Particle p in particles)
            {
                c.particles.Add(p);
                p.chunk = c;
            }
            c.CalculateInvariants();
            chunks.Add(c);

            // Setup the regions
            foreach (SmoothingRegion s in smoothingRegions)
            {
                s.CalculateInvariants();
            }
        }