Пример #1
0
    // Use this for initialization
    protected override IntPtr Init()
    {
        PulleyJointDef jd = new PulleyJointDef(other.body, body.body);

        jd.Initialize(other.body, body.body, groundAnchorA, groundAnchorB, anchorA, anchorB, ratio);
        return(API.CreatePulleyJoint(B2DWorld.instance.world, jd));
    }
Пример #2
0
        public PulleyJoint()
        {
            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 = (Box2DSharp.Dynamics.Joints.PulleyJoint)World.CreateJoint(pulleyDef);
            }
        }
Пример #3
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);
        }
Пример #4
0
        public Pulleys()
        {
            float y = 16.0f;
            float L = 12.0f;
            float a = 1.0f;
            float b = 2.0f;

            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                EdgeShape edge = new EdgeShape();
                edge.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                //ground.CreateFixture(shape, 0.0f);

                CircleShape circle = new CircleShape();
                circle.m_radius = 2.0f;

                circle.m_p.Set(-10.0f, y + b + L);
                circle.Density = 0;
                ground.CreateFixture(circle);

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

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

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

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

                bd.Position.Set(10.0f, y);
                Body body2 = m_world.CreateBody(bd);
                body2.CreateFixture(shape);

                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, 1.5f);

                m_joint1 = (PulleyJoint)m_world.CreateJoint(pulleyDef);
            }
        }
Пример #5
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);
            }
        }
Пример #6
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="PulleyJoint" /> class
        /// </summary>
        /// <param name="def">The def</param>
        public PulleyJoint(PulleyJointDef def)
            : base(def)
        {
            worldAnchorA = def.GroundAnchorA;
            worldAnchorB = def.GroundAnchorB;
            localAnchorA = def.LocalAnchorA;
            localAnchorB = def.LocalAnchorB;

            lengthA = def.LengthA;
            lengthB = def.LengthB;

            Debug.Assert(def.Ratio != 0.0f);
            ratio = def.Ratio;

            constant = def.LengthA + ratio * def.LengthB;
        }
Пример #7
0
    void Start()
    {
        var jointDef = new PulleyJointDef();

#if USING_BOX2DX
        jointDef.Initialize(GetComponent <Box2DBody>(), connectedBody,
                            groundAnchor1.ToVec2(), groundAnchor2.ToVec2(), anchor1.ToVec2(), anchor2.ToVec2(), ratio);
#else
        //jointDef.Length1 = 10f;

        jointDef.Initialize(GetComponent <Box2DBody>(), connectedBody, groundAnchor1, groundAnchor2, anchor1, anchor2, ratio);

        jointDef.MaxLength1 = 10f;
        jointDef.MaxLength2 = 10f;
#endif
        joint = (PulleyJoint)Box2DWorld.Instance().CreateJoint(jointDef);
    }
Пример #8
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);
            }
        }
Пример #9
0
        public void ApplyAttribtues(PulleyJointDef j)
        {
            j.collideConnected = collideConnected;

            if (maxLength1 != 0.0f)
            {
                j.maxLengthA = maxLength1;
            }
            if (maxLength2 != 0.0f)
            {
                j.maxLengthB = maxLength2;
            }

            if (ratio != 1.0f)
            {
                j.ratio = ratio;
            }
        }
Пример #10
0
        public Pulleys()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(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.BodyType = BodyType.Dynamic;

                bd.Position = new Vec2(-10.0f, y);
                Body body1 = m_world.CreateBody(bd);
                body1.CreateFixture(shape, 5.0f);

                bd.Position = new Vec2(10.0f, y);
                Body body2 = m_world.CreateBody(bd);
                body2.CreateFixture(shape, 5.0f);

                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);

                m_joint1 = (PulleyJoint)m_world.CreateJoint(pulleyDef);
            }
        }
Пример #11
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);
            }
        }
Пример #12
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);
            }
        }
Пример #13
0
        public static Joint AddJoint(this IJointable ithis, V2DJoint joint, float offsetX, float offsetY)
        {
            Joint    result   = null;
            JointDef jointDef = null;
            //Body targ0 = ithis.VScreen.bodyMap[joint.Body1];
            //Body targ1 = ithis.VScreen.bodyMap[joint.Body2];
            Body targ0 = GetBody(ithis, joint.Body1);
            Body targ1 = GetBody(ithis, joint.Body2);

            // gears need the first body static
            if (targ0 != null && targ1 != null && targ1.GetType() == BodyType.Static && targ0.GetType() != BodyType.Static)
            {
                Body temp = targ0;
                targ0 = targ1;
                targ1 = temp;
            }

            Vector2 pt0 = new Vector2(joint.X + offsetX, joint.Y + offsetY);

            string name = joint.Name;

            Vector2 anchor0 = new Vector2(pt0.X / V2DScreen.WorldScale, pt0.Y / V2DScreen.WorldScale);
            Vector2 anchor1 = new Vector2();

            switch (joint.Type)
            {
            case V2DJointKind.Distance:
                Vector2 pt1 = new Vector2(joint.X2 + offsetX, joint.Y2 + offsetY);
                anchor1 = new Vector2(pt1.X / V2DScreen.WorldScale, pt1.Y / V2DScreen.WorldScale);

                DistanceJointDef dj = new DistanceJointDef();
                dj.Initialize(targ0, targ1, anchor0, anchor1);
                dj.collideConnected = joint.CollideConnected;
                dj.dampingRatio     = joint.DampingRatio;
                dj.frequencyHz      = joint.FrequencyHz;
                if (joint.Length != -1)
                {
                    dj.length = joint.Length / V2DScreen.WorldScale;
                }

                jointDef = dj;
                break;

            case V2DJointKind.Revolute:
                float rot0 = joint.Min;                         //(typeof(joint["min"]) == "string") ? parseFloat(joint["min"]) / 180 * Math.PI : joint["min"];
                float rot1 = joint.Max;                         //(typeof(joint["max"]) == "string") ? parseFloat(joint["max"]) / 180 * Math.PI : joint["max"];

                RevoluteJointDef rj = new RevoluteJointDef();
                rj.Initialize(targ0, targ1, anchor0);
                rj.lowerAngle = rot0;
                rj.upperAngle = rot1;

                rj.enableLimit    = rot0 != 0 && rot1 != 0;
                rj.maxMotorTorque = joint.MaxMotorTorque;
                rj.motorSpeed     = joint.MotorSpeed;
                rj.enableMotor    = joint.EnableMotor;

                jointDef = rj;
                break;

            case V2DJointKind.Prismatic:
                float axisX = joint.AxisX;
                float axisY = joint.AxisY;
                float min   = joint.Min;
                float max   = joint.Max;

                PrismaticJointDef pj        = new PrismaticJointDef();
                Vector2           worldAxis = new Vector2(axisX, axisY);
                pj.Initialize(targ0, targ1, anchor0, worldAxis);
                pj.lowerTranslation = min / V2DScreen.WorldScale;
                pj.upperTranslation = max / V2DScreen.WorldScale;

                pj.enableLimit   = joint.EnableLimit;
                pj.maxMotorForce = joint.MaxMotorTorque;
                pj.motorSpeed    = joint.MotorSpeed;
                pj.enableMotor   = joint.EnableMotor;

                jointDef = pj;
                break;

            case V2DJointKind.Pully:
                Vector2 pt2 = new Vector2(joint.X2 + offsetX, joint.Y2 + offsetY);
                anchor1 = new Vector2(pt2.X / V2DScreen.WorldScale, pt2.Y / V2DScreen.WorldScale);

                Vector2 groundAnchor0 = new Vector2(joint.GroundAnchor1X / V2DScreen.WorldScale, joint.GroundAnchor1Y / V2DScreen.WorldScale);

                Vector2 groundAnchor1 = new Vector2(joint.GroundAnchor2X / V2DScreen.WorldScale, joint.GroundAnchor2Y / V2DScreen.WorldScale);

                float max0 = joint.MaxLength1;
                float max1 = joint.MaxLength2;

                float rat = joint.Ratio;

                PulleyJointDef puj = new PulleyJointDef();
                puj.Initialize(targ0, targ1, groundAnchor0, groundAnchor1, anchor0, anchor1, rat);
                puj.maxLengthA = (max0 + max1) / V2DScreen.WorldScale;
                puj.maxLengthB = (max0 + max1) / V2DScreen.WorldScale;

                puj.collideConnected = joint.CollideConnected;

                jointDef = puj;
                break;

            case V2DJointKind.Gear:
                GearJointDef gj = new GearJointDef();
                gj.bodyA  = targ0;
                gj.bodyB  = targ1;
                gj.joint1 = GetFirstGearableJoint(targ0.GetJointList());
                gj.joint2 = GetFirstGearableJoint(targ1.GetJointList());
                gj.ratio  = joint.Ratio;
                jointDef  = gj;
                break;
            }

            if (jointDef != null)
            {
                result = SetJointWithReflection(ithis, name, jointDef);

                if (result != null)
                {
                    Dictionary <string, string> dict = new Dictionary <string, string>();
                    dict["name"] = name;
                    result.SetUserData(dict);
                }
            }

            return(result);
        }
Пример #14
0
        public WorldData Deserialize(Stream stream)
        {
            XMLFragmentElement root = XMLFragmentParser.LoadFromStream(stream);

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

            WorldData data = new WorldData();

            if (root.Attributes.Count == 0)
                throw new Exception("No version");
            else if (int.Parse(root.Attributes[0].Value) != WorldXmlSerializer.XmlVersion)
                throw new Exception("Wrong version XML file");

            data.Version = int.Parse(root.Attributes[0].Value);

            foreach (var main in root.Elements)
            {
                switch (main.Name.ToLower())
                {
                case "gravity":
                    {
                        data.Gravity = ReadVector(main);
                    }
                    break;

                case "shapes":
                    {
                        foreach (var n in main.Elements)
                        {
                            if (n.Name.ToLower() != "shape")
                                throw new Exception();

                            ShapeType type = (ShapeType)Enum.Parse(typeof(ShapeType), n.Attributes[0].Value, true);
                            string name = "";

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

                                    foreach (var sn in n.Elements)
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "name":
                                            name = sn.Value;
                                            break;
                                        case "radius":
                                            shape.Radius = float.Parse(sn.Value);
                                            break;
                                        case "position":
                                            shape.Position = ReadVector(sn);
                                            break;
                                        default:
                                            throw new Exception();
                                        }
                                    }

                                    _shapes.Add(new ShapeSerialized(shape, name));
                                }
                                break;
                            case ShapeType.Polygon:
                                {
                                    PolygonShape shape = new PolygonShape();

                                    foreach (var sn in n.Elements)
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "name":
                                            name = sn.Value;
                                            break;
                                        case "vertices":
                                            {
                                                List<Vec2> verts = new List<Vec2>();

                                                foreach (var vert in sn.Elements)
                                                    verts.Add(ReadVector(vert));

                                                shape.Vertices = verts.ToArray();
                                            }
                                            break;
                                        case "centroid":
                                            shape.Centroid = ReadVector(sn);
                                            break;
                                        }
                                    }

                                    _shapes.Add(new ShapeSerialized(shape, name));
                                }
                                break;
                            }
                        }
                    }
                    break;
                case "fixtures":
                    {
                        foreach (var n in main.Elements)
                        {
                            FixtureDef fixture = new FixtureDef();

                            if (n.Name.ToLower() != "fixture")
                                throw new Exception();

                            string name = "";
                            int id = 0;

                            foreach (var sn in n.Elements)
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "name":
                                    name = sn.Value;
                                    break;
                                case "shape":
                                    id = int.Parse(sn.Value);
                                    break;
                                case "density":
                                    fixture.Density = float.Parse(sn.Value);
                                    break;
                                case "filterdata":
                                    fixture.Filter = (FilterData)ReadSimpleType(sn, typeof(FilterData), true);
                                    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;
                                }
                            }

                            fixture.Shape = _shapes[id].Shape;

                            _fixtures.Add(new FixtureDefSerialized(fixture, id, name));
                        }
                    }
                    break;
                case "bodies":
                    {
                        foreach (var n in main.Elements)
                        {
                            BodyDef body = new BodyDef();

                            if (n.Name.ToLower() != "body")
                                throw new Exception();

                            body.BodyType = (BodyType)Enum.Parse(typeof(BodyType), n.Attributes[0].Value, true);
                            List<int> fixtures = new List<int>();
                            string name = "";

                            foreach (var sn in n.Elements)
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "name":
                                    name = sn.Value;
                                    break;
                                case "active":
                                    body.Active = bool.Parse(sn.Value);
                                    break;
                                case "allowsleep":
                                    body.AllowSleep = bool.Parse(sn.Value);
                                    break;
                                case "angle":
                                    body.Angle = 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.Bullet = bool.Parse(sn.Value);
                                    break;
                                case "fixedrotation":
                                    body.FixedRotation = bool.Parse(sn.Value);
                                    break;
                                case "inertiascale":
                                    body.InertiaScale = float.Parse(sn.Value);
                                    break;
                                case "lineardamping":
                                    body.LinearDamping = float.Parse(sn.Value);
                                    break;
                                case "linearvelocity":
                                    body.LinearVelocity = ReadVector(sn);
                                    break;
                                case "position":
                                    body.Position = ReadVector(sn);
                                    break;
                                case "userdata":
                                    body.UserData = ReadSimpleType(sn, null, false);
                                    break;
                                case "fixtures":
                                    {
                                        foreach (var v in sn.Elements)
                                            fixtures.Add(int.Parse(v.Value));
                                        break;
                                    }
                                }
                            }

                            _bodies.Add(new BodyDefSerialized(null, body, fixtures, name));
                        }
                    }
                    break;
                case "joints":
                    {
                        foreach (var n in main.Elements)
                        {
                            JointDef mainDef = null;

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

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

                            int bodyA = -1, bodyB = -1;
                            bool collideConnected = false;
                            object userData = null;
                            string name = "";

                            switch (type)
                            {
                            case JointType.Distance:
                                mainDef = new DistanceJointDef();
                                break;
                            case JointType.Friction:
                                mainDef = new FrictionJointDef();
                                break;
                            case JointType.Line:
                                mainDef = new LineJointDef();
                                break;
                            case JointType.Prismatic:
                                mainDef = new PrismaticJointDef();
                                break;
                            case JointType.Pulley:
                                mainDef = new PulleyJointDef();
                                break;
                            case JointType.Revolute:
                                mainDef = new RevoluteJointDef();
                                break;
                            case JointType.Weld:
                                mainDef = new WeldJointDef();
                                break;
                            default:
                                throw new Exception("Invalid or unsupported joint");
                            }

                            foreach (var sn in n.Elements)
                            {
                                // check for specific nodes
                                switch (type)
                                {
                                case JointType.Distance:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "dampingratio":
                                            ((DistanceJointDef)mainDef).DampingRatio = float.Parse(sn.Value);
                                            break;
                                        case "frequencyhz":
                                            ((DistanceJointDef)mainDef).FrequencyHz = float.Parse(sn.Value);
                                            break;
                                        case "length":
                                            ((DistanceJointDef)mainDef).Length = float.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((DistanceJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((DistanceJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Friction:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "localanchora":
                                            ((FrictionJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((FrictionJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "maxforce":
                                            ((FrictionJointDef)mainDef).MaxForce = float.Parse(sn.Value);
                                            break;
                                        case "maxtorque":
                                            ((FrictionJointDef)mainDef).MaxTorque = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Line:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "enablelimit":
                                            ((LineJointDef)mainDef).EnableLimit = bool.Parse(sn.Value);
                                            break;
                                        case "enablemotor":
                                            ((LineJointDef)mainDef).EnableMotor = bool.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((LineJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((LineJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "localaxisa":
                                            ((LineJointDef)mainDef).LocalAxisA = ReadVector(sn);
                                            break;
                                        case "maxmotorforce":
                                            ((LineJointDef)mainDef).MaxMotorForce = float.Parse(sn.Value);
                                            break;
                                        case "motorspeed":
                                            ((LineJointDef)mainDef).MotorSpeed = float.Parse(sn.Value);
                                            break;
                                        case "lowertranslation":
                                            ((LineJointDef)mainDef).LowerTranslation = float.Parse(sn.Value);
                                            break;
                                        case "uppertranslation":
                                            ((LineJointDef)mainDef).UpperTranslation = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Prismatic:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "enablelimit":
                                            ((PrismaticJointDef)mainDef).EnableLimit = bool.Parse(sn.Value);
                                            break;
                                        case "enablemotor":
                                            ((PrismaticJointDef)mainDef).EnableMotor = bool.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((PrismaticJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((PrismaticJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "localaxisa":
                                            ((PrismaticJointDef)mainDef).LocalAxis = ReadVector(sn);
                                            break;
                                        case "maxmotorforce":
                                            ((PrismaticJointDef)mainDef).MaxMotorForce = float.Parse(sn.Value);
                                            break;
                                        case "motorspeed":
                                            ((PrismaticJointDef)mainDef).MotorSpeed = float.Parse(sn.Value);
                                            break;
                                        case "lowertranslation":
                                            ((PrismaticJointDef)mainDef).LowerTranslation = float.Parse(sn.Value);
                                            break;
                                        case "uppertranslation":
                                            ((PrismaticJointDef)mainDef).UpperTranslation = float.Parse(sn.Value);
                                            break;
                                        case "referenceangle":
                                            ((PrismaticJointDef)mainDef).ReferenceAngle = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Pulley:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "groundanchora":
                                            ((PulleyJointDef)mainDef).GroundAnchorA = ReadVector(sn);
                                            break;
                                        case "groundanchorb":
                                            ((PulleyJointDef)mainDef).GroundAnchorB = ReadVector(sn);
                                            break;
                                        case "lengtha":
                                            ((PulleyJointDef)mainDef).LengthA = float.Parse(sn.Value);
                                            break;
                                        case "lengthb":
                                            ((PulleyJointDef)mainDef).LengthB = float.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((PulleyJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((PulleyJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "maxlengtha":
                                            ((PulleyJointDef)mainDef).MaxLengthA = float.Parse(sn.Value);
                                            break;
                                        case "maxlengthb":
                                            ((PulleyJointDef)mainDef).MaxLengthB = float.Parse(sn.Value);
                                            break;
                                        case "ratio":
                                            ((PulleyJointDef)mainDef).Ratio = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Revolute:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "enablelimit":
                                            ((RevoluteJointDef)mainDef).EnableLimit = bool.Parse(sn.Value);
                                            break;
                                        case "enablemotor":
                                            ((RevoluteJointDef)mainDef).EnableMotor = bool.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((RevoluteJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((RevoluteJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "maxmotortorque":
                                            ((RevoluteJointDef)mainDef).MaxMotorTorque = float.Parse(sn.Value);
                                            break;
                                        case "motorspeed":
                                            ((RevoluteJointDef)mainDef).MotorSpeed = float.Parse(sn.Value);
                                            break;
                                        case "lowerangle":
                                            ((RevoluteJointDef)mainDef).LowerAngle = float.Parse(sn.Value);
                                            break;
                                        case "upperangle":
                                            ((RevoluteJointDef)mainDef).UpperAngle = float.Parse(sn.Value);
                                            break;
                                        case "referenceangle":
                                            ((RevoluteJointDef)mainDef).ReferenceAngle = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Weld:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "localanchora":
                                            ((WeldJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((WeldJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Gear:
                                    throw new Exception("Gear joint is unsupported");
                                }

                                switch (sn.Name.ToLower())
                                {
                                case "name":
                                    name = sn.Value;
                                    break;
                                case "bodya":
                                    bodyA = int.Parse(sn.Value);
                                    break;
                                case "bodyb":
                                    bodyB = int.Parse(sn.Value);
                                    break;
                                case "collideconnected":
                                    collideConnected = bool.Parse(sn.Value);
                                    break;
                                case "userdata":
                                    userData = ReadSimpleType(sn, null, false);
                                    break;
                                }
                            }

                            mainDef.CollideConnected = collideConnected;
                            mainDef.UserData = userData;
                            _joints.Add(new JointDefSerialized(mainDef, bodyA, bodyB, name));
                        }
                    }
                    break;
                }
            }

            return data;
        }
Пример #15
0
        public void DrawJoint(Box2CS.Serialize.JointDefSerialized x, Vec2 p1, Vec2 p2, BodyDef bodyA, BodyDef bodyB)
        {
            Transform xf1 = new Transform(bodyA.Position, new Mat22(bodyA.Angle));
            Transform xf2 = new Transform(bodyB.Position, new Mat22(bodyB.Angle));
            Vec2      x1  = xf1.Position;
            Vec2      x2  = xf2.Position;

            p1 = xf1 * p1;
            p2 = xf2 * p2;

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

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

            case JointType.Pulley:
            {
                PulleyJointDef pulley = (PulleyJointDef)x.Joint;
                Vec2           s1     = pulley.GroundAnchorA;
                Vec2           s2     = pulley.GroundAnchorB;
                DrawSegment(s1, p1, color);
                DrawSegment(s2, p2, color);
                DrawSegment(s1, s2, color);
            }
            break;

            case JointType.Revolute:
            {
                RevoluteJointDef rjd = (RevoluteJointDef)x.Joint;

                if (rjd.EnableLimit)
                {
                    Vec2 startPos = p1;
                    Vec2 sinCos   = new Vec2(-(float)Math.Cos((rjd.UpperAngle + rjd.ReferenceAngle) - (float)Math.PI / 2), -(float)Math.Sin((rjd.UpperAngle + rjd.ReferenceAngle) - (float)Math.PI / 2));

                    var end = startPos + (sinCos * 3);
                    DrawSegment(startPos, end, new ColorF(0, 0.65f, 0.65f));

                    sinCos = new Vec2(-(float)Math.Cos((rjd.LowerAngle + rjd.ReferenceAngle) - (float)Math.PI / 2), -(float)Math.Sin((rjd.LowerAngle + rjd.ReferenceAngle) - (float)Math.PI / 2));

                    end = startPos + (sinCos * 3);
                    DrawSegment(startPos, end, new ColorF(0, 0.65f, 0.65f));
                    DrawArc(startPos, 3, (-rjd.LowerAngle - rjd.ReferenceAngle), (-rjd.UpperAngle - rjd.ReferenceAngle));
                }

                DrawCircle(p1, 0.75f, new ColorF(0, 0.65f, 0.65f));

                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                DrawSegment(x2, p2, color);
            }
            break;

            default:
            case JointType.Unknown:
                DrawSegment(x1, p1, color);
                DrawSegment(p1, p2, color);
                DrawSegment(x2, p2, color);
                break;
            }
        }