Esempio n. 1
0
        Particle MakeLimb(Composite composite, Particle baseParticle, float branchLength, float angle)
        {
            Particle         p = new Particle(baseParticle.pos + AngleToVector2(angle) * branchLength);
            SpringConstraint s = new SpringConstraint(baseParticle, p, springStiffness);

            return(p);
        }
Esempio n. 2
0
        Composite MakeSpinalComposite(float length, int boneNum, Vector2 rootPosition)
        {
            if (boneNum <= 1)
            {
                throw new System.ArgumentException("boneNUmは\"2\"以上に設定してください。");
            }
            float boneLength = length / (boneNum + 1);

            var        composite       = new Composite();
            List <int> particleIndices = new List <int>();
            List <int> springIndices   = new List <int>();

            var tp = new Particle(rootPosition);

            particleIndices.Add(composite.elemNum);
            composite.AddSimElement(tp, 0);

            for (int i = 1; i < boneNum; ++i)
            {
                var p = new Particle(rootPosition + new Vector2(0f, -boneLength * i));
                particleIndices.Add(composite.elemNum);
                composite.AddSimElement(p, 0);

                var s = new SpringConstraint(tp, p);
                springIndices.Add(composite.elemNum);
                composite.AddSimElement(s, 1);

                tp = p;
            }

            composite.AddRenderingGroup(new SimRenderer.SimRenderingGroup(1, springIndices));
            composite.AddRenderingGroup(new SimRenderer.SimRenderingGroup(0, particleIndices));

            return(composite);
        }
Esempio n. 3
0
        void MakePlant(Vector2 basePosition, int joints, float baseAngle)
        {
            Particle root = new Particle(basePosition);

            particleIndices.Add(composite.elemNum);
            composite.AddSimElement(root);

            Particle      prev     = root;
            Particle      prev2    = root;
            Particle      current  = root;
            PinConstraint firstPin = null;
            float         angle    = baseAngle;

            for (var i = 0; i <= joints; ++i)
            {
                angle += angleRange.random * Mathf.Deg2Rad;
                if (i == joints)
                {
                    Particle leaf = new Particle(prev.pos + AngleToVector2(angle) * branchLengthRange.random);
                    particleIndices.Add(composite.elemNum);
                    composite.AddSimElement(leaf);

                    SpringConstraint leafSpring = new SpringConstraint(prev, leaf, springStiffness);
                    leafSpringIndices.Add(composite.elemNum);
                    composite.AddSimElement(leafSpring);

                    current = leaf;
                }
                else
                {
                    Particle joint = new Particle(prev.pos + AngleToVector2(angle) * branchLengthRange.random);
                    particleIndices.Add(composite.elemNum);
                    composite.AddSimElement(joint);

                    SpringConstraint spring = new SpringConstraint(prev, joint, springStiffness);
                    branchSpringIndices.Add(composite.elemNum);
                    composite.AddSimElement(spring);

                    current = joint;

                    if (i == 0)
                    {
                        firstPin = new PinConstraint(joint);
                    }
                }
                if (i > 0)
                {
                    AngleConstraint ac = new AngleConstraint(prev2, current, prev, angleStiffness);
                    composite.AddSimElement(ac);
                }
                prev2 = prev;
                prev  = current;
            }

            PinConstraint rootPin = new PinConstraint(root);

            composite.AddSimElement(rootPin);

            composite.AddSimElement(firstPin);
        }
        public void Update(float deltaTime)
        {
            Input();
            mSimulator.Update(deltaTime);


            for (int i = 0; i < mCircleZones.Count; i++)
            {
                mCircleZones[i].MoveTo(mSimulator.GetPositionOfNode(i));
            }

            mCentroids.Clear();

            for (int i = 0; i < mTriangleMidPoints.Count; i++)
            {
                mTriangleMidPoints[i].CalculateMidPoint();
                mCentroids.Add(mTriangleMidPoints[i].GetMidPoint());
            }



            if (Keyboard.IsKeyPressed(Keyboard.Key.Space))
            {
                SpringConstraint spring = (SpringConstraint)mSpringConstraints[rnd.Next(mSpringConstraints.Count)];
                spring.mRestLength += -5 + rnd.Next(10);
            }
        }
Esempio n. 5
0
        /*
         * Methods
         */

        public override SimElement MakeSimElement(AlignedEditableForm aef, List <SimElement> simElements)
        {
            var a = simElements[aef.uid2idxDic[_aUID]] as Particle;
            var b = simElements[aef.uid2idxDic[_bUID]] as Particle;

            var sc = new SpringConstraint(a, b, _stiffness);

            sc.OverrideUID(uid);

            return(sc);
        }
Esempio n. 6
0
        public void Undo()
        {
            var current = _sim.uidDistributer.current;

            _s = _sim.MakeSpringByUID(_aUID, _bUID, _stiffness);
            _s.OverrideUID(_uid);

            _sim.uidDistributer.SetCounter(current);

            _marker.MakeSpringMarker(_s);
        }
Esempio n. 7
0
        public DeleteSpringCommand(Simulator sim, MarkerManager marker, int uid)
        {
            _sim    = sim;
            _marker = marker;
            _uid    = uid;

            _s         = _sim.GetSpringByUID(uid);
            _aUID      = _s.a.uid;
            _bUID      = _s.b.uid;
            _stiffness = _s.stiffness;
        }
Esempio n. 8
0
        /*
         * Spring related
         */

        public void MakeSpringMarker(SpringConstraint s)
        {
            Vector3 pos = s.middlePos;

            pos.z = springMarkerDepth;

            var marker = MakeMarker(SPRING_ID, s, pos);

            marker.transform.rotation   = Quaternion.AngleAxis(s.a2bRadian * Mathf.Rad2Deg, Vector3.forward);
            marker.transform.localScale = new Vector3(s.currentLength, 0.4f, 1f);
        }
Esempio n. 9
0
        public bool Do()
        {
            if (_uid == -1)
            {
                _s   = _sim.MakeSpringByUID(_aUID, _bUID, _stiffness);
                _uid = _s.uid;
            }
            else
            {
                var current = _sim.uidDistributer.current;

                _s = _sim.MakeSpringByUID(_aUID, _bUID, _stiffness);
                _s.OverrideUID(_uid);

                _sim.uidDistributer.SetCounter(current);
            }
            _marker.MakeSpringMarker(_s);
            return(true);
        }
Esempio n. 10
0
        Particle MakeBranch(Particle baseParticle, int depth, float branchLength, float angle, float angleStiffness)
        {
            Particle p = new Particle(baseParticle.pos + AngleToVector2(angle) * branchLength);

            if (depth > 0)
            {
                branchParticleIndices.Add(composite.elemNum);
            }
            composite.AddSimElement(p);

            SpringConstraint s = new SpringConstraint(baseParticle, p, springStiffness);

            if (depth > 0)
            {
                springIndices.Add(composite.elemNum);
            }
            else
            {
                leafSpringIndices.Add(composite.elemNum);
            }
            composite.AddSimElement(s);

            if (depth > 0)
            {
                Particle        lp  = MakeBranch(p, depth - 1, branchLength, angle + Mathf.PI * 0.1f, angleStiffness * angleSoftness);
                AngleConstraint lac = new AngleConstraint(baseParticle, lp, p, angleStiffness);
                composite.AddSimElement(lac);

                Particle        rp  = MakeBranch(p, depth - 1, branchLength, angle - Mathf.PI * 0.1f, angleStiffness * angleSoftness);
                AngleConstraint rac = new AngleConstraint(baseParticle, rp, p, angleStiffness);
                composite.AddSimElement(rac);

                AngleConstraint cac = new AngleConstraint(lp, rp, p, angleStiffness);
                composite.AddSimElement(cac);
            }
            return(p);
        }
Esempio n. 11
0
        Composite MakeClothComposite(float rowSize, int row, float colSize, int col, float particleDamping, float springStiffness, Vector2 gravity, Vector2 rootPosition, Vector2 offset)
        {
            float rowCellSize = rowSize / row;
            float colCellSize = colSize / col;

            Particle[] rowParticles = new Particle[row];

            var        composite       = new Composite();
            List <int> particleIndices = new List <int>();
            List <int> springIndices   = new List <int>();

            for (var y = 0; y < col; ++y)
            {
                if (y == 0)
                {
                    for (var x = 0; x < row; ++x)
                    {
                        var p = new Particle(rootPosition + new Vector2(rowCellSize * x, -colCellSize * y) + offset, particleDamping);
                        particleIndices.Add(composite.elemNum);
                        composite.AddSimElement(p, 0);

                        var pc = new PinConstraint(p);
                        composite.AddSimElement(p, 10);

                        if (x != 0)
                        {
                            var s = new SpringConstraint(rowParticles[x - 1], p, springStiffness);
                            // var s = new DistanceConstraint(rowParticles[x - 1], p);
                            springIndices.Add(composite.elemNum);
                            composite.AddSimElement(s, 1);
                        }

                        rowParticles[x] = p;
                    }
                }
                else
                {
                    for (var x = 0; x < row; ++x)
                    {
                        var p = new Particle(rootPosition + new Vector2(rowCellSize * x, -colCellSize * y) + offset, particleDamping);
                        particleIndices.Add(composite.elemNum);
                        composite.AddSimElement(p, 0);

                        var cfc = new ConstantForceConstraint(p, gravity);
                        composite.AddSimElement(cfc, 5);

                        var s = new SpringConstraint(rowParticles[x], p, springStiffness);
                        // var s = new DistanceConstraint(rowParticles[x], p);
                        springIndices.Add(composite.elemNum);
                        composite.AddSimElement(s, 1);

                        if (x != 0)
                        {
                            s = new SpringConstraint(rowParticles[x - 1], p, springStiffness);
                            // s = new DistanceConstraint(rowParticles[x - 1], p);
                            springIndices.Add(composite.elemNum);
                            composite.AddSimElement(s, 1);
                        }

                        rowParticles[x] = p;
                    }
                }
            }

            composite.AddRenderingGroup(new SimRenderer.SimRenderingGroup(1, springIndices));
            composite.AddRenderingGroup(new SimRenderer.SimRenderingGroup(0, particleIndices));

            return(composite);
        }
Esempio n. 12
0
        public TestScene(RenderWindow window)
        {
            mWindow                   = window;
            mSimulator                = new Simulator(mWindow, SOLVER_ITERATIONS);
            mCircleZones              = new List <CircleZone>(100);
            mSpringConstraints        = new List <AConstraint>(300);
            mTriangleMidPoints        = new List <TriangleMidpoint>(24);
            mNodes                    = new List <Node>(100);
            mCentroids                = new List <Vector2f>(100);
            mCurrentConnections       = new List <CurrentConnection>(200);
            mNetwork                  = new LinkedList <CurrentConnection>();
            mCentroidMarker           = new CircleShape(2);
            mCentroidMarker.FillColor = Color.Black;
            mCentroidMarker.Origin    = new Vector2f(1, 1);
            //Temp
            rnd = new Random();

            #region initZones

            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(400, 200)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(450, 200)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(500, 200)));

            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(375, 240)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(425, 240)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(475, 240)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(525, 240)));

            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(350, 280)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(400, 280)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(450, 280)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(500, 280)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(550, 280)));

            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(375, 320)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(425, 320)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(475, 320)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(525, 320)));

            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(400, 360)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(450, 360)));
            mCircleZones.Add(new CircleZone(mWindow, ZONE_INITIAL_RADIUS, new Vector2f(500, 360)));

            for (int i = 0; i < mCircleZones.Count; i++)
            {
                mCircleZones[i].SetNodeIndex(i);
                mSimulator.AddNode(i, mCircleZones[i].GetPosition(), 1);
                mNodes.Add(mSimulator.GetNode(i));
            }

            mSpringConstraints.Add(new SpringConstraint(mNodes[0], mNodes[1]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[1], mNodes[2]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[0], mNodes[3]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[0], mNodes[4]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[1], mNodes[4]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[5], mNodes[1]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[5], mNodes[2]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[6], mNodes[2]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[3], mNodes[4]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[4], mNodes[5]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[6], mNodes[5]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[7], mNodes[3]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[3], mNodes[8]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[7], mNodes[8]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[8], mNodes[4]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[9], mNodes[4]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[9], mNodes[5]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[8], mNodes[9]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[10], mNodes[5]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[9], mNodes[10]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[6], mNodes[10]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[6], mNodes[11]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[10], mNodes[11]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[7], mNodes[12]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[8], mNodes[12]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[8], mNodes[13]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[12], mNodes[13]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[9], mNodes[13]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[9], mNodes[14]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[13], mNodes[14]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[14], mNodes[10]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[15], mNodes[10]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[14], mNodes[15]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[15], mNodes[11]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[16], mNodes[12]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[16], mNodes[13]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[16], mNodes[17]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[14], mNodes[17]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[17], mNodes[18]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[15], mNodes[18]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[14], mNodes[18]));
            mSpringConstraints.Add(new SpringConstraint(mNodes[13], mNodes[17]));

            for (int i = 0; i < mSpringConstraints.Count; i++)
            {
                SpringConstraint spring = (SpringConstraint)mSpringConstraints[i];
                spring.mStiffness = (float)(1d - Math.Pow(1f - SPRING_STIFFNESS, 1f / 10f));
            }
            mSimulator.AddConstraints(mSpringConstraints);

            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[0], mCircleZones[4], mCircleZones[3]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[0], mCircleZones[1], mCircleZones[4]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[1], mCircleZones[4], mCircleZones[5]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[1], mCircleZones[5], mCircleZones[2]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[2], mCircleZones[5], mCircleZones[6]));

            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[3], mCircleZones[7], mCircleZones[8]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[3], mCircleZones[4], mCircleZones[8]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[4], mCircleZones[8], mCircleZones[9]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[4], mCircleZones[5], mCircleZones[9]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[5], mCircleZones[9], mCircleZones[10]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[5], mCircleZones[6], mCircleZones[10]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[6], mCircleZones[10], mCircleZones[11]));

            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[7], mCircleZones[8], mCircleZones[12]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[12], mCircleZones[8], mCircleZones[13]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[8], mCircleZones[13], mCircleZones[9]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[13], mCircleZones[9], mCircleZones[14]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[9], mCircleZones[10], mCircleZones[14]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[14], mCircleZones[10], mCircleZones[15]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[10], mCircleZones[11], mCircleZones[15]));

            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[12], mCircleZones[13], mCircleZones[16]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[16], mCircleZones[13], mCircleZones[17]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[13], mCircleZones[17], mCircleZones[14]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[17], mCircleZones[14], mCircleZones[18]));
            mTriangleMidPoints.Add(new TriangleMidpoint(mCircleZones[14], mCircleZones[15], mCircleZones[18]));

            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[0], mTriangleMidPoints[1]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[1], mTriangleMidPoints[2]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[2], mTriangleMidPoints[3]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[3], mTriangleMidPoints[4]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[0], mTriangleMidPoints[6]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[2], mTriangleMidPoints[8]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[10], mTriangleMidPoints[4]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[5], mTriangleMidPoints[6]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[6], mTriangleMidPoints[7]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[7], mTriangleMidPoints[8]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[9], mTriangleMidPoints[8]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[9], mTriangleMidPoints[10]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[10], mTriangleMidPoints[11]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[5], mTriangleMidPoints[12]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[14], mTriangleMidPoints[7]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[16], mTriangleMidPoints[9]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[18], mTriangleMidPoints[11]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[12], mTriangleMidPoints[13]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[13], mTriangleMidPoints[14]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[14], mTriangleMidPoints[15]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[15], mTriangleMidPoints[16]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[17], mTriangleMidPoints[16]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[17], mTriangleMidPoints[18]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[13], mTriangleMidPoints[19]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[15], mTriangleMidPoints[21]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[23], mTriangleMidPoints[17]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[19], mTriangleMidPoints[20]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[20], mTriangleMidPoints[21]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[21], mTriangleMidPoints[22]));
            mCurrentConnections.Add(new CurrentConnection(mTriangleMidPoints[22], mTriangleMidPoints[23]));


            #endregion
        }
Esempio n. 13
0
 public void DeleteSpringMarker(SpringConstraint s)
 {
     DeleteMarker(SPRING_ID, s);
 }