示例#1
0
        /// <summary>
        /// Creates line renderers.
        /// </summary>
        /// <param name="sd">
        /// The spring damper.
        /// </param>
        /// <returns>
        /// The <see cref="GameObject"/>.
        /// </returns>
        private GameObject CreateLineRenderers(SpringDamper sd)
        {
            // instantiate a line prefab
            var lineGo = Instantiate(this.linePrefab, (sd.P1.Position + sd.P2.Position) / 2, new Quaternion()) as GameObject; // use line prefab to draw from first particle to second particle

            return(lineGo);
        }
示例#2
0
        /// <summary>
        /// Finds particle neighbors and creates spring dampers and line renderers there.
        /// </summary>
        private void SetNeighborsAndDampers()
        {
            foreach (var p in this.particleList)
            {
                p.Neighbors = new List <Particle>();
                var particleNum = 0;

                for (var i = 0; i < this.particleList.Count; i++)
                {
                    // go through however many particles are in the list
                    if (this.particleList[i] == p)
                    {
                        // if the index of a particle in the list equals the current particle, set that particle's number to be the same as that index
                        particleNum = i;
                    }
                }

                // Note: how many heights you have is how many times you iterate through widths. Create spring dampers for every neighbor
                if (particleNum + this.width < this.particleList.Count)
                {
                    // above neighbor
                    p.Neighbors.Add(this.particleList[particleNum + this.width]);                                                                        // add that particle to the neighbor list
                    var sd = new SpringDamper(p, this.particleList[particleNum + this.width], this.springConstant, this.dampingFactor, this.restLength); // create a spring damper
                    this.springDamperList.Add(sd);                                                                                                       // add that spring damper to the spring damper list
                    this.lineList.Add(this.CreateLineRenderers(sd));                                                                                     // create a line renderer here and add it to the line list
                }

                if ((particleNum + 1) % this.width > particleNum % this.width)
                {
                    // immediate right neighbor
                    p.Neighbors.Add(this.particleList[particleNum + 1]);                                                                        // add that particle to the neighbor list
                    var sd = new SpringDamper(p, this.particleList[particleNum + 1], this.springConstant, this.dampingFactor, this.restLength); // create a spring damper
                    this.springDamperList.Add(sd);                                                                                              // add that spring damper to the spring damper list
                    this.lineList.Add(this.CreateLineRenderers(sd));                                                                            // create a line renderer here and add it to the line list
                }

                if (particleNum + this.width + 1 < this.particleList.Count && (particleNum + 1) % this.width > particleNum % this.width)
                {
                    // top right neighbor
                    p.Neighbors.Add(this.particleList[particleNum + this.width + 1]);                                                                        // add that particle to the neighbor list
                    var sd = new SpringDamper(p, this.particleList[particleNum + this.width + 1], this.springConstant, this.dampingFactor, this.restLength); // create a spring damper
                    this.springDamperList.Add(sd);                                                                                                           // add that spring damper to the spring damper list
                    this.lineList.Add(this.CreateLineRenderers(sd));                                                                                         // create a line renderer here and add it to the line list
                }

                if (particleNum + this.width - 1 < this.particleList.Count && (particleNum + this.width - 1) % this.width < particleNum % this.width)
                {
                    // top left neighbor
                    p.Neighbors.Add(this.particleList[particleNum + this.width - 1]);                                                                        // add that particle to the neighbor list
                    var sd = new SpringDamper(p, this.particleList[particleNum + this.width - 1], this.springConstant, this.dampingFactor, this.restLength); // create a spring damper
                    this.springDamperList.Add(sd);                                                                                                           // add that spring damper to the spring damper list
                    this.lineList.Add(this.CreateLineRenderers(sd));                                                                                         // create a line renderer here and add it to the line list
                }
            }
        }
示例#3
0
        public GameObject CreateDrawer(SpringDamper sd)                                                                       //takes in sd value
        {                                                                                                                     //used for line renderers
            var drawerGo = Instantiate(PrefabDamper, (sd.P1.Position + sd.P2.Position) / 2f, new Quaternion()) as GameObject; //instantiate game object

            if (drawerGo == null)
            {
                return(null);
            }
            var lr = drawerGo.GetComponent <LineRenderer>();

            lr.materials[0].color = Color.black; //the color of the line renderers. //will be black
            lr.SetWidth(.1f, .1f);
            return(drawerGo);
        }
示例#4
0
        public void SetSpringDampers()
        {
            foreach (var mp in Monoparticles)
            {
                var index = FindIndex(Monoparticles, mp);
                mp.Particle.Particles = new List <Particle>();

                if ((index + 1) % Width > index % Width)
                { //creates a horizontal line
                    mp.Particle.Particles.Add(Monoparticles[index + 1].Particle);
                    var sd = new SpringDamper(mp.Particle, Monoparticles[index + 1].Particle, SpringConstant, DampingFactor, RestLength);
                    Drawers.Add(CreateDrawer(sd));
                    SpringDampers.Add(sd);
                }

                if (index + Width < Monoparticles.Count)
                { //creates a vertical line
                    mp.Particle.Particles.Add(Monoparticles[index + Width].Particle);
                    var sd = new SpringDamper(mp.Particle, Monoparticles[index + Width].Particle, SpringConstant, DampingFactor, RestLength);
                    Drawers.Add(CreateDrawer(sd));
                    SpringDampers.Add(sd);
                }

                if (index + Width - 1 < Monoparticles.Count && index - 1 >= 0 && (index - 1) % Width < index % Width)
                { //creates a diagonal line (top left)
                    mp.Particle.Particles.Add(Monoparticles[index + Width - 1].Particle);
                    var sd = new SpringDamper(mp.Particle, Monoparticles[index + Width - 1].Particle, SpringConstant, DampingFactor, RestLength);
                    Drawers.Add(CreateDrawer(sd));
                    SpringDampers.Add(sd);
                }

                if (index + Width + 1 >= Monoparticles.Count || (index + 1) % Width <= index % Width)
                {
                    continue;
                }
                {
                    //creates a diagonal line (top right)
                    mp.Particle.Particles.Add(Monoparticles[index + Width + 1].Particle);
                    var sd = new SpringDamper(mp.Particle, Monoparticles[index + Width + 1].Particle, SpringConstant, DampingFactor, RestLength);
                    Drawers.Add(CreateDrawer(sd));
                    SpringDampers.Add(sd);
                }
            }
        }