Пример #1
0
 public void Add(PhysicsJoint joint)
 {
     Debug.Assert(JointCount < _jointCapacity);
     _joints[JointCount++] = joint;
 }
Пример #2
0
        /// <summary>
        /// Requires two existing revolute or prismatic joints (any combination will work).
        /// The provided joints must attach a dynamic body to a static body.
        /// </summary>
        /// <param name="jointA">The first joint.</param>
        /// <param name="jointB">The second joint.</param>
        /// <param name="ratio">The ratio.</param>
        public GearJoint(PhysicsJoint jointA, PhysicsJoint jointB, float ratio)
            : base(jointA.BodyA, jointA.BodyB)
        {
            JointType = JointType.Gear;
            JointA = jointA;
            JointB = jointB;
            Ratio = ratio;

            JointType type1 = jointA.JointType;
            JointType type2 = jointB.JointType;

            // Make sure its the right kind of joint
            Debug.Assert(type1 == JointType.Revolute ||
                         type1 == JointType.Prismatic ||
                         type1 == JointType.FixedRevolute ||
                         type1 == JointType.FixedPrismatic);
            Debug.Assert(type2 == JointType.Revolute ||
                         type2 == JointType.Prismatic ||
                         type2 == JointType.FixedRevolute ||
                         type2 == JointType.FixedPrismatic);

            // In the case of a prismatic and revolute joint, the first body must be static.
            if (type1 == JointType.Revolute || type1 == JointType.Prismatic)
                Debug.Assert(jointA.BodyA.BodyType == BodyType.Static);
            if (type2 == JointType.Revolute || type2 == JointType.Prismatic)
                Debug.Assert(jointB.BodyA.BodyType == BodyType.Static);

            float coordinate1 = 0.0f, coordinate2 = 0.0f;

            switch (type1)
            {
                case JointType.Revolute:
                    BodyA = jointA.BodyB;
                    _revolute1 = (RevoluteJoint)jointA;
                    LocalAnchor1 = _revolute1.LocalAnchorB;
                    coordinate1 = _revolute1.JointAngle;
                    break;

                case JointType.Prismatic:
                    BodyA = jointA.BodyB;
                    _prismatic1 = (PrismaticJoint)jointA;
                    LocalAnchor1 = _prismatic1.LocalAnchorB;
                    coordinate1 = _prismatic1.JointTranslation;
                    break;

                case JointType.FixedRevolute:
                    BodyA = jointA.BodyA;
                    _fixedRevolute1 = (FixedRevoluteJoint)jointA;
                    LocalAnchor1 = _fixedRevolute1.LocalAnchorA;
                    coordinate1 = _fixedRevolute1.JointAngle;
                    break;

                case JointType.FixedPrismatic:
                    BodyA = jointA.BodyA;
                    _fixedPrismatic1 = (FixedPrismaticJoint)jointA;
                    LocalAnchor1 = _fixedPrismatic1.LocalAnchorA;
                    coordinate1 = _fixedPrismatic1.JointTranslation;
                    break;
            }

            switch (type2)
            {
                case JointType.Revolute:
                    BodyB = jointB.BodyB;
                    _revolute2 = (RevoluteJoint)jointB;
                    LocalAnchor2 = _revolute2.LocalAnchorB;
                    coordinate2 = _revolute2.JointAngle;
                    break;

                case JointType.Prismatic:
                    BodyB = jointB.BodyB;
                    _prismatic2 = (PrismaticJoint)jointB;
                    LocalAnchor2 = _prismatic2.LocalAnchorB;
                    coordinate2 = _prismatic2.JointTranslation;
                    break;

                case JointType.FixedRevolute:
                    BodyB = jointB.BodyA;
                    _fixedRevolute2 = (FixedRevoluteJoint)jointB;
                    LocalAnchor2 = _fixedRevolute2.LocalAnchorA;
                    coordinate2 = _fixedRevolute2.JointAngle;
                    break;

                case JointType.FixedPrismatic:
                    BodyB = jointB.BodyA;
                    _fixedPrismatic2 = (FixedPrismaticJoint)jointB;
                    LocalAnchor2 = _fixedPrismatic2.LocalAnchorA;
                    coordinate2 = _fixedPrismatic2.JointTranslation;
                    break;
            }

            _ant = coordinate1 + Ratio * coordinate2;
        }
Пример #3
0
 /// <summary>
 /// Destroy a joint. This may cause the connected bodies to begin colliding.
 /// </summary>
 /// <param name="joint">The joint.</param>
 public void RemoveJoint(PhysicsJoint joint)
 {
     RemoveJoint(joint, true);
 }
Пример #4
0
        /// <summary>
        /// Create a joint to constrain bodies together. This may cause the connected bodies to cease colliding.
        /// </summary>
        /// <param name="joint">The joint.</param>
        public void AddJoint(PhysicsJoint joint)
        {
            Debug.Assert(!_jointAddList.Contains(joint), "You are adding the same joint more than once.");

            if (!_jointAddList.Contains(joint))
                _jointAddList.Add(joint);
        }
Пример #5
0
        private void RemoveJoint(PhysicsJoint joint, bool doCheck)
        {
            if (doCheck)
            {
                Debug.Assert(!_jointRemoveList.Contains(joint),
                             "The joint is already marked for removal. You are removing the joint more than once.");
            }

            if (!_jointRemoveList.Contains(joint))
                _jointRemoveList.Add(joint);
        }
Пример #6
0
 public static GearJoint CreateGearJoint(PhysicsWorld world, PhysicsJoint jointA, PhysicsJoint jointB, float ratio)
 {
     GearJoint gearJoint = new GearJoint(jointA, jointB, ratio);
     world.AddJoint(gearJoint);
     return gearJoint;
 }
Пример #7
0
        private void SerializeJoint(PhysicsJoint joint)
        {
            if (joint.IsFixedType())
                return;

            _writer.WriteStartElement("Joint");

            _writer.WriteAttributeString("Type", joint.JointType.ToString());

            WriteElement("BodyA", FindBodyIndex(joint.BodyA));
            WriteElement("BodyB", FindBodyIndex(joint.BodyB));

            WriteElement("CollideConnected", joint.CollideConnected);

            WriteElement("Breakpoint", joint.Breakpoint);

            if (joint.UserData != null)
            {
                _writer.WriteStartElement("UserData");
                WriteDynamicType(joint.UserData.GetType(), joint.UserData);
                _writer.WriteEndElement();
            }

            switch (joint.JointType)
            {
                case JointType.Distance:
                    {
                        DistanceJoint djd = (DistanceJoint)joint;

                        WriteElement("DampingRatio", djd.DampingRatio);
                        WriteElement("FrequencyHz", djd.Frequency);
                        WriteElement("Length", djd.Length);
                        WriteElement("LocalAnchorA", djd.LocalAnchorA);
                        WriteElement("LocalAnchorB", djd.LocalAnchorB);
                    }
                    break;

                case JointType.Friction:
                    {
                        FrictionJoint fjd = (FrictionJoint)joint;
                        WriteElement("LocalAnchorA", fjd.LocalAnchorA);
                        WriteElement("LocalAnchorB", fjd.LocalAnchorB);
                        WriteElement("MaxForce", fjd.MaxForce);
                        WriteElement("MaxTorque", fjd.MaxTorque);
                    }
                    break;

                case JointType.Gear:
                    throw new Exception("Gear joint not supported by serialization");
                case JointType.Line:
                    {
                        LineJoint ljd = (LineJoint)joint;

                        WriteElement("EnableMotor", ljd.MotorEnabled);
                        WriteElement("LocalAnchorA", ljd.LocalAnchorA);
                        WriteElement("LocalAnchorB", ljd.LocalAnchorB);
                        WriteElement("MotorSpeed", ljd.MotorSpeed);
                        WriteElement("DampingRatio", ljd.DampingRatio);
                        WriteElement("MaxMotorTorque", ljd.MaxMotorTorque);
                        WriteElement("FrequencyHz", ljd.Frequency);
                        WriteElement("LocalXAxis", ljd.LocalXAxis);
                    }
                    break;

                case JointType.Prismatic:
                    {
                        PrismaticJoint pjd = (PrismaticJoint)joint;

                        //NOTE: Does not conform with Box2DScene

                        WriteElement("EnableLimit", pjd.LimitEnabled);
                        WriteElement("EnableMotor", pjd.MotorEnabled);
                        WriteElement("LocalAnchorA", pjd.LocalAnchorA);
                        WriteElement("LocalAnchorB", pjd.LocalAnchorB);
                        WriteElement("LocalXAxis1", pjd.LocalXAxis1);
                        WriteElement("LowerTranslation", pjd.LowerLimit);
                        WriteElement("UpperTranslation", pjd.UpperLimit);
                        WriteElement("MaxMotorForce", pjd.MaxMotorForce);
                        WriteElement("MotorSpeed", pjd.MotorSpeed);
                    }
                    break;

                case JointType.Pulley:
                    {
                        PulleyJoint pjd = (PulleyJoint)joint;

                        WriteElement("GroundAnchorA", pjd.GroundAnchorA);
                        WriteElement("GroundAnchorB", pjd.GroundAnchorB);
                        WriteElement("LengthA", pjd.LengthA);
                        WriteElement("LengthB", pjd.LengthB);
                        WriteElement("LocalAnchorA", pjd.LocalAnchorA);
                        WriteElement("LocalAnchorB", pjd.LocalAnchorB);
                        WriteElement("MaxLengthA", pjd.MaxLengthA);
                        WriteElement("MaxLengthB", pjd.MaxLengthB);
                        WriteElement("Ratio", pjd.Ratio);
                    }
                    break;

                case JointType.Revolute:
                    {
                        RevoluteJoint rjd = (RevoluteJoint)joint;

                        WriteElement("EnableLimit", rjd.LimitEnabled);
                        WriteElement("EnableMotor", rjd.MotorEnabled);
                        WriteElement("LocalAnchorA", rjd.LocalAnchorA);
                        WriteElement("LocalAnchorB", rjd.LocalAnchorB);
                        WriteElement("LowerAngle", rjd.LowerLimit);
                        WriteElement("MaxMotorTorque", rjd.MaxMotorTorque);
                        WriteElement("MotorSpeed", rjd.MotorSpeed);
                        WriteElement("ReferenceAngle", rjd.ReferenceAngle);
                        WriteElement("UpperAngle", rjd.UpperLimit);
                    }
                    break;

                case JointType.Weld:
                    {
                        WeldJoint wjd = (WeldJoint)joint;

                        WriteElement("LocalAnchorA", wjd.LocalAnchorA);
                        WriteElement("LocalAnchorB", wjd.LocalAnchorB);
                    }
                    break;

                //
                // Not part of Box2DScene
                //
                case JointType.Rope:
                    {
                        RopeJoint rjd = (RopeJoint)joint;

                        WriteElement("LocalAnchorA", rjd.LocalAnchorA);
                        WriteElement("LocalAnchorB", rjd.LocalAnchorB);
                        WriteElement("MaxLength", rjd.MaxLength);
                    }
                    break;

                case JointType.Angle:
                    {
                        AngleJoint aj = (AngleJoint)joint;
                        WriteElement("BiasFactor", aj.BiasFactor);
                        WriteElement("MaxImpulse", aj.MaxImpulse);
                        WriteElement("Softness", aj.Softness);
                        WriteElement("TargetAngle", aj.TargetAngle);
                    }
                    break;

                case JointType.Slider:
                    {
                        SliderJoint sliderJoint = (SliderJoint)joint;
                        WriteElement("DampingRatio", sliderJoint.DampingRatio);
                        WriteElement("FrequencyHz", sliderJoint.Frequency);
                        WriteElement("MaxLength", sliderJoint.MaxLength);
                        WriteElement("MinLength", sliderJoint.MinLength);
                        WriteElement("LocalAnchorA", sliderJoint.LocalAnchorA);
                        WriteElement("LocalAnchorB", sliderJoint.LocalAnchorB);
                    }
                    break;

                default:
                    throw new Exception("Joint not supported");
            }

            _writer.WriteEndElement();
        }