Пример #1
0
        public static RopeJoint CreateRopeJoint(World world, Body bodyA, Body bodyB, TSVector2 anchorA, TSVector2 anchorB, bool useWorldCoordinates = false)
        {
            RopeJoint ropeJoint = new RopeJoint(bodyA, bodyB, anchorA, anchorB, useWorldCoordinates);

            world.AddJoint(ropeJoint);
            return(ropeJoint);
        }
Пример #2
0
        private static void SerializeJoint(List <Body> bodies, Joint joint)
        {
            WorldXmlSerializer._writer.WriteStartElement("Joint");
            WorldXmlSerializer._writer.WriteAttributeString("Type", joint.JointType.ToString());
            WorldXmlSerializer.WriteElement("BodyA", WorldXmlSerializer.FindIndex(bodies, joint.BodyA));
            WorldXmlSerializer.WriteElement("BodyB", WorldXmlSerializer.FindIndex(bodies, joint.BodyB));
            WorldXmlSerializer.WriteElement("CollideConnected", joint.CollideConnected);
            WorldXmlSerializer.WriteElement("Breakpoint", joint.Breakpoint);
            bool flag = joint.UserData != null;

            if (flag)
            {
                WorldXmlSerializer._writer.WriteStartElement("UserData");
                WorldXmlSerializer.WriteDynamicType(joint.UserData.GetType(), joint.UserData);
                WorldXmlSerializer._writer.WriteEndElement();
            }
            switch (joint.JointType)
            {
            case JointType.Revolute:
            {
                RevoluteJoint revoluteJoint = (RevoluteJoint)joint;
                WorldXmlSerializer.WriteElement("EnableLimit", revoluteJoint.LimitEnabled);
                WorldXmlSerializer.WriteElement("EnableMotor", revoluteJoint.MotorEnabled);
                WorldXmlSerializer.WriteElement("LocalAnchorA", revoluteJoint.LocalAnchorA);
                WorldXmlSerializer.WriteElement("LocalAnchorB", revoluteJoint.LocalAnchorB);
                WorldXmlSerializer.WriteElement("LowerAngle", revoluteJoint.LowerLimit);
                WorldXmlSerializer.WriteElement("MaxMotorTorque", revoluteJoint.MaxMotorTorque);
                WorldXmlSerializer.WriteElement("MotorSpeed", revoluteJoint.MotorSpeed);
                WorldXmlSerializer.WriteElement("ReferenceAngle", revoluteJoint.ReferenceAngle);
                WorldXmlSerializer.WriteElement("UpperAngle", revoluteJoint.UpperLimit);
                break;
            }

            case JointType.Prismatic:
            {
                PrismaticJoint prismaticJoint = (PrismaticJoint)joint;
                WorldXmlSerializer.WriteElement("EnableLimit", prismaticJoint.LimitEnabled);
                WorldXmlSerializer.WriteElement("EnableMotor", prismaticJoint.MotorEnabled);
                WorldXmlSerializer.WriteElement("LocalAnchorA", prismaticJoint.LocalAnchorA);
                WorldXmlSerializer.WriteElement("LocalAnchorB", prismaticJoint.LocalAnchorB);
                WorldXmlSerializer.WriteElement("Axis", prismaticJoint.Axis);
                WorldXmlSerializer.WriteElement("LowerTranslation", prismaticJoint.LowerLimit);
                WorldXmlSerializer.WriteElement("UpperTranslation", prismaticJoint.UpperLimit);
                WorldXmlSerializer.WriteElement("MaxMotorForce", prismaticJoint.MaxMotorForce);
                WorldXmlSerializer.WriteElement("MotorSpeed", prismaticJoint.MotorSpeed);
                break;
            }

            case JointType.Distance:
            {
                DistanceJoint distanceJoint = (DistanceJoint)joint;
                WorldXmlSerializer.WriteElement("DampingRatio", distanceJoint.DampingRatio);
                WorldXmlSerializer.WriteElement("FrequencyHz", distanceJoint.Frequency);
                WorldXmlSerializer.WriteElement("Length", distanceJoint.Length);
                WorldXmlSerializer.WriteElement("LocalAnchorA", distanceJoint.LocalAnchorA);
                WorldXmlSerializer.WriteElement("LocalAnchorB", distanceJoint.LocalAnchorB);
                break;
            }

            case JointType.Pulley:
            {
                PulleyJoint pulleyJoint = (PulleyJoint)joint;
                WorldXmlSerializer.WriteElement("WorldAnchorA", pulleyJoint.WorldAnchorA);
                WorldXmlSerializer.WriteElement("WorldAnchorB", pulleyJoint.WorldAnchorB);
                WorldXmlSerializer.WriteElement("LengthA", pulleyJoint.LengthA);
                WorldXmlSerializer.WriteElement("LengthB", pulleyJoint.LengthB);
                WorldXmlSerializer.WriteElement("LocalAnchorA", pulleyJoint.LocalAnchorA);
                WorldXmlSerializer.WriteElement("LocalAnchorB", pulleyJoint.LocalAnchorB);
                WorldXmlSerializer.WriteElement("Ratio", pulleyJoint.Ratio);
                WorldXmlSerializer.WriteElement("Constant", pulleyJoint.Constant);
                break;
            }

            case JointType.Gear:
                throw new Exception("Gear joint not supported by serialization");

            case JointType.Wheel:
            {
                WheelJoint wheelJoint = (WheelJoint)joint;
                WorldXmlSerializer.WriteElement("EnableMotor", wheelJoint.MotorEnabled);
                WorldXmlSerializer.WriteElement("LocalAnchorA", wheelJoint.LocalAnchorA);
                WorldXmlSerializer.WriteElement("LocalAnchorB", wheelJoint.LocalAnchorB);
                WorldXmlSerializer.WriteElement("MotorSpeed", wheelJoint.MotorSpeed);
                WorldXmlSerializer.WriteElement("DampingRatio", wheelJoint.DampingRatio);
                WorldXmlSerializer.WriteElement("MaxMotorTorque", wheelJoint.MaxMotorTorque);
                WorldXmlSerializer.WriteElement("FrequencyHz", wheelJoint.Frequency);
                WorldXmlSerializer.WriteElement("Axis", wheelJoint.Axis);
                break;
            }

            case JointType.Weld:
            {
                WeldJoint weldJoint = (WeldJoint)joint;
                WorldXmlSerializer.WriteElement("LocalAnchorA", weldJoint.LocalAnchorA);
                WorldXmlSerializer.WriteElement("LocalAnchorB", weldJoint.LocalAnchorB);
                break;
            }

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

            case JointType.Rope:
            {
                RopeJoint ropeJoint = (RopeJoint)joint;
                WorldXmlSerializer.WriteElement("LocalAnchorA", ropeJoint.LocalAnchorA);
                WorldXmlSerializer.WriteElement("LocalAnchorB", ropeJoint.LocalAnchorB);
                WorldXmlSerializer.WriteElement("MaxLength", ropeJoint.MaxLength);
                break;
            }

            case JointType.Motor:
            {
                MotorJoint motorJoint = (MotorJoint)joint;
                WorldXmlSerializer.WriteElement("AngularOffset", motorJoint.AngularOffset);
                WorldXmlSerializer.WriteElement("LinearOffset", motorJoint.LinearOffset);
                WorldXmlSerializer.WriteElement("MaxForce", motorJoint.MaxForce);
                WorldXmlSerializer.WriteElement("MaxTorque", motorJoint.MaxTorque);
                WorldXmlSerializer.WriteElement("CorrectionFactor", motorJoint.CorrectionFactor);
                break;
            }

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

            default:
                throw new Exception("Joint not supported");
            }
            WorldXmlSerializer._writer.WriteEndElement();
        }