A distance joint rains two points on two bodies to remain at a fixed distance from each other. You can view this as a massless, rigid rod.
Inheritance: Joint
Ejemplo n.º 1
0
 public static FixedDistanceJoint CreateFixedDistanceJoint(World world, Body body, Vector2 localAnchor,
                                                           Vector2 worldAnchor)
 {
     FixedDistanceJoint distanceJoint = new FixedDistanceJoint(body, localAnchor, worldAnchor);
     world.AddJoint(distanceJoint);
     return distanceJoint;
 }
Ejemplo n.º 2
0
        private WebTest()
        {
            World.JointRemoved += JointRemovedFired;
            World.BodyRemoved += BodyRemovedFired;

            {
                PolygonShape shape = new PolygonShape(5);
                shape.SetAsBox(0.5f, 0.5f);

                _bodies[0] = BodyFactory.CreateBody(World);
                _bodies[0].BodyType = BodyType.Dynamic;
                _bodies[0].Position = new Vector2(-5.0f, 5.0f);

                _bodies[0].CreateFixture(shape);

                _bodies[1] = BodyFactory.CreateBody(World);
                _bodies[1].BodyType = BodyType.Dynamic;
                _bodies[1].Position = new Vector2(5.0f, 5.0f);

                _bodies[1].CreateFixture(shape);

                _bodies[2] = BodyFactory.CreateBody(World);
                _bodies[2].BodyType = BodyType.Dynamic;
                _bodies[2].Position = new Vector2(5.0f, 15.0f);

                _bodies[2].CreateFixture(shape);

                _bodies[3] = BodyFactory.CreateBody(World);
                _bodies[3].BodyType = BodyType.Dynamic;
                _bodies[3].Position = new Vector2(-5.0f, 15.0f);

                _bodies[3].CreateFixture(shape);

                FixedDistanceJoint dj = new FixedDistanceJoint(_bodies[0], new Vector2(-0.5f, -0.5f),
                                                               new Vector2(-10.0f, 0.0f));
                _joints[0] = dj;
                dj.Frequency = 2.0f;
                dj.DampingRatio = 0.0f;
                World.AddJoint(_joints[0]);

                FixedDistanceJoint dj1 = new FixedDistanceJoint(_bodies[1], new Vector2(0.5f, -0.5f),
                                                                new Vector2(10.0f, 0.0f));
                _joints[1] = dj1;
                dj1.Frequency = 2.0f;
                dj1.DampingRatio = 0.0f;
                World.AddJoint(_joints[1]);

                FixedDistanceJoint dj2 = new FixedDistanceJoint(_bodies[2], new Vector2(0.5f, 0.5f),
                                                                new Vector2(10.0f, 20.0f));
                _joints[2] = dj2;
                dj2.Frequency = 2.0f;
                dj2.DampingRatio = 0.0f;
                World.AddJoint(_joints[2]);

                FixedDistanceJoint dj3 = new FixedDistanceJoint(_bodies[3], new Vector2(-0.5f, 0.5f),
                                                                new Vector2(-10.0f, 20.0f));
                _joints[3] = dj3;
                dj3.Frequency = 2.0f;
                dj3.DampingRatio = 0.0f;
                World.AddJoint(_joints[3]);

                DistanceJoint dj4 = new DistanceJoint(_bodies[0], _bodies[1], Vector2.Zero,
                                                      Vector2.Zero);
                _joints[4] = dj4;
                dj4.Frequency = 2.0f;
                dj4.DampingRatio = 0.0f;
                World.AddJoint(_joints[4]);

                DistanceJoint dj5 = new DistanceJoint(_bodies[1], _bodies[2], Vector2.Zero,
                                                      Vector2.Zero);
                _joints[5] = dj5;
                dj5.Frequency = 2.0f;
                dj5.DampingRatio = 0.0f;
                World.AddJoint(_joints[5]);

                DistanceJoint dj6 = new DistanceJoint(_bodies[2], _bodies[3], Vector2.Zero,
                                                      Vector2.Zero);
                _joints[6] = dj6;
                dj6.Frequency = 2.0f;
                dj6.DampingRatio = 0.0f;
                World.AddJoint(_joints[6]);

                DistanceJoint dj7 = new DistanceJoint(_bodies[3], _bodies[0], Vector2.Zero,
                                                      Vector2.Zero);
                _joints[7] = dj7;
                dj7.Frequency = 2.0f;
                dj7.DampingRatio = 0.0f;
                World.AddJoint(_joints[7]);
            }
        }
        public static Joint CopyJoint(Joint joint, Body bodyA, Body bodyB, World world)
        {
            Joint newJoint = null;

            switch (joint.JointType)
            {
                case JointType.Angle:
                    newJoint = JointFactory.CreateAngleJoint(world, bodyA, bodyB);
                    break;
                case JointType.Distance:
                    newJoint = new DistanceJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter);
                    break;
                case JointType.FixedAngle:
                    newJoint = new FixedAngleJoint(bodyA);
                    break;
                case JointType.FixedDistance:
                    newJoint = new FixedDistanceJoint(bodyA, bodyA.WorldCenter, Vector2.Zero);
                    break;
                case JointType.FixedFriction:
                    newJoint = new FixedFrictionJoint(bodyA, bodyA.WorldCenter);
                    break;
                case JointType.FixedPrismatic:
                    var fpJoint = joint as FixedPrismaticJoint;
                    var fpAxis = fpJoint.LocalXAxis1;
                    newJoint = new FixedPrismaticJoint(bodyA, bodyA.WorldCenter, fpAxis);
                    break;
                case JointType.FixedRevolute:
                    newJoint = new FixedRevoluteJoint(bodyA, bodyA.WorldCenter, Vector2.Zero);
                    break;
                case JointType.Friction:
                    newJoint = new FrictionJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter);
                    break;
                case JointType.Line:
                    var lineJoint = joint as LineJoint;
                    var axis = lineJoint.LocalXAxis;
                    newJoint = new LineJoint(bodyA, bodyB, bodyA.WorldCenter, axis);
                    break;
                case JointType.Prismatic:
                    var pJoint = joint as PrismaticJoint;
                    var pAxis = pJoint.LocalXAxis1;
                    newJoint = new PrismaticJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter, pAxis);
                    ((PrismaticJoint)newJoint).LimitEnabled = pJoint.LimitEnabled;
                    ((PrismaticJoint)newJoint).MotorEnabled = pJoint.MotorEnabled;
                    ((PrismaticJoint)newJoint).MaxMotorForce = pJoint.MaxMotorForce;
                    ((PrismaticJoint)newJoint).MotorSpeed = pJoint.MotorSpeed;
                    ((PrismaticJoint)newJoint).LowerLimit = pJoint.LowerLimit;
                    ((PrismaticJoint)newJoint).UpperLimit = pJoint.UpperLimit;
                    ((PrismaticJoint)newJoint).ReferenceAngle = pJoint.ReferenceAngle;
                    ((PrismaticJoint)newJoint).LocalXAxis1 = pJoint.LocalXAxis1;
                    break;
                case JointType.Pulley:
                    var pulleyJoint = joint as PulleyJoint;
                    var ratio = pulleyJoint.Ratio;
                    newJoint = new PulleyJoint(bodyA, bodyB, Vector2.Zero, Vector2.Zero, bodyA.WorldCenter, bodyB.WorldCenter, ratio);
                    break;
                case JointType.Revolute:
                    newJoint = new RevoluteJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter);
                    break;
                case JointType.Slider:
                    var sliderJoint = joint as SliderJoint;
                    var minLength = sliderJoint.MinLength;
                    var maxLength = sliderJoint.MaxLength;
                    newJoint = new SliderJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter, minLength, maxLength);
                    break;
                case JointType.Weld:
                    newJoint = new WeldJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter);
                    break;
            }

            var data = new FarseerJointUserData();
            data.BodyAName = ((FarseerJointUserData) joint.UserData).BodyAName;
            data.BodyBName = ((FarseerJointUserData) joint.UserData).BodyBName;

            joint.UserData = data;

            return newJoint;
        }