private PulleysTest()
        {
            //Ground
            BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

            {
                const float a = 2.0f;
                const float b = 4.0f;
                const float y = 16.0f;
                const float l = 12.0f;

                PolygonShape shape = new PolygonShape(5);
                shape.SetAsBox(a, b);

                Body body1 = BodyFactory.CreateBody(World);
                body1.BodyType = BodyType.Dynamic;
                body1.Position = new Vector2(-10.0f, y);
                body1.CreateFixture(shape);

                Body body2 = BodyFactory.CreateBody(World);
                body2.BodyType = BodyType.Dynamic;
                body2.Position = new Vector2(10.0f, y);

                body2.CreateFixture(shape);

                Vector2 anchor1       = new Vector2(-10.0f, y + b);
                Vector2 anchor2       = new Vector2(10.0f, y + b);
                Vector2 groundAnchor1 = new Vector2(-10.0f, y + b + l);
                Vector2 groundAnchor2 = new Vector2(10.0f, y + b + l);
                _joint1 = new PulleyJoint(body1, body2, groundAnchor1, groundAnchor2, body1.GetLocalPoint(anchor1),
                                          body2.GetLocalPoint(anchor2), 2.0f);
                World.AddJoint(_joint1);
            }
        }
Пример #2
0
        public static PulleyJoint CreatePulleyJoint(World world, Body bodyA, Body bodyB, Vector2 groundAnchorA, Vector2 groundAnchorB, Vector2 anchorA, Vector2 anchorB, float ratio)
        {
            PulleyJoint pulleyJoint = new PulleyJoint(bodyA, bodyB, anchorA, anchorB, groundAnchorA, groundAnchorB, ratio);

            world.AddJoint(pulleyJoint);
            return(pulleyJoint);
        }
Пример #3
0
        protected override void Create()
        {
            var y = 16.0f;
            var L = 12.0f;
            var a = 1.0f;
            var b = 2.0f;

            Body ground;
            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var edge = new EdgeShape();
                edge.Set(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

                //ground.CreateFixture(shape, 0.0f);

                var circle = new CircleShape();
                circle.Radius = 2.0f;

                circle.Position.Set(-10.0f, y + b + L);
                ground.CreateFixture(circle, 0.0f);

                circle.Position.Set(10.0f, y + b + L);
                ground.CreateFixture(circle, 0.0f);
            }

            {
                var shape = new PolygonShape();
                shape.SetAsBox(a, b);

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;

                //bd.fixedRotation = true;
                bd.Position.Set(-10.0f, y);
                var body1 = World.CreateBody(bd);
                body1.CreateFixture(shape, 5.0f);

                bd.Position.Set(10.0f, y);
                var body2 = World.CreateBody(bd);
                body2.CreateFixture(shape, 5.0f);

                var pulleyDef     = new PulleyJointDef();
                var anchor1       = new Vector2(-10.0f, y + b);
                var anchor2       = new Vector2(10.0f, y + b);
                var groundAnchor1 = new Vector2(-10.0f, y + b + L);
                var groundAnchor2 = new Vector2(10.0f, y + b + L);
                pulleyDef.Initialize(
                    body1,
                    body2,
                    groundAnchor1,
                    groundAnchor2,
                    anchor1,
                    anchor2,
                    1.5f);

                _joint1 = (PulleyJoint)World.CreateJoint(pulleyDef);
            }
        }
Пример #4
0
        private PulleysTest()
        {
            //Ground
            Body ground = BodyFactory.CreateBody(World);

            FixtureFactory.AttachCircle(2, 0, ground, new Vector2(-10.0f, Y + B + L));
            FixtureFactory.AttachCircle(2, 0, ground, new Vector2(10.0f, Y + B + L));

            {
                PolygonShape shape = new PolygonShape(5);
                shape.Vertices = PolygonTools.CreateRectangle(A, B);

                Body body1 = BodyFactory.CreateBody(World);
                body1.BodyType = BodyType.Dynamic;
                body1.Position = new Vector2(-10.0f, Y);
                body1.CreateFixture(shape);

                Body body2 = BodyFactory.CreateBody(World);
                body2.BodyType = BodyType.Dynamic;
                body2.Position = new Vector2(10.0f, Y);

                body2.CreateFixture(shape);

                Vector2 anchor1       = new Vector2(-10.0f, Y + B);
                Vector2 anchor2       = new Vector2(10.0f, Y + B);
                Vector2 groundAnchor1 = new Vector2(-10.0f, Y + B + L);
                Vector2 groundAnchor2 = new Vector2(10.0f, Y + B + L);
                _joint1 = new PulleyJoint(body1, body2, anchor1, anchor2, groundAnchor1, groundAnchor2, 1.5f, true);
                World.AddJoint(_joint1);
            }
        }
Пример #5
0
        public static PulleyJoint CreatePulleyJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB, Vector2 worldAnchorA, Vector2 worldAnchorB, float ratio, bool useWorldCoordinates = false)
        {
            PulleyJoint pulleyJoint = new PulleyJoint(bodyA, bodyB, anchorA, anchorB, worldAnchorA, worldAnchorB, ratio, useWorldCoordinates);

            world.Add(pulleyJoint);
            return(pulleyJoint);
        }
Пример #6
0
        public override Joint CreateJoint()
        {
            var joint = new PulleyJoint(BodyA, BodyB, OwnerBodyAnchor * FSConvert.DisplayToSim, OtherBodyGroundAnchor * FSConvert.DisplayToSim,
                                        OwnerBodyGroundAnchor * FSConvert.DisplayToSim, OtherBodyGroundAnchor * FSConvert.DisplayToSim, Ratio);

            joint.CollideConnected = CollideConnected;
            return(joint);
        }
Пример #7
0
        public override Joint createJoint()
        {
            var joint = new PulleyJoint(bodyA, bodyB, ownerBodyAnchor * FSConvert.displayToSim, otherBodyGroundAnchor * FSConvert.displayToSim,
                                        ownerBodyGroundAnchor * FSConvert.displayToSim, otherBodyGroundAnchor * FSConvert.displayToSim, ratio);

            joint.collideConnected = collideConnected;
            return(joint);
        }
Пример #8
0
        /// <summary>
        /// Creates a distance joint between two bodies
        /// </summary>
        /// <param name="b1"></param>
        /// <param name="b2"></param>
        /// <param name="gAnchor1"></param>
        /// <param name="gAnchor2"></param>
        /// <param name="ratio"></param>
        /// <returns></returns>
        public PulleyJoint joinBodies_Pulley(Body b1, Body b2, Vector2 gAnchor1, Vector2 gAnchor2, float ratio)
        {
            PulleyJointDef pjd = new PulleyJointDef();

            pjd.Initialize(b1, b2, gAnchor1, gAnchor2, b1.GetWorldCenter(), b2.GetWorldCenter(), ratio);
            PulleyJoint pj = physicsWorld.CreateJoint(pjd) as PulleyJoint;

            return(pj);
        }
Пример #9
0
        protected override Joint CreateJoint(Body bodyA, Body bodyB)
        {
            if (bodyA == null || bodyB == null)
            {
                return(null);
            }
            PulleyJoint joint = new PulleyJoint(bodyA, bodyB, Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero, 1.0f);

            Scene.PhysicsWorld.AddJoint(joint);
            return(joint);
        }
Пример #10
0
        private PulleyJointTest()
        {
            float y = 16.0f;
            float L = 12.0f;
            float a = 1.0f;
            float b = 2.0f;

            Body ground;
            {
                BodyDef bd = new BodyDef();
                ground = BodyFactory.CreateFromDef(World, bd);

                CircleShape circle = new CircleShape(0.0f);
                circle.Radius = 2.0f;

                circle.Position = new Vector2(-10.0f, y + b + L);
                ground.AddFixture(circle);

                circle.Position = new Vector2(10.0f, y + b + L);
                ground.AddFixture(circle);
            }

            {
                PolygonShape shape = new PolygonShape(5.0f);
                shape.SetAsBox(a, b);

                BodyDef bd = new BodyDef();
                bd.Type = BodyType.Dynamic;

                //bd.FixedRotation = true;
                bd.Position = new Vector2(-10.0f, y);
                Body body1 = BodyFactory.CreateFromDef(World, bd);
                body1.AddFixture(shape);

                bd.Position = new Vector2(10.0f, y);
                Body body2 = BodyFactory.CreateFromDef(World, bd);
                body2.AddFixture(shape);

                PulleyJointDef pulleyDef     = new PulleyJointDef();
                Vector2        anchor1       = new Vector2(-10.0f, y + b);
                Vector2        anchor2       = new Vector2(10.0f, y + b);
                Vector2        groundAnchor1 = new Vector2(-10.0f, y + b + L);
                Vector2        groundAnchor2 = new Vector2(10.0f, y + b + L);
                pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5f);

                _joint1 = (PulleyJoint)JointFactory.CreateFromDef(World, pulleyDef);
            }
        }
Пример #11
0
        public Pulleys()
        {
            Body ground = null;
            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(50.0f, 10.0f);

                BodyDef bd = new BodyDef();
                bd.Position.Set(0.0f, -10.0f);
                ground = _world.CreateBody(bd);
                ground.CreateShape(sd);
            }

            {
                float a = 2.0f;
                float b = 4.0f;
                float y = 16.0f;
                float L = 12.0f;

                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(a, b);
                sd.Density = 5.0f;

                BodyDef bd = new BodyDef();

                bd.Position.Set(-10.0f, y);
                Body body1 = _world.CreateBody(bd);
                body1.CreateShape(sd);
                body1.SetMassFromShapes();

                bd.Position.Set(10.0f, y);
                Body body2 = _world.CreateBody(bd);
                body2.CreateShape(sd);
                body2.SetMassFromShapes();

                PulleyJointDef pulleyDef     = new PulleyJointDef();
                Vec2           anchor1       = new Vec2(-10.0f, y + b);
                Vec2           anchor2       = new Vec2(10.0f, y + b);
                Vec2           groundAnchor1 = new Vec2(-10.0f, y + b + L);
                Vec2           groundAnchor2 = new Vec2(10.0f, y + b + L);
                pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 2.0f);

                _joint1 = (PulleyJoint)_world.CreateJoint(pulleyDef);
            }
        }
Пример #12
0
    public override void InitJoint()
    {
        base.InitJoint();

        Vector3 p0 = BodyA.transform.TransformPoint(new Vector3(LocalAnchorA.x, LocalAnchorA.y, -5f));
        Vector3 p1 = BodyB.transform.TransformPoint(new Vector3(LocalAnchorB.x, LocalAnchorB.y, -5f));

        joint = JointFactory.CreatePulleyJoint(FSWorldComponent.PhysicsWorld,
                                               BodyA.PhysicsBody,
                                               BodyB.PhysicsBody,
                                               FSHelper.Vector3ToFVector2(GroundAnchorA.position),
                                               FSHelper.Vector3ToFVector2(GroundAnchorB.position),
                                               BodyA.PhysicsBody.GetLocalPoint(FSHelper.Vector3ToFVector2(p0)),
                                               BodyB.PhysicsBody.GetLocalPoint(FSHelper.Vector3ToFVector2(p1)),
                                               ForceRatio);

        joint.CollideConnected = CollideConnected;
    }
Пример #13
0
        internal override void UpdateJoint()
        {
            base.UpdateJoint();
            if (this.joint == null)
            {
                return;
            }

            PulleyJoint j = this.joint as PulleyJoint;

            j.LocalAnchorB  = GetFarseerPoint(this.BodyB, this.localAnchorB);
            j.LocalAnchorA  = GetFarseerPoint(this.BodyA, this.localAnchorA);
            j.GroundAnchorB = PhysicsConvert.ToPhysicalUnit(this.worldAnchorB);
            j.GroundAnchorA = PhysicsConvert.ToPhysicalUnit(this.worldAnchorA);
            j.MaxLengthA    = PhysicsConvert.ToPhysicalUnit(this.maxLengthA);
            j.MaxLengthB    = PhysicsConvert.ToPhysicalUnit(this.maxLengthB);
            j.TotalLength   = PhysicsConvert.ToPhysicalUnit(this.totalLength);
            j.Ratio         = this.ratio;
        }
Пример #14
0
        internal override void UpdateJoint()
        {
            base.UpdateJoint();
            if (this.joint == null)
            {
                return;
            }

            PulleyJoint j = this.joint as PulleyJoint;

            j.LocalAnchorB  = GetFarseerPoint(this.OtherBody, this.localAnchorB);
            j.LocalAnchorA  = GetFarseerPoint(this.ParentBody, this.localAnchorA);
            j.GroundAnchorB = PhysicsUnit.LengthToPhysical * this.worldAnchorB;
            j.GroundAnchorA = PhysicsUnit.LengthToPhysical * this.worldAnchorA;
            j.MaxLengthA    = PhysicsUnit.LengthToPhysical * this.maxLengthA;
            j.MaxLengthB    = PhysicsUnit.LengthToPhysical * this.maxLengthB;
            j.TotalLength   = PhysicsUnit.LengthToPhysical * this.totalLength;
            j.Ratio         = this.ratio;
        }
Пример #15
0
        Pulleys()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                float a = 2.0f;
                float b = 4.0f;
                float y = 16.0f;
                float L = 12.0f;

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(a, b);

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;

                bd.position = new Vector2(-10.0f, y);
                Body body1 = _world.CreateBody(bd);
                body1.CreateFixture(shape, 5.0f);

                bd.position = new Vector2(10.0f, y);
                Body body2 = _world.CreateBody(bd);
                body2.CreateFixture(shape, 5.0f);

                PulleyJointDef pulleyDef = new PulleyJointDef();
                Vector2 anchor1 = new Vector2(-10.0f, y + b);
                Vector2 anchor2 = new Vector2(10.0f, y + b);
                Vector2 groundAnchor1 = new Vector2(-10.0f, y + b + L);
                Vector2 groundAnchor2 = new Vector2(10.0f, y + b + L);
                pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 2.0f);

                _joint1 = (PulleyJoint)_world.CreateJoint(pulleyDef);
            }
        }
Пример #16
0
        Pulleys()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vector2(-40.0, 0.0), new Vector2(40.0, 0.0));
                ground.CreateFixture(shape, 0.0);
            }

            {
                double a = 2.0;
                double b = 4.0;
                double y = 16.0;
                double L = 12.0;

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(a, b);

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;

                bd.position = new Vector2(-10.0, y);
                Body body1 = _world.CreateBody(bd);
                body1.CreateFixture(shape, 5.0);

                bd.position = new Vector2(10.0, y);
                Body body2 = _world.CreateBody(bd);
                body2.CreateFixture(shape, 5.0);

                PulleyJointDef pulleyDef     = new PulleyJointDef();
                Vector2        anchor1       = new Vector2(-10.0, y + b);
                Vector2        anchor2       = new Vector2(10.0, y + b);
                Vector2        groundAnchor1 = new Vector2(-10.0, y + b + L);
                Vector2        groundAnchor2 = new Vector2(10.0, y + b + L);
                pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 2.0f);

                _joint1 = (PulleyJoint)_world.CreateJoint(pulleyDef);
            }
        }
Пример #17
0
        public override void Load()
        {
            Physics.SetMeter(1);

            float y = 16.0f;
            float L = 12.0f;
            float a = 1.0f;
            float b = 2.0f;

            Body ground;
            {
                ground = Physics.NewBody(m_world);

                EdgeShape edge = Physics.NewEdgeShape(-40.0f, 0.0f, 40.0f, 0.0f);
                CircleShape circle = Physics.NewCircleShape(2.0f);

                circle.SetPoint(-10.0f, y + b + L);
                Physics.NewFixture(ground, circle, 0);

                circle.SetPoint(10.0f, y + b + L);
                Physics.NewFixture(ground, circle, 0);
            }

            {
                PolygonShape shape = Physics.NewRectangleShape(a * 2, b * 2);

                Body body1 = Physics.NewBody(m_world, -10.0f, y, BodyType.Dynamic);
                Physics.NewFixture(body1, shape, 5.0f);

                Body body2 = Physics.NewBody(m_world, 10.0f, y, BodyType.Dynamic);
                Physics.NewFixture(body2, shape, 5.0f);


                Vector2 anchor1 = new Vector2(-10.0f, y + b);
                Vector2 anchor2 = new Vector2(10.0f, y + b);
                Vector2 groundAnchor1 = new Vector2(-10.0f, y + b + L);
                Vector2 groundAnchor2 = new Vector2(10.0f, y + b + L);

                PulleyJoint m_joint1 = Physics.NewPulleyJoint(body1, body2,
                    groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5f);
            }
        }
Пример #18
0
        //private static Joint DeserializeSliderJoint(XElement jointElement, Body bodyA, Body bodyB, World world)
        //{
        //    SliderJoint joint = null;
        //    Vector2 localAnchorA, localAnchorB;
        //    float minLength = 0.0f, maxLength = 100.0f, dampingRatio = 0.0f, frequency = 0.0f;

        //    localAnchorA = bodyA.WorldCenter;
        //    localAnchorB = bodyB.WorldCenter;

        //    foreach (XElement element in jointElement.Elements())
        //    {
        //        switch (element.Name.ToString().ToLower())
        //        {
        //            case "minlength":
        //                float.TryParse(element.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out minLength);
        //                break;
        //            case "maxlength":
        //                float.TryParse(element.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out maxLength);
        //                break;
        //            case "dampingratio":
        //                float.TryParse(element.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out dampingRatio);
        //                break;
        //            case "frequency":
        //                float.TryParse(element.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out frequency);
        //                break;
        //            case "localanchora":
        //                localAnchorA = localAnchorA.DeserializeOffset(element);
        //                localAnchorA = ConvertUnits.ToSimUnits(localAnchorA);
        //                break;
        //            case "localanchorb":
        //                localAnchorB = localAnchorB.DeserializeOffset(element);
        //                localAnchorB = ConvertUnits.ToSimUnits(localAnchorB);
        //                break;
        //        }
        //    }

        //    joint = JointFactory.CreateSliderJoint(world, bodyA, bodyB, localAnchorA, localAnchorB, minLength, maxLength);
        //    joint.DampingRatio = dampingRatio;
        //    joint.Frequency = frequency;

        //    return joint;
        //}

        #endregion

        #endregion

        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 FarseerPhysics.Factories.JointFactory().(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.FixedLine:
             *  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.LocalXAxis;
                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).LocalXAxis = pJoint.LocalXAxis;
                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);
        }
Пример #19
0
        private void DrawJoint(Joint joint)
        {
            Body      b1 = joint.BodyA;
            Body      b2 = joint.BodyB;
            Transform xf1, xf2;

            b1.GetTransform(out xf1);

            Vector2 x2 = new Vector2();

            // WIP David
            if (!joint.IsFixedType())
            {
                b2.GetTransform(out xf2);
                x2 = xf2.p;
            }
            Vector2 p2 = joint.WorldAnchorB;

            Vector2 x1 = xf1.p;

            Vector2 p1 = joint.WorldAnchorA;

            Color color = Color.FromArgb(255, 128, 205, 205);

            switch (joint.JointType)
            {
            case JointType.Distance:
                DrawSegment(p1, p2, color);
                break;

            case JointType.Pulley:
            {
                PulleyJoint pulley = (PulleyJoint)joint;
                Vector2     s1     = pulley.GroundAnchorA;
                Vector2     s2     = pulley.GroundAnchorB;
                DrawSegment(s1, p1, color);
                DrawSegment(s2, p2, color);
                DrawSegment(s1, s2, color);
            }
            break;

            case JointType.FixedMouse:
                DrawPoint(p1, 0.5f, Color.FromArgb(255, 0, 255, 0));
                DrawSegment(p1, p2, Color.FromArgb(255, 205, 205, 205));
                break;

            case JointType.Revolute:
                //DrawSegment(x2, p1, color);
                DrawSegment(p2, p1, color);
                DrawSolidCircle(p2, 0.1f, new Vector2(), Colors.Red);
                DrawSolidCircle(p1, 0.1f, new Vector2(), Colors.Blue);
                break;

            case JointType.FixedRevolute:
                DrawSolidCircle(p1, 0.1f, new Vector2(), Colors.Purple);
                break;

            case JointType.FixedLine:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.FixedDistance:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.FixedPrismatic:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.Gear:
                DrawSegment(x1, x2, color);
                //DrawSegment(x1, p1, color);
                //DrawSegment(p1, p2, color);
                break;

            default:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                DrawSegment(x2, p2, color);
                break;
            }
        }
        private void DrawJoint(Joint joint, Color mainColor)
        {
            if (!joint.Enabled)
            {
                return;
            }

            Body      b1 = joint.BodyA;
            Body      b2 = joint.BodyB;
            Transform xf1, xf2;

            b1.GetTransform(out xf1);

            Vector2 x2 = Vector2.Zero;

            if (!joint.IsFixedType())
            {
                b2.GetTransform(out xf2);
                x2 = xf2.Position;
            }

            Vector2 p1 = joint.WorldAnchorA;
            Vector2 p2 = joint.WorldAnchorB;
            Vector2 x1 = xf1.Position;

            switch (joint.JointType)
            {
            case JointType.Distance:
                DrawSegment(p1, p2, mainColor);
                break;

            case JointType.Pulley:
                PulleyJoint pulley = (PulleyJoint)joint;
                Vector2     s1     = pulley.GroundAnchorA;
                Vector2     s2     = pulley.GroundAnchorB;
                DrawSegment(s1, p1, mainColor);
                DrawSegment(s2, p2, mainColor);
                DrawSegment(s1, s2, mainColor);
                break;

            case JointType.FixedMouse:
                DrawPoint(p1, 1.0f, new Color(0.0f, 1.0f, 0.0f));
                DrawSegment(p1, p2, new Color(0.8f, 0.8f, 0.8f));
                break;

            case JointType.Revolute:
                //DrawSegment(x2, p1, color);
                DrawSegment(p2, p1, mainColor);
                DrawSolidCircle(p2, 0.5f, Vector2.Zero, Color.Green);
                DrawSolidCircle(p1, 0.5f, Vector2.Zero, Color.Blue);
                break;

            case JointType.FixedAngle:
                //Should not draw anything.
                break;

            case JointType.FixedRevolute:
                DrawSegment(x1, p1, mainColor);
                DrawSolidCircle(p1, 0.5f, Vector2.Zero, Color.Blue);
                break;

            case JointType.FixedLine:
                DrawSegment(x1, p1, mainColor);
                DrawSegment(p1, p2, mainColor);
                break;

            case JointType.FixedDistance:
                DrawSegment(x1, p1, mainColor);
                DrawSegment(p1, p2, mainColor);
                break;

            case JointType.FixedPrismatic:
                DrawSegment(x1, p1, mainColor);
                DrawSegment(p1, p2, mainColor);
                break;

            case JointType.Gear:
                DrawSegment(x1, x2, mainColor);
                break;

            //case JointType.Weld:
            //    break;
            default:
                DrawSegment(x1, p1, mainColor);
                DrawSegment(p1, p2, mainColor);
                DrawSegment(x2, p2, mainColor);
                break;
            }
        }
Пример #21
0
        private static void SerializeJoint(List <Body> bodies, Joint joint)
        {
            _writer.WriteStartElement("Joint");
            _writer.WriteAttributeString("Type", joint.JointType.ToString());

            WriteElement("BodyA", FindIndex(bodies, joint.BodyA));
            WriteElement("BodyB", FindIndex(bodies, 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 distanceJoint = (DistanceJoint)joint;
                WriteElement("DampingRatio", distanceJoint.DampingRatio);
                WriteElement("FrequencyHz", distanceJoint.Frequency);
                WriteElement("Length", distanceJoint.Length);
                WriteElement("LocalAnchorA", distanceJoint.LocalAnchorA);
                WriteElement("LocalAnchorB", distanceJoint.LocalAnchorB);
            }
            break;

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

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

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

            case JointType.Prismatic:
            {
                //NOTE: Does not conform with Box2DScene

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

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

            case JointType.Revolute:
            {
                RevoluteJoint revoluteJoint = (RevoluteJoint)joint;
                WriteElement("EnableLimit", revoluteJoint.LimitEnabled);
                WriteElement("EnableMotor", revoluteJoint.MotorEnabled);
                WriteElement("LocalAnchorA", revoluteJoint.LocalAnchorA);
                WriteElement("LocalAnchorB", revoluteJoint.LocalAnchorB);
                WriteElement("LowerAngle", revoluteJoint.LowerLimit);
                WriteElement("MaxMotorTorque", revoluteJoint.MaxMotorTorque);
                WriteElement("MotorSpeed", revoluteJoint.MotorSpeed);
                WriteElement("ReferenceAngle", revoluteJoint.ReferenceAngle);
                WriteElement("UpperAngle", revoluteJoint.UpperLimit);
            }
            break;

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

            //
            // Not part of Box2DScene
            //
            case JointType.Rope:
            {
                RopeJoint ropeJoint = (RopeJoint)joint;
                WriteElement("LocalAnchorA", ropeJoint.LocalAnchorA);
                WriteElement("LocalAnchorB", ropeJoint.LocalAnchorB);
                WriteElement("MaxLength", ropeJoint.MaxLength);
            }
            break;

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

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

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

            _writer.WriteEndElement();
        }
Пример #22
0
        private void DrawJoint(Joint joint)
        {
            if (!joint.Enabled)
            {
                return;
            }

            Body      b1 = joint.BodyA;
            Body      b2 = joint.BodyB;
            Transform xf1;

            b1.GetTransform(out xf1);

            Vector2 x2 = Vector2.Zero;

            // WIP David
            if (!joint.IsFixedType())
            {
                Transform xf2;
                b2.GetTransform(out xf2);
                x2 = xf2.p;
            }

            Vector2 p1 = joint.WorldAnchorA;
            Vector2 p2 = joint.WorldAnchorB;
            Vector2 x1 = xf1.p;

            Color color = new Color(0.5f, 0.8f, 0.8f);

            switch (joint.JointType)
            {
            case JointType.Distance:
                DrawSegment(p1, p2, color);
                break;

            case JointType.Pulley:
                PulleyJoint pulley = (PulleyJoint)joint;
                Vector2     s1     = b1.GetWorldPoint(pulley.LocalAnchorA);
                Vector2     s2     = b2.GetWorldPoint(pulley.LocalAnchorB);
                DrawSegment(p1, p2, color);
                DrawSegment(p1, s1, color);
                DrawSegment(p2, s2, color);
                break;

            case JointType.FixedMouse:
                DrawPoint(p1, 0.5f, new Color(0.0f, 1.0f, 0.0f));
                DrawSegment(p1, p2, new Color(0.8f, 0.8f, 0.8f));
                break;

            case JointType.Revolute:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                DrawSegment(x2, p2, color);

                DrawSolidCircle(p2, 0.1f, Vector2.Zero, Color.Red);
                DrawSolidCircle(p1, 0.1f, Vector2.Zero, Color.Blue);
                break;

            case JointType.FixedAngle:
                //Should not draw anything.
                break;

            case JointType.FixedRevolute:
                DrawSegment(x1, p1, color);
                DrawSolidCircle(p1, 0.1f, Vector2.Zero, Color.Pink);
                break;

            case JointType.FixedLine:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.FixedDistance:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.FixedPrismatic:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.Gear:
                DrawSegment(x1, x2, color);
                break;

            default:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                DrawSegment(x2, p2, color);
                break;
            }
        }
Пример #23
0
        public void DrawJoint(Joint joint)
        {
            if (!joint.Enabled)
            {
                return;
            }

            Body bA = joint.BodyA;
            Body bB = joint.BodyB;

            bA.GetTransform(out Transform xfA);
            Vector2 x1 = xfA.p;

            Vector2   x2  = Vector2.Zero;
            Transform xfB = default;

            if (!joint.IsFixedType())
            {
                bB.GetTransform(out xfB);
                x2 = xfB.p;
            }

            Vector2 p1 = joint.WorldAnchorA;
            Vector2 p2 = joint.WorldAnchorB;

            Color color = new Color(0.5f, 0.8f, 0.8f);

            if (joint is PrismaticJoint pj)
            {
                Vector2 pA = MathUtils.Mul(ref xfA, pj.LocalAnchorA);
                Vector2 pB = MathUtils.Mul(ref xfB, pj.LocalAnchorB);

                Vector2 axis = MathUtils.Mul(ref xfA.q, pj.LocalXAxisA);

                Color c1 = new Color(0.7f, 0.7f, 0.7f);
                Color c2 = new Color(0.3f, 0.9f, 0.3f);
                Color c3 = new Color(0.9f, 0.3f, 0.3f);
                Color c4 = new Color(0.3f, 0.3f, 0.9f);
                Color c5 = new Color(0.4f, 0.4f, 0.4f);

                DrawSegment(pA, pB, c5);

                if (pj.LimitEnabled)
                {
                    Vector2 lower = pA + pj.LowerLimit * axis;
                    Vector2 upper = pA + pj.UpperLimit * axis;
                    Vector2 perp  = MathUtils.Mul(xfA.q, pj.LocalYAxisA);
                    DrawSegment(lower, upper, c1);
                    DrawSegment(lower - 0.5f * perp, lower + 0.5f * perp, c2);
                    DrawSegment(upper - 0.5f * perp, upper + 0.5f * perp, c3);
                }
                else
                {
                    DrawSegment(pA - 1.0f * axis, pA + 1.0f * axis, c1);
                }

                DrawPoint(pA, 1.0f, c1);
                DrawPoint(pB, 1.0f, c4);
            }
            else if (joint is WheelJoint wj)
            {
                Vector2 pA = MathUtils.Mul(ref xfA, wj.LocalAnchorA);
                Vector2 pB = MathUtils.Mul(ref xfB, wj.LocalAnchorB);

                Vector2 axis = MathUtils.Mul(xfA.q, wj.LocalXAxisA);

                Color c1 = new Color(0.7f, 0.7f, 0.7f);
                Color c2 = new Color(0.3f, 0.9f, 0.3f);
                Color c3 = new Color(0.9f, 0.3f, 0.3f);
                Color c4 = new Color(0.3f, 0.3f, 0.9f);
                Color c5 = new Color(0.4f, 0.4f, 0.4f);

                DrawSegment(pA, pB, c5);

                if (wj.EnableLimit)
                {
                    Vector2 lower = pA + wj.LowerLimit * axis;
                    Vector2 upper = pA + wj.UpperLimit * axis;
                    Vector2 perp  = MathUtils.Mul(xfA.q, wj.LocalYAxisA);
                    DrawSegment(lower, upper, c1);
                    DrawSegment(lower - 0.5f * perp, lower + 0.5f * perp, c2);
                    DrawSegment(upper - 0.5f * perp, upper + 0.5f * perp, c3);
                }
                else
                {
                    DrawSegment(pA - 1.0f * axis, pA + 1.0f * axis, c1);
                }

                DrawPoint(pA, 0.1f, c1);
                DrawPoint(pB, 0.1f, c4);
            }
            else
            {
                switch (joint.JointType)
                {
                case JointType.Distance:
                    DrawSegment(p1, p2, color);
                    break;

                case JointType.Pulley:
                    PulleyJoint pulley = (PulleyJoint)joint;
                    Vector2     s1     = bA.GetWorldPoint(pulley.LocalAnchorA);
                    Vector2     s2     = bB.GetWorldPoint(pulley.LocalAnchorB);
                    DrawSegment(p1, p2, color);
                    DrawSegment(p1, s1, color);
                    DrawSegment(p2, s2, color);
                    break;

                case JointType.FixedMouse:
                    DrawPoint(p1, 0.5f, new Color(0.0f, 1.0f, 0.0f));
                    DrawSegment(p1, p2, new Color(0.8f, 0.8f, 0.8f));
                    break;

                case JointType.Revolute:
                    DrawSegment(x1, p1, color);
                    DrawSegment(p1, p2, color);
                    DrawSegment(x2, p2, color);

                    DrawSolidCircle(p2, 0.1f, Vector2.Zero, Color.Red);
                    DrawSolidCircle(p1, 0.1f, Vector2.Zero, Color.Blue);
                    break;

                case JointType.FixedAngle:

                    //Should not draw anything.
                    break;

                case JointType.FixedRevolute:
                    DrawSegment(x1, p1, color);
                    DrawSolidCircle(p1, 0.1f, Vector2.Zero, Color.Pink);
                    break;

                case JointType.FixedLine:
                    DrawSegment(x1, p1, color);
                    DrawSegment(p1, p2, color);
                    break;

                case JointType.FixedDistance:
                    DrawSegment(x1, p1, color);
                    DrawSegment(p1, p2, color);
                    break;

                case JointType.FixedPrismatic:
                    DrawSegment(x1, p1, color);
                    DrawSegment(p1, p2, color);
                    break;

                case JointType.Gear:
                    DrawSegment(x1, x2, color);
                    break;

                default:
                    DrawSegment(x1, p1, color);
                    DrawSegment(p1, p2, color);
                    DrawSegment(x2, p2, color);
                    break;
                }
            }
        }
Пример #24
0
        private void DrawJoint(FarseerJoint joint)
        {
            if (!joint.Enabled)
            {
                return;
            }

            Body      b1 = joint.BodyA;
            Body      b2 = joint.BodyB;
            Transform xf1, xf2;

            b1.GetTransform(out xf1);

            FVector2 x2 = FVector2.Zero;

            // WIP David
            if (!joint.IsFixedType())
            {
                b2.GetTransform(out xf2);
                x2 = xf2.p;
            }

            FVector2 p1 = joint.WorldAnchorA;
            FVector2 p2 = joint.WorldAnchorB;
            FVector2 x1 = xf1.p;

            Color color = new Color(0.5f, 0.8f, 0.8f);

            switch (joint.JointType)
            {
            case JointType.Distance:
                DrawSegment(p1, p2, color);
                break;

            case JointType.Pulley:
                PulleyJoint pulley = (PulleyJoint)joint;
                FVector2    s1     = pulley.GroundAnchorA;
                FVector2    s2     = pulley.GroundAnchorB;
                DrawSegment(s1, p1, color);
                DrawSegment(s2, p2, color);
                DrawSegment(s1, s2, color);
                break;

            case JointType.FixedMouse:
                DrawPoint(p1, 0.5f, new Color(0.0f, 1.0f, 0.0f));
                DrawSegment(p1, p2, new Color(0.8f, 0.8f, 0.8f));
                break;

            case JointType.Revolute:
                //DrawSegment(x2, p1, color);
                DrawSegment(p2, p1, color);
                DrawSolidCircle(p2, 0.1f, FVector2.Zero, Color.red);
                DrawSolidCircle(p1, 0.1f, FVector2.Zero, Color.blue);
                break;

            case JointType.FixedAngle:
                //Should not draw anything.
                break;

            case JointType.FixedRevolute:
                DrawSegment(x1, p1, color);
                DrawSolidCircle(p1, 0.1f, FVector2.Zero, new Color(1f, 0.5f, 1f));
                break;

            case JointType.FixedLine:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.FixedDistance:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.FixedPrismatic:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.Gear:
                DrawSegment(x1, x2, color);
                break;

            //case JointType.Weld:
            //    break;
            default:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                DrawSegment(x2, p2, color);
                break;
            }
        }
Пример #25
0
        private static void Deserialize(World world, Stream stream)
        {
            List <Body>    bodies   = new List <Body>();
            List <Fixture> fixtures = new List <Fixture>();
            List <Joint>   joints   = new List <Joint>();
            List <Shape>   shapes   = new List <Shape>();

            XMLFragmentElement root = XMLFragmentParser.LoadFromStream(stream);

            if (root.Name.ToLower() != "world")
            {
                throw new Exception();
            }

            //Read gravity
            foreach (XMLFragmentElement element in root.Elements)
            {
                if (element.Name.ToLower() == "gravity")
                {
                    world.Gravity = ReadVector(element);
                    break;
                }
            }

            //Read shapes
            foreach (XMLFragmentElement shapeElement in root.Elements)
            {
                if (shapeElement.Name.ToLower() == "shapes")
                {
                    foreach (XMLFragmentElement element in shapeElement.Elements)
                    {
                        if (element.Name.ToLower() != "shape")
                        {
                            throw new Exception();
                        }

                        ShapeType type    = (ShapeType)Enum.Parse(typeof(ShapeType), element.Attributes[0].Value, true);
                        float     density = float.Parse(element.Attributes[1].Value);

                        switch (type)
                        {
                        case ShapeType.Circle:
                        {
                            CircleShape shape = new CircleShape();
                            shape._density = density;

                            foreach (XMLFragmentElement sn in element.Elements)
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "radius":
                                    shape.Radius = float.Parse(sn.Value);
                                    break;

                                case "position":
                                    shape.Position = ReadVector(sn);
                                    break;

                                default:
                                    throw new Exception();
                                }
                            }

                            shapes.Add(shape);
                        }
                        break;

                        case ShapeType.Polygon:
                        {
                            PolygonShape shape = new PolygonShape();
                            shape._density = density;

                            foreach (XMLFragmentElement sn in element.Elements)
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "vertices":
                                {
                                    List <Vector2> verts = new List <Vector2>(sn.Elements.Count);

                                    foreach (XMLFragmentElement vert in sn.Elements)
                                    {
                                        verts.Add(ReadVector(vert));
                                    }

                                    shape.Vertices = new Vertices(verts);
                                }
                                break;

                                case "centroid":
                                    shape.MassData.Centroid = ReadVector(sn);
                                    break;
                                }
                            }

                            shapes.Add(shape);
                        }
                        break;

                        case ShapeType.Edge:
                        {
                            EdgeShape shape = new EdgeShape();
                            shape._density = density;

                            foreach (XMLFragmentElement sn in element.Elements)
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "hasvertex0":
                                    shape.HasVertex0 = bool.Parse(sn.Value);
                                    break;

                                case "hasvertex3":
                                    shape.HasVertex0 = bool.Parse(sn.Value);
                                    break;

                                case "vertex0":
                                    shape.Vertex0 = ReadVector(sn);
                                    break;

                                case "vertex1":
                                    shape.Vertex1 = ReadVector(sn);
                                    break;

                                case "vertex2":
                                    shape.Vertex2 = ReadVector(sn);
                                    break;

                                case "vertex3":
                                    shape.Vertex3 = ReadVector(sn);
                                    break;

                                default:
                                    throw new Exception();
                                }
                            }
                            shapes.Add(shape);
                        }
                        break;

                        case ShapeType.Chain:
                        {
                            ChainShape shape = new ChainShape();
                            shape._density = density;

                            foreach (XMLFragmentElement sn in element.Elements)
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "vertices":
                                {
                                    List <Vector2> verts = new List <Vector2>(sn.Elements.Count);

                                    foreach (XMLFragmentElement vert in sn.Elements)
                                    {
                                        verts.Add(ReadVector(vert));
                                    }

                                    shape.Vertices = new Vertices(verts);
                                }
                                break;

                                case "nextvertex":
                                    shape.NextVertex = ReadVector(sn);
                                    break;

                                case "prevvertex":
                                    shape.PrevVertex = ReadVector(sn);
                                    break;

                                default:
                                    throw new Exception();
                                }
                            }
                            shapes.Add(shape);
                        }
                        break;
                        }
                    }
                }
            }

            //Read fixtures
            foreach (XMLFragmentElement fixtureElement in root.Elements)
            {
                if (fixtureElement.Name.ToLower() == "fixtures")
                {
                    foreach (XMLFragmentElement element in fixtureElement.Elements)
                    {
                        Fixture fixture = new Fixture();

                        if (element.Name.ToLower() != "fixture")
                        {
                            throw new Exception();
                        }

                        fixture.FixtureId = int.Parse(element.Attributes[0].Value);

                        foreach (XMLFragmentElement sn in element.Elements)
                        {
                            switch (sn.Name.ToLower())
                            {
                            case "filterdata":
                                foreach (XMLFragmentElement ssn in sn.Elements)
                                {
                                    switch (ssn.Name.ToLower())
                                    {
                                    case "categorybits":
                                        fixture._collisionCategories = (Category)int.Parse(ssn.Value);
                                        break;

                                    case "maskbits":
                                        fixture._collidesWith = (Category)int.Parse(ssn.Value);
                                        break;

                                    case "groupindex":
                                        fixture._collisionGroup = short.Parse(ssn.Value);
                                        break;

                                    case "CollisionIgnores":
                                        string[] split = ssn.Value.Split('|');
                                        foreach (string s in split)
                                        {
                                            fixture._collisionIgnores.Add(int.Parse(s));
                                        }
                                        break;
                                    }
                                }

                                break;

                            case "friction":
                                fixture.Friction = float.Parse(sn.Value);
                                break;

                            case "issensor":
                                fixture.IsSensor = bool.Parse(sn.Value);
                                break;

                            case "restitution":
                                fixture.Restitution = float.Parse(sn.Value);
                                break;

                            case "userdata":
                                fixture.UserData = ReadSimpleType(sn, null, false);
                                break;
                            }
                        }

                        fixtures.Add(fixture);
                    }
                }
            }

            //Read bodies
            foreach (XMLFragmentElement bodyElement in root.Elements)
            {
                if (bodyElement.Name.ToLower() == "bodies")
                {
                    foreach (XMLFragmentElement element in bodyElement.Elements)
                    {
                        Body body = new Body(world);

                        if (element.Name.ToLower() != "body")
                        {
                            throw new Exception();
                        }

                        body.BodyType = (BodyType)Enum.Parse(typeof(BodyType), element.Attributes[0].Value, true);

                        foreach (XMLFragmentElement sn in element.Elements)
                        {
                            switch (sn.Name.ToLower())
                            {
                            case "active":
                                bool enabled = bool.Parse(sn.Value);
                                if (enabled)
                                {
                                    body._flags |= BodyFlags.Enabled;
                                }
                                else
                                {
                                    body._flags &= ~BodyFlags.Enabled;
                                }
                                break;

                            case "allowsleep":
                                body.SleepingAllowed = bool.Parse(sn.Value);
                                break;

                            case "angle":
                            {
                                Vector2 position = body.Position;
                                body.SetTransformIgnoreContacts(ref position, float.Parse(sn.Value));
                            }
                            break;

                            case "angulardamping":
                                body.AngularDamping = float.Parse(sn.Value);
                                break;

                            case "angularvelocity":
                                body.AngularVelocity = float.Parse(sn.Value);
                                break;

                            case "awake":
                                body.Awake = bool.Parse(sn.Value);
                                break;

                            case "bullet":
                                body.IsBullet = bool.Parse(sn.Value);
                                break;

                            case "fixedrotation":
                                body.FixedRotation = bool.Parse(sn.Value);
                                break;

                            case "lineardamping":
                                body.LinearDamping = float.Parse(sn.Value);
                                break;

                            case "linearvelocity":
                                body.LinearVelocity = ReadVector(sn);
                                break;

                            case "position":
                            {
                                float   rotation = body.Rotation;
                                Vector2 position = ReadVector(sn);
                                body.SetTransformIgnoreContacts(ref position, rotation);
                            }
                            break;

                            case "userdata":
                                body.UserData = ReadSimpleType(sn, null, false);
                                break;

                            case "bindings":
                            {
                                foreach (XMLFragmentElement pair in sn.Elements)
                                {
                                    Fixture fix = fixtures[int.Parse(pair.Attributes[0].Value)];
                                    fix.Shape = shapes[int.Parse(pair.Attributes[1].Value)].Clone();
                                    fix.CloneOnto(body);
                                }
                                break;
                            }
                            }
                        }

                        bodies.Add(body);
                    }
                }
            }

            //Read joints
            foreach (XMLFragmentElement jointElement in root.Elements)
            {
                if (jointElement.Name.ToLower() == "joints")
                {
                    foreach (XMLFragmentElement n in jointElement.Elements)
                    {
                        Joint joint;

                        if (n.Name.ToLower() != "joint")
                        {
                            throw new Exception();
                        }

                        JointType type = (JointType)Enum.Parse(typeof(JointType), n.Attributes[0].Value, true);

                        int    bodyAIndex = -1, bodyBIndex = -1;
                        bool   collideConnected = false;
                        object userData         = null;

                        foreach (XMLFragmentElement sn in n.Elements)
                        {
                            switch (sn.Name.ToLower())
                            {
                            case "bodya":
                                bodyAIndex = int.Parse(sn.Value);
                                break;

                            case "bodyb":
                                bodyBIndex = int.Parse(sn.Value);
                                break;

                            case "collideconnected":
                                collideConnected = bool.Parse(sn.Value);
                                break;

                            case "userdata":
                                userData = ReadSimpleType(sn, null, false);
                                break;
                            }
                        }

                        Body bodyA = bodies[bodyAIndex];
                        Body bodyB = bodies[bodyBIndex];

                        switch (type)
                        {
                        //case JointType.FixedMouse:
                        //    joint = new FixedMouseJoint();
                        //    break;
                        //case JointType.FixedRevolute:
                        //    break;
                        //case JointType.FixedDistance:
                        //    break;
                        //case JointType.FixedLine:
                        //    break;
                        //case JointType.FixedPrismatic:
                        //    break;
                        //case JointType.FixedAngle:
                        //    break;
                        //case JointType.FixedFriction:
                        //    break;
                        case JointType.Distance:
                            joint = new DistanceJoint();
                            break;

                        case JointType.Friction:
                            joint = new FrictionJoint();
                            break;

                        case JointType.Wheel:
                            joint = new WheelJoint();
                            break;

                        case JointType.Prismatic:
                            joint = new PrismaticJoint();
                            break;

                        case JointType.Pulley:
                            joint = new PulleyJoint();
                            break;

                        case JointType.Revolute:
                            joint = new RevoluteJoint();
                            break;

                        case JointType.Weld:
                            joint = new WeldJoint();
                            break;

                        case JointType.Rope:
                            joint = new RopeJoint();
                            break;

                        case JointType.Angle:
                            joint = new AngleJoint();
                            break;

                        case JointType.Motor:
                            joint = new MotorJoint();
                            break;

                        case JointType.Gear:
                            throw new Exception("GearJoint is not supported.");

                        default:
                            throw new Exception("Invalid or unsupported joint.");
                        }

                        joint.CollideConnected = collideConnected;
                        joint.UserData         = userData;
                        joint.BodyA            = bodyA;
                        joint.BodyB            = bodyB;
                        joints.Add(joint);
                        world.AddJoint(joint);

                        foreach (XMLFragmentElement sn in n.Elements)
                        {
                            // check for specific nodes
                            switch (type)
                            {
                            case JointType.Distance:
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "dampingratio":
                                    ((DistanceJoint)joint).DampingRatio = float.Parse(sn.Value);
                                    break;

                                case "frequencyhz":
                                    ((DistanceJoint)joint).Frequency = float.Parse(sn.Value);
                                    break;

                                case "length":
                                    ((DistanceJoint)joint).Length = float.Parse(sn.Value);
                                    break;

                                case "localanchora":
                                    ((DistanceJoint)joint).LocalAnchorA = ReadVector(sn);
                                    break;

                                case "localanchorb":
                                    ((DistanceJoint)joint).LocalAnchorB = ReadVector(sn);
                                    break;
                                }
                            }
                            break;

                            case JointType.Friction:
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "localanchora":
                                    ((FrictionJoint)joint).LocalAnchorA = ReadVector(sn);
                                    break;

                                case "localanchorb":
                                    ((FrictionJoint)joint).LocalAnchorB = ReadVector(sn);
                                    break;

                                case "maxforce":
                                    ((FrictionJoint)joint).MaxForce = float.Parse(sn.Value);
                                    break;

                                case "maxtorque":
                                    ((FrictionJoint)joint).MaxTorque = float.Parse(sn.Value);
                                    break;
                                }
                            }
                            break;

                            case JointType.Wheel:
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "enablemotor":
                                    ((WheelJoint)joint).MotorEnabled = bool.Parse(sn.Value);
                                    break;

                                case "localanchora":
                                    ((WheelJoint)joint).LocalAnchorA = ReadVector(sn);
                                    break;

                                case "localanchorb":
                                    ((WheelJoint)joint).LocalAnchorB = ReadVector(sn);
                                    break;

                                case "motorspeed":
                                    ((WheelJoint)joint).MotorSpeed = float.Parse(sn.Value);
                                    break;

                                case "dampingratio":
                                    ((WheelJoint)joint).DampingRatio = float.Parse(sn.Value);
                                    break;

                                case "maxmotortorque":
                                    ((WheelJoint)joint).MaxMotorTorque = float.Parse(sn.Value);
                                    break;

                                case "frequencyhz":
                                    ((WheelJoint)joint).Frequency = float.Parse(sn.Value);
                                    break;

                                case "axis":
                                    ((WheelJoint)joint).Axis = ReadVector(sn);
                                    break;
                                }
                            }
                            break;

                            case JointType.Prismatic:
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "enablelimit":
                                    ((PrismaticJoint)joint).LimitEnabled = bool.Parse(sn.Value);
                                    break;

                                case "enablemotor":
                                    ((PrismaticJoint)joint).MotorEnabled = bool.Parse(sn.Value);
                                    break;

                                case "localanchora":
                                    ((PrismaticJoint)joint).LocalAnchorA = ReadVector(sn);
                                    break;

                                case "localanchorb":
                                    ((PrismaticJoint)joint).LocalAnchorB = ReadVector(sn);
                                    break;

                                case "axis":
                                    ((PrismaticJoint)joint).Axis = ReadVector(sn);
                                    break;

                                case "maxmotorforce":
                                    ((PrismaticJoint)joint).MaxMotorForce = float.Parse(sn.Value);
                                    break;

                                case "motorspeed":
                                    ((PrismaticJoint)joint).MotorSpeed = float.Parse(sn.Value);
                                    break;

                                case "lowertranslation":
                                    ((PrismaticJoint)joint).LowerLimit = float.Parse(sn.Value);
                                    break;

                                case "uppertranslation":
                                    ((PrismaticJoint)joint).UpperLimit = float.Parse(sn.Value);
                                    break;

                                case "referenceangle":
                                    ((PrismaticJoint)joint).ReferenceAngle = float.Parse(sn.Value);
                                    break;
                                }
                            }
                            break;

                            case JointType.Pulley:
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "worldanchora":
                                    ((PulleyJoint)joint).WorldAnchorA = ReadVector(sn);
                                    break;

                                case "worldanchorb":
                                    ((PulleyJoint)joint).WorldAnchorB = ReadVector(sn);
                                    break;

                                case "lengtha":
                                    ((PulleyJoint)joint).LengthA = float.Parse(sn.Value);
                                    break;

                                case "lengthb":
                                    ((PulleyJoint)joint).LengthB = float.Parse(sn.Value);
                                    break;

                                case "localanchora":
                                    ((PulleyJoint)joint).LocalAnchorA = ReadVector(sn);
                                    break;

                                case "localanchorb":
                                    ((PulleyJoint)joint).LocalAnchorB = ReadVector(sn);
                                    break;

                                case "ratio":
                                    ((PulleyJoint)joint).Ratio = float.Parse(sn.Value);
                                    break;

                                case "constant":
                                    ((PulleyJoint)joint).Constant = float.Parse(sn.Value);
                                    break;
                                }
                            }
                            break;

                            case JointType.Revolute:
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "enablelimit":
                                    ((RevoluteJoint)joint).LimitEnabled = bool.Parse(sn.Value);
                                    break;

                                case "enablemotor":
                                    ((RevoluteJoint)joint).MotorEnabled = bool.Parse(sn.Value);
                                    break;

                                case "localanchora":
                                    ((RevoluteJoint)joint).LocalAnchorA = ReadVector(sn);
                                    break;

                                case "localanchorb":
                                    ((RevoluteJoint)joint).LocalAnchorB = ReadVector(sn);
                                    break;

                                case "maxmotortorque":
                                    ((RevoluteJoint)joint).MaxMotorTorque = float.Parse(sn.Value);
                                    break;

                                case "motorspeed":
                                    ((RevoluteJoint)joint).MotorSpeed = float.Parse(sn.Value);
                                    break;

                                case "lowerangle":
                                    ((RevoluteJoint)joint).LowerLimit = float.Parse(sn.Value);
                                    break;

                                case "upperangle":
                                    ((RevoluteJoint)joint).UpperLimit = float.Parse(sn.Value);
                                    break;

                                case "referenceangle":
                                    ((RevoluteJoint)joint).ReferenceAngle = float.Parse(sn.Value);
                                    break;
                                }
                            }
                            break;

                            case JointType.Weld:
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "localanchora":
                                    ((WeldJoint)joint).LocalAnchorA = ReadVector(sn);
                                    break;

                                case "localanchorb":
                                    ((WeldJoint)joint).LocalAnchorB = ReadVector(sn);
                                    break;
                                }
                            }
                            break;

                            case JointType.Rope:
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "localanchora":
                                    ((RopeJoint)joint).LocalAnchorA = ReadVector(sn);
                                    break;

                                case "localanchorb":
                                    ((RopeJoint)joint).LocalAnchorB = ReadVector(sn);
                                    break;

                                case "maxlength":
                                    ((RopeJoint)joint).MaxLength = float.Parse(sn.Value);
                                    break;
                                }
                            }
                            break;

                            case JointType.Gear:
                                throw new Exception("Gear joint is unsupported");

                            case JointType.Angle:
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "biasfactor":
                                    ((AngleJoint)joint).BiasFactor = float.Parse(sn.Value);
                                    break;

                                case "maximpulse":
                                    ((AngleJoint)joint).MaxImpulse = float.Parse(sn.Value);
                                    break;

                                case "softness":
                                    ((AngleJoint)joint).Softness = float.Parse(sn.Value);
                                    break;

                                case "targetangle":
                                    ((AngleJoint)joint).TargetAngle = float.Parse(sn.Value);
                                    break;
                                }
                            }
                            break;

                            case JointType.Motor:
                                switch (sn.Name.ToLower())
                                {
                                case "angularoffset":
                                    ((MotorJoint)joint).AngularOffset = float.Parse(sn.Value);
                                    break;

                                case "linearoffset":
                                    ((MotorJoint)joint).LinearOffset = ReadVector(sn);
                                    break;

                                case "maxforce":
                                    ((MotorJoint)joint).MaxForce = float.Parse(sn.Value);
                                    break;

                                case "maxtorque":
                                    ((MotorJoint)joint).MaxTorque = float.Parse(sn.Value);
                                    break;

                                case "correctionfactor":
                                    ((MotorJoint)joint).CorrectionFactor = float.Parse(sn.Value);
                                    break;
                                }
                                break;
                            }
                        }
                    }
                }
            }

            world.ProcessChanges();
        }
Пример #26
0
        private void DrawJoint(Joint joint)
        {
            Body      b1 = joint.BodyA;
            Body      b2 = joint.BodyB;
            Transform xf1, xf2;

            b1.GetTransform(out xf1);

            Vector2 x2 = new Vector2();

            // WIP David
            if (!joint.IsFixedType())
            {
                b2.GetTransform(out xf2);
                x2 = xf2.Position;
            }
            Vector2 p2 = joint.WorldAnchorB;

            Vector2 x1 = xf1.Position;

            Vector2 p1 = joint.WorldAnchorA;

            Color color = new Color(0.5f, 0.8f, 0.8f);

            switch (joint.JointType)
            {
            case JointType.Distance:
                DrawSegment(p1, p2, color);
                break;

            case JointType.Pulley:
            {
                PulleyJoint pulley = (PulleyJoint)joint;
                Vector2     s1     = pulley.GroundAnchorA;
                Vector2     s2     = pulley.GroundAnchorB;
                DrawSegment(s1, p1, color);
                DrawSegment(s2, p2, color);
                DrawSegment(s1, s2, color);
            }
            break;

            case JointType.FixedMouse:

                FixedMouseJoint fixedMouseJoint = (FixedMouseJoint)joint;
                p1 = fixedMouseJoint.Target;

                DrawPoint(p2, 0.5f, new Color(0.0f, 1.0f, 0.0f));
                DrawSegment(p1, p2, new Color(0.8f, 0.8f, 0.8f));
                break;

            case JointType.Revolute:
                //DrawSegment(x2, p1, color);
                DrawSegment(p2, p1, color);
                DrawSolidCircle(p2, 0.1f, new Vector2(), Color.Red);
                DrawSolidCircle(p1, 0.1f, new Vector2(), Color.Blue);
                break;

            case JointType.FixedRevolute:
                DrawSolidCircle(p1, 0.1f, new Vector2(), Color.Pink);
                break;

            case JointType.FixedLine:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.FixedDistance:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.FixedPrismatic:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                break;

            case JointType.Gear:
                DrawSegment(x1, x2, color);
                //DrawSegment(x1, p1, color);
                //DrawSegment(p1, p2, color);
                break;

            default:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                DrawSegment(x2, p2, color);
                break;
            }
        }
Пример #27
0
        private static void DrawJoint(FSDebugView instance, Joint joint)
        {
            if (!joint.Enabled)
            {
                return;
            }

            Body b1 = joint.BodyA;
            Body b2 = joint.BodyB;

            FarseerPhysics.Common.Transform xf1;
            b1.GetTransform(out xf1);

            Vector2 x2 = Vector2.Zero;

            if (b2 != null || !joint.IsFixedType())
            {
                FarseerPhysics.Common.Transform xf2;
                b2.GetTransform(out xf2);
                x2 = xf2.P;
            }

            Vector2 p1 = joint.WorldAnchorA;
            Vector2 p2 = joint.WorldAnchorB;
            Vector2 x1 = xf1.P;

            Color color = new Color(0.5f, 0.8f, 0.8f);

            switch (joint.JointType)
            {
            case JointType.Distance: {
                instance.DrawSegment(p1, p2, color);
                break;
            }

            case JointType.Pulley: {
                PulleyJoint pulley = (PulleyJoint)joint;
                Vector2     s1     = b1.GetWorldPoint(pulley.LocalAnchorA);
                Vector2     s2     = b2.GetWorldPoint(pulley.LocalAnchorB);
                instance.DrawSegment(p1, p2, color);
                instance.DrawSegment(p1, s1, color);
                instance.DrawSegment(p2, s2, color);
                break;
            }

            case JointType.FixedMouse: {
                instance.DrawPoint(p1, 0.2f, new Color(0.0f, 1.0f, 0.0f));
                instance.DrawSegment(p1, p2, new Color(0.8f, 0.8f, 0.8f));
                break;
            }

            case JointType.Revolute: {
                instance.DrawSegment(x1, p1, color);
                instance.DrawSegment(p1, p2, color);
                instance.DrawSegment(x2, p2, color);

                instance.DrawSolidCircle(p2, 0.1f, Vector2.Zero, Color.Red);
                instance.DrawSolidCircle(p1, 0.1f, Vector2.Zero, Color.Blue);
                break;
            }

            case JointType.Gear: {
                instance.DrawSegment(x1, x2, color);
                break;
            }

            default: {
                instance.DrawSegment(x1, p1, color);
                instance.DrawSegment(p1, p2, color);
                instance.DrawSegment(x2, p2, color);
                break;
            }
            }
        }
Пример #28
0
 public static PulleyJoint CreatePulleyJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB, Vector2 worldAnchorA, Vector2 worldAnchorB, GGame.Math.Fix64 ratio, bool useWorldCoordinates = false)
 {
     PulleyJoint pulleyJoint = new PulleyJoint(bodyA, bodyB, anchorA, anchorB, worldAnchorA, worldAnchorB, ratio, useWorldCoordinates);
     world.AddJoint(pulleyJoint);
     return pulleyJoint;
 }