예제 #1
0
        public static void BuildRope(int x, int y, int len, double e, double m, double r)
        {
            Particle prev = null;
            //Particle last = null;
            int X = x;

            for (int i = 0; x < X + len; i++)
            {
                Particle p = new Particle();
                World.AddParticle(p);
                p.Radius      = r;
                p.Position    = new Vector(x, y);
                p.Mass        = m;
                p.Restitution = e;
                if (prev != null)
                {
                    ParticleConnection c = new ParticleConnection(prev, p);
                    c.SpringConstant  = 300;
                    c.RestLength      = p.Radius * 1.3;
                    c.DampingConstant = 50;
                    World.Connections.Add(c);
                }
                prev = p;
                x   += (int)(2 * r);
            }
        }
예제 #2
0
        private void simForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (particle != null)
            {
                particle.Unmark();
            }
            if (line != null)
            {
                line.Unmark();
            }
            if (connection != null)
            {
                connection.Unmark();
            }

            a = new Vector(e.X, e.Y);

            particle = null;
            foreach (Particle p in World.Particles)
            {
                if ((p.Position - a).Length < (p.Radius / 2) + 1)
                {
                    particle = p;
                    particle.Mark();
                    break;
                }
            }

            if (true)
            {
                line = null;
                foreach (Line l in World.Lines)
                {
                    if (l.Distance(a) < 5)
                    {
                        line = l;
                        line.Mark();
                        break;
                    }
                }
            }

            if (true)
            {
                connection = null;
                foreach (ParticleConnection c in World.Connections)
                {
                    Line temp = new Line(c.Particle1.Position, c.Particle2.Position);
                    if (temp.Distance(a) < 5)
                    {
                        connection = c;
                        connection.Mark();
                        break;
                    }
                }
            }
        }
예제 #3
0
        public static void BuildCircle(int x, int y, int r, int R)
        {
            Particle mid = new Particle();

            mid.Position = new Vector(x, y);
            mid.Radius   = R;
            World.AddParticle(mid);

            Particle prev  = null;
            Particle first = null;
            Vector   rv    = new Vector(r, 0);

            int k = (int)((2 * 3.14 * r) / (mid.Radius * 3));

            k += 1;
            for (int i = 0; i < k; i++)
            {
                Particle p = new Particle();
                p.Radius = R;
                p.Mass   = 1;
                if (first == null)
                {
                    first = p;
                }
                p.Position = new Vector(x + rv.X, y + rv.Y);
                Vector n = rv.Orthogonal();
                n.Normalize();
                n  *= (p.Radius * 3);
                rv += n;
                rv.Normalize();
                rv *= r;
                World.AddParticle(p);
                ParticleConnection cm = new ParticleConnection(mid, p);
                //cm.RestLength = r - r / 8;
                cm.SpringConstant = 200;
                //cm.DampingConstant = 100;
                World.Connections.Add(cm);

                if (prev != null)
                {
                    ParticleConnection c = new ParticleConnection(prev, p);
                    c.RestLength     /= 1.1;
                    c.SpringConstant  = 300;
                    c.DampingConstant = 40;
                    World.Connections.Add(c);
                }
                prev = p;
            }
            ParticleConnection cc = new ParticleConnection(prev, first);

            cc.SpringConstant  = 300;
            cc.DampingConstant = 40;
            cc.RestLength     /= 1.1;
            //cc.RestLength = (prev.Position - first.Position).Length;
            World.Connections.Add(cc);
        }
예제 #4
0
        public static void BuildChain(int x, int y, int l, int r, int m, bool xory, bool pin)
        {
            Particle prev = null;
            Particle last = null;
            int      xx   = x;
            int      yy   = y;

            for (int i = 0; true; i++)
            {
                Particle p = new Particle();
                World.AddParticle(p);
                p.Radius   = r;
                p.Position = new Vector(x, y);
                p.Mass     = m;
                if (prev != null)
                {
                    ParticleConnection c = new ParticleConnection(prev, p);
                    c.SpringConstant = 2000;
                    c.RestLength    -= p.Radius * 2;
                    World.Connections.Add(c);
                }

                if (i == 0)
                {
                    p.SimulateOn = false;
                    p.Mass       = double.PositiveInfinity;
                }

                last = p;
                prev = p;
                if (xory)
                {
                    x += 4 * r;
                    if (x > xx + l)
                    {
                        break;
                    }
                }
                else
                {
                    y += 4 * r;
                    if (y > yy + l)
                    {
                        break;
                    }
                }
            }
            if (pin && last != null)
            {
                last.SimulateOn = false;
                last.Mass       = double.PositiveInfinity;
            }
        }
예제 #5
0
        public static void BuildSquare(int x, int y, int a, int b)
        {
            Particle p1 = new Particle();

            p1.Position = new Vector(x, y);
            Particle p2 = new Particle();

            p2.Position = new Vector(x + a, y);
            Particle p3 = new Particle();

            p3.Position = new Vector(x + a, y + b);
            Particle p4 = new Particle();

            p4.Position = new Vector(x, y + b);
            p1.Radius   = p2.Radius = p3.Radius = p4.Radius = 2;
            World.AddParticle(p1);
            World.AddParticle(p2);
            World.AddParticle(p3);
            World.AddParticle(p4);
            ParticleConnection c12 = new ParticleConnection(p1, p2);
            ParticleConnection c23 = new ParticleConnection(p2, p3);
            ParticleConnection c34 = new ParticleConnection(p3, p4);
            ParticleConnection c41 = new ParticleConnection(p4, p1);
            ParticleConnection c24 = new ParticleConnection(p2, p4);
            ParticleConnection c13 = new ParticleConnection(p1, p3);

            c12.RenderOn = true;
            c23.RenderOn = true;
            c34.RenderOn = true;
            c41.RenderOn = true;
            World.Connections.Add(c12);
            World.Connections.Add(c23);
            World.Connections.Add(c34);
            World.Connections.Add(c41);
            World.Connections.Add(c24);
            World.Connections.Add(c13);
        }
예제 #6
0
        private void simForm_MouseUp(object sender, MouseEventArgs e)
        {
            if (a == null)
            {
                return;
            }

            Vector b    = new Vector(e.X, e.Y);
            Line   line = null;

            if (this.mode == Keys.L)
            {
                line = new Line(a, b);
            }
            else
            {
                if ((a - b).Length > 40)
                {
                    line = new Segment(a, b);
                }
            }

            dv = b - a;
            if (particle != null)
            {
                Particle p2 = null;
                foreach (Particle p in World.Particles)
                {
                    if ((p.Position - b).Length < p.Radius * 4)
                    {
                        p2     = p;
                        p2.Pen = new Pen(Brushes.Green, 1.0f);
                        break;
                    }
                }
                if (p2 != null)
                {
                    if ((particle.Position - p2.Position).Length > 10)
                    {
                        ParticleConnection c = new ParticleConnection(particle, p2);
                        c.RestLength = dv.Length;
                        if (p2 != particle)
                        {
                            World.Connections.Add(c);
                        }
                    }
                }
                else
                {
                    particle.Position += dv;
                }
            }
            else
            {
                if (line != null && line.Length > 40)
                {
                    World.Lines.Add(line);
                }
            }
            Invalidate();
        }