Exemplo n.º 1
0
    /// <summary>
    /// Process a property for a pointConstraint node
    /// </summary>
    /// <param name="go">
    /// A <see cref="GameObject"/>
    /// </param>
    /// <param name="nodeName">
    /// A <see cref="System.String"/>
    /// </param>
    /// <param name="attributeName">
    /// A <see cref="System.String"/>
    /// </param>
    /// <param name="val">
    /// A <see cref="System.Object"/>
    /// </param>
    void ProcessPointConstraintProperty(GameObject go, string nodeName, string attributeName, System.Object val)
    {
        PointConstraint node = GetMayaNodeOnGameObject(nodeName, typeof(PointConstraint), go) as PointConstraint;

        node.constrainedObject = go.transform;
        switch (attributeName)
        {
        case "offset":
            node.offset = (Vector4)val;
            break;

        case "constraintOffsetPolarity":
            node.constraintOffsetPolarity = (float)val;
            break;

        default:
            if (IsConstraintWeightAttribute(attributeName))
            {
                node.InsertTargetUsingWeightAttribute(attributeName, (float)val);
            }
            if (IsConstraintTargetAttribute(attributeName))
            {
                node.InsertTargetUsingTargetAttribute(attributeName, val as string);
            }
            break;
        }
    }
        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;
            }
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        public void CreateScene(int sceneNumber)
        {
            _physics.Clear();
            _markers.Clear();

            Room room = new Room(this);

            _physics.Add(room);
            _physics.Gravity = new Vector3(0f, 0f, -9.8f);

            Model cubeModel     = this.Content.Load <Model>("models/small_cube");
            Model obeliskModel  = this.Content.Load <Model>("models/obelisk");
            Model sphereModel   = this.Content.Load <Model>("models/sphere");
            Model capsuleModel  = this.Content.Load <Model>("models/capsule");
            Model torusModel    = this.Content.Load <Model>("models/torus");
            Model slabModel     = this.Content.Load <Model>("models/slab");
            Model triangleModel = this.Content.Load <Model>("models/triangle");

            switch (sceneNumber)
            {
            case 1:
            {
                for (int i = 0; i < 12; i++)
                {
                    var cube = new SolidThing(this, cubeModel);
                    cube.SetWorld(new Vector3(0f, 0f, 0.25f + 0.51f * i));
                    _physics.Add(cube);
                }
            }
            break;

            case 2:
            {
                for (int i = 0; i < 7; i++)
                {
                    for (int j = 0; j < 7 - i; j++)
                    {
                        var cube = new SolidThing(this, cubeModel);
                        cube.SetWorld(new Vector3(0f, 0.501f * j + 0.25f * i, 0.5f + 0.55f * i));
                        _physics.Add(cube);
                    }
                }
            }
            break;

            case 3:
            {
                for (int i = 0; i < 6; i++)
                {
                    for (int j = 0; j < 6 - i; j++)
                    {
                        var cube = new SolidThing(this, cubeModel);
                        cube.SetWorld(new Vector3(0f, 2.2f * j + 1f * i - 4.1f, 0.75f * i + 0.25f));
                        _physics.Add(cube);
                    }
                }
                for (int i = 0; i < 6; i++)
                {
                    for (int j = 0; j < 5 - i; j++)
                    {
                        var plank = new SolidThing(this, obeliskModel);
                        plank.SetWorld(new Vector3(0f, 2.2f * j + 1f * i + 1f - 4f, 0.75f * i + 0.65f),
                                       Quaternion.CreateFromAxisAngle(Vector3.UnitZ, MathHelper.ToRadians(90f)));
                        _physics.Add(plank);
                    }
                }
            }
            break;

            case 4:
            {
                int size = 9;
#if WINDOWS
#else
                size = 4;
#endif
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size - i; j++)
                    {
                        for (int k = 0; k < size - i; k++)
                        {
                            var sphere = new SolidThing(this, sphereModel);
                            sphere.SetWorld(new Vector3(
                                                0.501f * j + 0.25f * i,
                                                0.501f * k + 0.25f * i,
                                                0.501f * i + 0.5f
                                                ), Quaternion.Identity);
                            _physics.Add(sphere);
                        }
                    }
                }
            }
            break;

            case 5:
            {
                var plank = new SolidThing(this, obeliskModel);
                plank.SetWorld(new Vector3(0.0f, 0.0f, 4.0f),
                               Quaternion.CreateFromAxisAngle(Vector3.UnitY, MathHelper.ToRadians(15f)));
                MassProperties immovableMassProperties = new MassProperties(float.PositiveInfinity, Matrix.Identity);
                plank.MassProperties = immovableMassProperties;
                _physics.Add(plank);

                var sphere = new SolidThing(this, sphereModel);
                sphere.SetWorld(new Vector3(-4.9f, 0.0f, 9.0f), Quaternion.Identity);
                _physics.Add(sphere);


//                        int size = 9;
//#if WINDOWS
//#else
//                        size = 4;
//#endif
//                        var models = new Model[] { cubeModel, sphereModel };
//                        for (int i = 0; i < size; i++)
//                        {
//                            for (int j = 0; j < size - i; j++)
//                            {
//                                for (int k = 0; k < size - i; k++)
//                                {
//                                    var sphere = new SolidThing(this, i % 2 == 0 ? sphereModel : cubeModel);
//                                    sphere.SetWorld(new Vector3(
//                                        0.501f * j + 0.25f * i,
//                                        0.501f * k + 0.25f * i,
//                                        0.501f * i + 0.5f
//                                        ), Quaternion.Identity);
//                                    _physics.Add(sphere);
//                                }
//                            }
//                        }
            }
            break;

            case 6:
            {
                int size = 9;
#if WINDOWS
#else
                size = 4;
#endif
                var models = new Model[] { cubeModel, sphereModel, capsuleModel };
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size - i; j++)
                    {
                        for (int k = 0; k < size - i; k++)
                        {
                            var sphere = new SolidThing(this, models[_rand.Next(3)]);
                            sphere.SetWorld(new Vector3(
                                                0.501f * j + 0.25f * i,
                                                0.501f * k + 0.25f * i,
                                                1f * i + 0.5f));
                            _physics.Add(sphere);
                        }
                    }
                }
            }
            break;

            case 7:
            {
                var o = new SolidThing(this, torusModel);
                o.SetWorld(new Vector3(0f, 0f, 0.5f), Quaternion.CreateFromAxisAngle(Vector3.UnitY, -MathHelper.PiOver2));
                _physics.Add(o);

                o = new SolidThing(this, torusModel);
                o.SetWorld(new Vector3(0f, 0f, 4f), Quaternion.CreateFromAxisAngle(Vector3.UnitY, -MathHelper.PiOver4));
                _physics.Add(o);

                o = new SolidThing(this, slabModel);
                o.SetWorld(new Vector3(-4f, 4f, 2f));
                _physics.Add(o);

                o = new SolidThing(this, this.Content.Load <Model>("models/cone"));
                o.SetWorld(new Vector3(-4f, -4f, 1f), Quaternion.CreateFromAxisAngle(Vector3.UnitZ, MathHelper.PiOver2));
                _physics.Add(o);

                o = new SolidThing(this, cubeModel);
                o.SetWorld(new Vector3(-4f, 6.1f, 3f));
                _physics.Add(o);
            }
            break;

            case 8:
            {
                RigidBody oLast = null;
                for (int i = 0; i < 10; i++)
                {
                    var o = new SolidThing(this, capsuleModel);
                    o.SetWorld(new Vector3(0f, 0f, 9.5f - i));
                    _physics.Add(o);
                    if (i == 0)
                    {
                        var j = new PointConstraint(o, room, new Vector3(0f, 0f, 10f));
                        j.IsCollisionEnabled = false;
                        _physics.Add(j);
                    }
                    else
                    {
                        var j = new PointConstraint(oLast, o, new Vector3(0f, 0f, 10f - (float)i));
                        j.IsCollisionEnabled = false;
                        _physics.Add(j);
                    }
                    oLast = o;
                }

                var a = new SolidThing(this, cubeModel);
                a.SetWorld(new Vector3(1f, 0f, 0.25f));
                _physics.Add(a);
                var b = new SolidThing(this, cubeModel);
                b.SetWorld(new Vector3(1f, 0f, 0.75f));
                _physics.Add(b);
                var j2 = new RevoluteJoint(b, a, new Vector3(1.25f, 0f, 0.5f), Vector3.UnitY,
                                           0f, MathHelper.PiOver2);
                j2.IsCollisionEnabled = false;
                _physics.Add(j2);

                a = new SolidThing(this, cubeModel);
                a.SetWorld(new Vector3(1f, 1f, 0.25f));
                _physics.Add(a);
                b = new SolidThing(this, cubeModel);
                b.SetWorld(new Vector3(1f, 1f, 0.75f));
                _physics.Add(b);
                var j4 = new GenericConstraint(b, a, new Frame(new Vector3(1f, 1f, 0.5f)),
                                               Axes.All, Vector3.Zero, new Vector3(0f, 0f, 0.5f),
                                               Axes.All, Vector3.Zero, Vector3.Zero);
                j4.IsCollisionEnabled = false;
                _physics.Add(j4);

                a = new SolidThing(this, cubeModel);
                a.SetWorld(new Vector3(1f, 2f, 0.25f));
                _physics.Add(a);
                b = new SolidThing(this, cubeModel);
                b.SetWorld(new Vector3(1f, 2f, 0.75f));
                _physics.Add(b);
                var j5 = new GenericConstraint(b, a, new Frame(new Vector3(1f, 2f, 0.5f)),
                                               Axes.All, new Vector3(-0.125f, -0.125f, 0f), new Vector3(0.125f, 0.125f, 0f),
                                               Axes.All, Vector3.Zero, Vector3.Zero);
                j5.IsCollisionEnabled = false;
                _physics.Add(j5);

                a = new SolidThing(this, sphereModel);
                a.SetWorld(new Vector3(2f, 0f, 2f));
                _physics.Add(a);
                b = new SolidThing(this, sphereModel);
                b.SetWorld(new Vector3(2f, 0f, 1f));
                _physics.Add(b);
                var g1 = new SpringForce(a, b, Vector3.Zero, Vector3.Zero, 1f, 5f, 0.05f);
                _physics.Add(g1);
                var j3 = new WorldPointConstraint(a, new Vector3(2f, 0f, 2f));
                _physics.Add(j3);
            }
            break;

            case 9:
            {
                var a = new SolidThing(this, sphereModel);
                a.Skin.Remove(a.Skin[0]);
                a.Skin.Add(new SpherePart(new Sphere(Vector3.Zero, 0.25f)), new Material(0f, 0.5f));
                a.SetWorld(7.0f, new Vector3(0f, 0f, 5f), Quaternion.Identity);
                a.MassProperties = MassProperties.Immovable;
                _physics.Add(a);

                _physics.Add(new SingularityForce(new Vector3(0f, 0f, 5f), 1E12f));

                _physics.Gravity = Vector3.Zero;

                var b = new SolidThing(this, cubeModel);
                b.SetWorld(new Vector3(0f, 0f, 8f),
                           Quaternion.CreateFromAxisAngle(Vector3.UnitX, MathHelper.PiOver4 / 2.0f) *
                           Quaternion.CreateFromAxisAngle(Vector3.UnitY, MathHelper.PiOver4)
                           );
                _physics.Add(b);
            }
            break;

            default:
                break;
            }
        }
        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;
    }
        }
Exemplo n.º 6
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];
        }
    }
Exemplo n.º 7
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];
        }
    }
        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);
        }