Exemple #1
0
    public void Init()
    {
        if ( startShape != null )
            InitFromShape(startShape);
        else
        {
            if ( top == null || bottom == null )
                return;

            Vector3 p1 = top.position;
            Vector3 p2 = bottom.position;

            RopeLength = (p1 - p2).magnitude;

            if ( masses == null )
                masses = new List<MegaRopeMass>();

            transform.position = Vector3.zero;

            masses.Clear();
            float ms = Mass / (float)(points + 1);

            for ( int i = 0; i <= points; i++ )
            {
                float alpha = (float)i / (float)points;

                MegaRopeMass rm = new MegaRopeMass(ms, Vector3.Lerp(p1, p2, alpha));
                masses.Add(rm);
            }

            if ( springs == null )
                springs = new List<MegaRopeSpring>();

            springs.Clear();

            if ( constraints == null )
                constraints = new List<MegaRopeConstraint>();

            constraints.Clear();

            for ( int i = 0; i < masses.Count - 1; i++ )
            {
                MegaRopeSpring spr = new MegaRopeSpring(i, i + 1, spring, damp, this);
                springs.Add(spr);

                LengthConstraint lcon = new LengthConstraint(i, i + 1, spr.restlen);
                constraints.Add(lcon);
            }

            int gap = 2;
            for ( int i = 0; i < masses.Count - gap; i++ )
            {
                float alpha = (float)i / (float)masses.Count;
                MegaRopeSpring spr = new MegaRopeSpring(i, i + gap, stiffspring * stiffnessCrv.Evaluate(alpha), stiffdamp * stiffnessCrv.Evaluate(alpha), this);
                springs.Add(spr);

                LengthConstraint lcon = new LengthConstraint(i, i + gap, spr.restlen);
                constraints.Add(lcon);
            }

            // Apply fixed end constraints
            PointConstraint pcon = new PointConstraint(0, top.transform);
            constraints.Add(pcon);

            pcon = new PointConstraint(masses.Count - 1, bottom.transform);
            constraints.Add(pcon);

            PointConstraint1 pcon1 = new PointConstraint1();
            pcon1.p1 = 1;
            pcon1.off = new Vector3(0.0f, springs[0].restlen, 0.0f);
            pcon1.obj = top.transform;	//.position;
            constraints.Add(pcon1);
            endcon = pcon1;

            masspos = new Vector3[masses.Count + 2];

            for ( int i = 0; i < masses.Count; i++ )
                masspos[i + 1] = masses[i].pos;

            masspos[0] = masspos[1];
            masspos[masspos.Length - 1] = masspos[masspos.Length - 2];
        }
    }
        public void Rope()
        {
    RigidBody last = null;

    for (int i = 0; i < RopeSize; i++)
    {
        RigidBody body = new RigidBody(new BoxShape(JVector.One));
        body.Position = new JVector(i * 1.5f, 0.5f, 0);

        JVector jpos2 = body.Position;

        body.Position = jpos2;

        world.AddBody(body);
        body.Update();

        if (last != null)
        {
            JVector jpos3 = last.Position;

            JVector dif; JVector.Subtract(ref jpos2, ref jpos3, out dif);
            JVector.Multiply(ref dif, 0.5f, out dif);
            JVector.Subtract(ref jpos2, ref dif, out dif);

            Constraint cons = new PointConstraint(last, body, dif);
            world.AddConstraint(cons);
        }

        last = body;
    }
        }
        private void BuildScene()
        {
            // creating two boxShapes with different sizes
            Shape boxShape = new BoxShape(JVector.One);
            Shape groundShape = new BoxShape(new JVector(10, 1, 10));

            // create new instances of the rigid body class and pass 
            // the boxShapes to them
            RigidBody boxBody1 = new RigidBody(boxShape);
            RigidBody boxBody2 = new RigidBody(boxShape);

            boxBody1.Tag = Color.LightPink;
            boxBody2.Tag = Color.LightSkyBlue;

            RigidBody groundBody = new RigidBody(groundShape);

            groundBody.Tag = Color.LightGreen;

            // set the position of the box size=(1,1,1)
            // 2 and 5 units above the ground box size=(10,1,10)
            boxBody1.Position = new JVector(0, 4, 1.0f);
            boxBody2.Position = new JVector(0, 4, -1.0f);

            pointConstraint = new PointConstraint(
                boxBody1, boxBody2, new JVector(0, 4f, 0));

            // add a force to one body - so it's not that boring
            boxBody1.AddForce(JVector.One * 10);

            world.AddConstraint(pointConstraint);

            // make the body static, so it can't be moved
            groundBody.IsStatic = true;

            // add the bodies to the world.
            world.AddBody(boxBody1);
            world.AddBody(boxBody2);
            world.AddBody(groundBody);
        }