Constraint which tries to maintain the distance between points on two entities.
Inheritance: BEPUphysics.Constraints.TwoEntity.Joints.Joint, I1DImpulseConstraintWithError, I1DJacobianConstraint
        /// <summary>
        /// Constructs a new demo.
        /// </summary>
        /// <param name="game">Game owning this demo.</param>
        public ClothJointLatticeDemo(DemosGame game)
            : base(game)
        {
            //Joints can also act like springs by modifying their springSettings.
            //Though using a bunch of DistanceJoint objects can be slower than just applying direct spring forces,
            //it is significantly more stable and allows rigid structures.
            //The extra stability can make it useful for cloth-like simulations.
            Entity latticePiece;
            DistanceJoint distanceJoint;

            int numColumns = 16;
            int numRows = 16;
            float xSpacing = 3.09f;
            float zSpacing = 3.09f;
            var lattice = new Entity[numRows,numColumns];
            for (int i = 0; i < numRows; i++)
                for (int j = 0; j < numColumns; j++)
                {
                    latticePiece = new Sphere(
                        new Vector3(
                            xSpacing * i - (numRows - 1) * xSpacing / 2f,
                            15.58f,
                            2 + zSpacing * j - (numColumns - 1) * zSpacing / 2f),
                        .3f, 10);

                    latticePiece.LocalInertiaTensorInverse = new Matrix3x3();
                    latticePiece.Tag = "noDisplayObject"; //The joint lines are visible enough; don't add a sphere model for this sphere.
                    lattice[i, j] = latticePiece;

                    Space.Add(latticePiece);
                }
            //The joints composing the cloth can have their max iterations set independently from the solver iterations.
            //More iterations (up to the solver's own max) will increase the quality at the cost of speed.
            int clothIterations = 1;
            //So while the above prevents joints from using more than 1 iteration, setting the solver's iteration limit can lower the
            //rest of the solving load (collisions).
            Space.Solver.IterationLimit = 2;

            //Loop through the grid and set up the joints.
            for (int i = 0; i < numRows; i++)
                for (int j = 0; j < numColumns; j++)
                {
                    if (i + 1 < numRows)
                    {
                        distanceJoint = new DistanceJoint(lattice[i, j], lattice[i + 1, j], lattice[i, j].Position, lattice[i + 1, j].Position);
                        distanceJoint.SpringSettings.Stiffness = 1000;
                        distanceJoint.SpringSettings.Damping = 1000;
                        distanceJoint.SolverSettings.MaximumIterationCount = clothIterations;
                        Space.Add(distanceJoint);

                        if (j + 1 < numColumns)
                        {
                            distanceJoint = new DistanceJoint(lattice[i, j], lattice[i + 1, j + 1], lattice[i, j].Position, lattice[i + 1, j + 1].Position);
                            distanceJoint.SpringSettings.Stiffness = 1000;
                            distanceJoint.SpringSettings.Damping = 1000;
                            distanceJoint.SolverSettings.MaximumIterationCount = clothIterations;
                            Space.Add(distanceJoint);
                        }
                        if (j - 1 >= 0)
                        {
                            distanceJoint = new DistanceJoint(lattice[i, j], lattice[i + 1, j - 1], lattice[i, j].Position, lattice[i + 1, j - 1].Position);
                            distanceJoint.SpringSettings.Stiffness = 1000;
                            distanceJoint.SpringSettings.Damping = 1000;
                            distanceJoint.SolverSettings.MaximumIterationCount = clothIterations;
                            Space.Add(distanceJoint);
                        }
                    }
                    if (j + 1 < numColumns)
                    {
                        distanceJoint = new DistanceJoint(lattice[i, j], lattice[i, j + 1], lattice[i, j].Position, lattice[i, j + 1].Position);
                        distanceJoint.SpringSettings.Stiffness = 1000;
                        distanceJoint.SpringSettings.Damping = 1000;
                        distanceJoint.SolverSettings.MaximumIterationCount = clothIterations;
                        Space.Add(distanceJoint);
                    }
                }

      


            //Add some ground.
            Space.Add(new Sphere(new Vector3(0, 0, 0), 10));
            Space.Add(new Box(new Vector3(0, -3.5f, 0), 80f, 1, 80f));

            game.Camera.Position = new Vector3(0, 5, 25);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructs a new demo.
        /// </summary>
        /// <param name="game">Game owning this demo.</param>
        public EarthquakeDemo(DemosGame game)
            : base(game)
        {
            Entity ground = new Box(new Vector3(0, 0, 0), 50f, 1f, 50f);
            Space.Add(ground);
            //Springs: Create a lattice of springs to hold the boxes together.
            var platform = new Box(new Vector3(0, 4, 0), 18, 1, 18, 400);
            platform.Material.KineticFriction = .8f;

            Space.Add(platform);

            //Create a big mess of DistanceJoints representing springs.

            var distanceJoint = new DistanceJoint(ground, platform, new Vector3(9, .5f, -9), new Vector3(9, 3.5f, -9));
            distanceJoint.SpringSettings.Stiffness = 3000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(9, .5f, -9), new Vector3(-9, 3.5f, -9));
            distanceJoint.SpringSettings.Stiffness = 3000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(-9, .5f, -9), new Vector3(9, 3.5f, -9));
            distanceJoint.SpringSettings.Stiffness = 3000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(-9, .5f, -9), new Vector3(-9, 3.5f, -9));
            distanceJoint.SpringSettings.Stiffness = 3000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);

            distanceJoint = new DistanceJoint(ground, platform, new Vector3(9, .5f, 9), new Vector3(9, 3.5f, 9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(9, .5f, 9), new Vector3(-9, 3.5f, 9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(-9, .5f, 9), new Vector3(9, 3.5f, 9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(-9, .5f, 9), new Vector3(-9, 3.5f, 9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);

            distanceJoint = new DistanceJoint(ground, platform, new Vector3(9, .5f, -9), new Vector3(9, 3.5f, -9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(9, .5f, -9), new Vector3(9, 3.5f, 9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(9, .5f, 9), new Vector3(9, 3.5f, -9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(9, .5f, 9), new Vector3(9, 3.5f, 9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);

            distanceJoint = new DistanceJoint(ground, platform, new Vector3(-9, .5f, -9), new Vector3(-9, 3.5f, -9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(-9, .5f, -9), new Vector3(-9, 3.5f, 9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(-9, .5f, 9), new Vector3(-9, 3.5f, -9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);
            distanceJoint = new DistanceJoint(ground, platform, new Vector3(-9, .5f, 9), new Vector3(-9, 3.5f, 9));
            distanceJoint.SpringSettings.Stiffness = 6000;
            distanceJoint.SpringSettings.Damping = 0;
            Space.Add(distanceJoint);

            int numBlocksTall = 10; //How many 'stories' tall.
            float blockWidth = 4; //Total width/length of the tower.
            float blockHeight = 1.333f;
            Entity toAdd;
            for (int i = 0; i < numBlocksTall; i++)
            {
                if (i % 2 == 0)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        toAdd =
                            new Box(
                                new Vector3(
                                    j * blockWidth / 3 - blockWidth / 3f,
                                    5 + blockHeight / 2 + i * blockHeight,
                                    0),
                                blockWidth / 3, blockHeight, blockWidth, 3 * numBlocksTall + 1 - 2 * i);
                        Space.Add(toAdd);
                        toAdd.Material = new Material(.8f, .8f, 0);
                    }
                }
                else
                {
                    for (int j = 0; j < 3; j++)
                    {
                        toAdd =
                            new Box(
                                new Vector3(0,
                                            5 + blockHeight / 2 + i * blockHeight,
                                            j * blockWidth / 3 - blockWidth / 3f),
                                blockWidth, blockHeight, blockWidth / 3, 3 * numBlocksTall + 1 - 2 * i);
                        Space.Add(toAdd);
                        toAdd.Material = new Material(.8f, .8f, 0);
                    }
                }
            }

            game.Camera.Position = new Vector3(0, 7, 30);
        }
Exemplo n.º 3
0
        public override void InitializeComponents()
        {
            Component shade;
            Light shadeLight;

            var toplink = new Component(Position, Quaternion.Identity, scalevec / 32.0f);
            toplink.Mesh = new LPPMesh(segmentModel);
            toplink.SetCollision(segmentPhys, true);

            Components.Add(toplink);

            Vector3 yVec = new Vector3(0.0f, scalevec.Y, 0.0f);

            var lastlink = toplink;

            for (int i = 0; i < numSegments; i++)
            {
                var link = new Component(Position - (yVec * (float)i * linklength), Quaternion.Identity, scalevec / 32.0f);
                link.Mesh = new LPPMesh(segmentModel);
                link.SetCollision(segmentPhys, false);
                CollisionRules.AddRule(lastlink.Entity, link.Entity, CollisionRule.NoBroadPhase);
                var joint = new DistanceJoint(lastlink.Entity, link.Entity, link.Position, link.Position);

                joint.SpringSettings.Advanced.UseAdvancedSettings = true;
                joint.SpringSettings.Advanced.ErrorReductionFactor = 0.99f;
                joint.SpringSettings.Advanced.Softness = 0.1f;

                Components.Add(link);
                Components.Add(new DummyComponent(joint));
                lastlink = link;
            }

            shade = new Component(Position - (yVec * ((numSegments * linklength) + 0.25f)), Quaternion.Identity, scalevec / 32.0f);
            shade.Mesh = new LPPMesh(lampModel);

            shadeLight = new Light();
            shadeLight.LocalTransform = Matrix.CreateTranslation(yVec * -0.3f);
            shadeLight.Shadow = true;
            shadeLight.LightType = "BakedShadow";
            shadeLight.Radius = 12.5f;
            shadeLight.Intensity = 0.75f;
            shadeLight.Color = lightColor;
            PhysicsInfo PI = new PhysicsInfo(lampPhys);
            PI.Density /= scalevec.Length() / Vector3.One.Length();
            shade.SetCollision(PI, false);
            shade.Lights.Add(shadeLight);
            shade.Entity.Tag = new EntityTag(this, shade);

            shadeLight = new Light();
            shadeLight.Color = lightColor;
            shadeLight.LocalTransform = Matrix.CreateTranslation(yVec);
            shadeLight.Radius = 11.9f;
            shadeLight.Intensity = 0.75f;
            shadeLight.Shadow = false;
            shade.Lights.Add(shadeLight);

            shadeLight = new Light();
            shadeLight.Intensity = 1.0f;
            shadeLight.Radius = 0.5f;
            shade.Lights.Add(shadeLight);
            Components.Add(shade);

            CollisionRules.AddRule(shade.Entity, lastlink.Entity, CollisionRule.NoBroadPhase);
            var joint1 = new DistanceJoint(lastlink.Entity, shade.Entity, shade.Position, lastlink.Position);
            joint1.SpringSettings.DampingConstant = 1000000f;
            joint1.SpringSettings.StiffnessConstant = 600000f;
            var angularMotor1 = new AngularMotor(lastlink.Entity, shade.Entity);
            angularMotor1.Settings.MaximumForce = 50f;
            Components.Add(new DummyComponent(joint1));
            Components.Add(new DummyComponent(angularMotor1));
            base.InitializeComponents();
        }