Example #1
0
        public b2PrismaticJoint(b2PrismaticJointDef def)
            : base(def)
        {
            m_localAnchorA = def.localAnchorA;
            m_localAnchorB = def.localAnchorB;
            m_localXAxisA  = def.localAxisA;
            m_localXAxisA.Normalize();
            m_localYAxisA    = m_localXAxisA.NegUnitCross(); //  b2Math.b2Cross(1.0f, m_localXAxisA);
            m_referenceAngle = def.referenceAngle;

            m_impulse.SetZero();
            m_motorMass    = 0.0f;
            m_motorImpulse = 0.0f;

            m_lowerTranslation = def.lowerTranslation;
            m_upperTranslation = def.upperTranslation;
            m_maxMotorForce    = def.maxMotorForce;
            m_motorSpeed       = def.motorSpeed;
            m_enableLimit      = def.enableLimit;
            m_enableMotor      = def.enableMotor;
            m_limitState       = b2LimitState.e_inactiveLimit;

            m_axis.SetZero();
            m_perp.SetZero();
        }
        //--------------- Internals Below -------------------

        /** @private */
        public b2PrismaticJoint(b2PrismaticJointDef def) : base(def)
        {
            b2Mat22 tMat;
            float   tX;
            float   tY;

            m_localAnchor1.SetV(def.localAnchorA);
            m_localAnchor2.SetV(def.localAnchorB);
            m_localXAxis1.SetV(def.localAxisA);

            //m_localYAxisA = b2Cross(1.0f, m_localXAxisA);
            m_localYAxis1.x = -m_localXAxis1.y;
            m_localYAxis1.y = m_localXAxis1.x;

            m_refAngle = def.referenceAngle;

            m_impulse.SetZero();
            m_motorMass    = 0.0f;
            m_motorImpulse = 0.0f;

            m_lowerTranslation = def.lowerTranslation;
            m_upperTranslation = def.upperTranslation;
            m_maxMotorForce    = def.maxMotorForce;
            m_motorSpeed       = def.motorSpeed;
            m_enableLimit      = def.enableLimit;
            m_enableMotor      = def.enableMotor;
            m_limitState       = e_inactiveLimit;

            m_axis.SetZero();
            m_perp.SetZero();
        }
Example #3
0
        public Prismatic()
        {
            b2Body ground = null;
            {
                b2BodyDef bd  = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(2.0f, 0.5f);

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-10.0f, 10.0f);
                bd.angle = 0.5f * b2Settings.b2_pi;
                bd.allowSleep = false;
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                b2PrismaticJointDef pjd = new b2PrismaticJointDef();

                // Bouncy limit
                b2Vec2 axis = new b2Vec2(2.0f, 1.0f);
                axis.Normalize();
                pjd.Initialize(ground, body, new b2Vec2(0.0f, 0.0f), axis);

                // Non-bouncy limit
                //pjd.Initialize(ground, body, b2Vec2(-10.0f, 10.0f), b2Vec2(1.0f, 0.0f));

                pjd.motorSpeed = 10.0f;
                pjd.maxMotorForce = 10000.0f;
                pjd.enableMotor = true;
                pjd.lowerTranslation = 0.0f;
                pjd.upperTranslation = 20.0f;
                pjd.enableLimit = true;

                m_joint = (b2PrismaticJoint) m_world.CreateJoint(pjd);
            }
        }
Example #4
0
        public b2PrismaticJoint(b2PrismaticJointDef def)
            : base(def)
        {
            m_localAnchorA = def.localAnchorA;
            m_localAnchorB = def.localAnchorB;
            m_localXAxisA = def.localAxisA;
            m_localXAxisA.Normalize();
            m_localYAxisA = m_localXAxisA.NegUnitCross(); //  b2Math.b2Cross(1.0f, m_localXAxisA);
            m_referenceAngle = def.referenceAngle;

            m_impulse.SetZero();
            m_motorMass = 0.0f;
            m_motorImpulse = 0.0f;

            m_lowerTranslation = def.lowerTranslation;
            m_upperTranslation = def.upperTranslation;
            m_maxMotorForce = def.maxMotorForce;
            m_motorSpeed = def.motorSpeed;
            m_enableLimit = def.enableLimit;
            m_enableMotor = def.enableMotor;
            m_limitState = b2LimitState.e_inactiveLimit;

            m_axis.SetZero();
            m_perp.SetZero();
        }
Example #5
0
        public CollisionFiltering()
        {
            // Ground body
            {
                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));

                b2FixtureDef sd = new b2FixtureDef();
                sd.shape = shape;
                sd.friction = 0.3f;

                b2BodyDef bd  = new b2BodyDef();
                b2Body ground = m_world.CreateBody(bd);
                ground.CreateFixture(sd);
            }

            // Small triangle
            b2Vec2[] vertices = new b2Vec2[3];
            vertices[0].Set(-1.0f, 0.0f);
            vertices[1].Set(1.0f, 0.0f);
            vertices[2].Set(0.0f, 2.0f);
            b2PolygonShape polygon = new b2PolygonShape();
            polygon.Set(vertices, 3);

            b2FixtureDef triangleShapeDef = new b2FixtureDef();
            triangleShapeDef.shape = polygon;
            triangleShapeDef.density = 1.0f;

            triangleShapeDef.filter.groupIndex = k_smallGroup;
            triangleShapeDef.filter.categoryBits = k_triangleCategory;
            triangleShapeDef.filter.maskBits = k_triangleMask;

            b2BodyDef triangleBodyDef  = new b2BodyDef();
            triangleBodyDef.type = b2BodyType.b2_dynamicBody;
            triangleBodyDef.position.Set(-5.0f, 2.0f);

            b2Body body1 = m_world.CreateBody(triangleBodyDef);
            body1.CreateFixture(triangleShapeDef);

            // Large triangle (recycle definitions)
            vertices[0] *= 2.0f;
            vertices[1] *= 2.0f;
            vertices[2] *= 2.0f;
            polygon.Set(vertices, 3);
            triangleShapeDef.filter.groupIndex = k_largeGroup;
            triangleBodyDef.position.Set(-5.0f, 6.0f);
            triangleBodyDef.fixedRotation = true; // look at me!

            b2Body body2 = m_world.CreateBody(triangleBodyDef);
            body2.CreateFixture(triangleShapeDef);

            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-5.0f, 10.0f);
                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape p = new b2PolygonShape();
                p.SetAsBox(0.5f, 1.0f);
                body.CreateFixture(p, 1.0f);

                b2PrismaticJointDef jd = new b2PrismaticJointDef();
                jd.BodyA = body2;
                jd.BodyB = body;
                jd.enableLimit = true;
                jd.localAnchorA.Set(0.0f, 4.0f);
                jd.localAnchorB.SetZero();
                jd.localAxisA.Set(0.0f, 1.0f);
                jd.lowerTranslation = -1.0f;
                jd.upperTranslation = 1.0f;

                m_world.CreateJoint(jd);
            }

            // Small box
            polygon.SetAsBox(1.0f, 0.5f);
            b2FixtureDef boxShapeDef = new b2FixtureDef();
            boxShapeDef.shape = polygon;
            boxShapeDef.density = 1.0f;
            boxShapeDef.restitution = 0.1f;

            boxShapeDef.filter.groupIndex = k_smallGroup;
            boxShapeDef.filter.categoryBits = k_boxCategory;
            boxShapeDef.filter.maskBits = k_boxMask;

            b2BodyDef boxBodyDef  = new b2BodyDef();
            boxBodyDef.type = b2BodyType.b2_dynamicBody;
            boxBodyDef.position.Set(0.0f, 2.0f);

            b2Body body3 = m_world.CreateBody(boxBodyDef);
            body3.CreateFixture(boxShapeDef);

            // Large box (recycle definitions)
            polygon.SetAsBox(2.0f, 1.0f);
            boxShapeDef.filter.groupIndex = k_largeGroup;
            boxBodyDef.position.Set(0.0f, 6.0f);

            b2Body body4 = m_world.CreateBody(boxBodyDef);
            body4.CreateFixture(boxShapeDef);

            // Small circle
            b2CircleShape circle = new b2CircleShape();
            circle.Radius = 1.0f;

            b2FixtureDef circleShapeDef = new b2FixtureDef();
            circleShapeDef.shape = circle;
            circleShapeDef.density = 1.0f;

            circleShapeDef.filter.groupIndex = k_smallGroup;
            circleShapeDef.filter.categoryBits = k_circleCategory;
            circleShapeDef.filter.maskBits = k_circleMask;

            b2BodyDef circleBodyDef  = new b2BodyDef();
            circleBodyDef.type = b2BodyType.b2_dynamicBody;
            circleBodyDef.position.Set(5.0f, 2.0f);

            b2Body body5 = m_world.CreateBody(circleBodyDef);
            body5.CreateFixture(circleShapeDef);

            // Large circle
            circle.Radius = circle.Radius * 2.0f;
            circleShapeDef.filter.groupIndex = k_largeGroup;
            circleBodyDef.position.Set(5.0f, 6.0f);

            b2Body body6 = m_world.CreateBody(circleBodyDef);
            body6.CreateFixture(circleShapeDef);
        }
Example #6
0
        public Gears()
        {
            b2Body ground = null;
            {
                b2BodyDef bd  = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(50.0f, 0.0f), new b2Vec2(-50.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            // Gears co
            {
                b2CircleShape circle1 = new b2CircleShape();
                circle1.Radius = 1.0f;

                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(0.5f, 5.0f);

                b2CircleShape circle2 = new b2CircleShape();
                circle2.Radius = 2.0f;

                b2BodyDef bd1  = new b2BodyDef();
                bd1.type = b2BodyType.b2_staticBody;
                bd1.position.Set(10.0f, 9.0f);
                b2Body body1 = m_world.CreateBody(bd1);
                body1.CreateFixture(circle1, 0.0f);

                b2BodyDef bd2  = new b2BodyDef();
                bd2.type = b2BodyType.b2_dynamicBody;
                bd2.position.Set(10.0f, 8.0f);
                b2Body body2 = m_world.CreateBody(bd2);
                body2.CreateFixture(box, 5.0f);

                b2BodyDef bd3  = new b2BodyDef();
                bd3.type = b2BodyType.b2_dynamicBody;
                bd3.position.Set(10.0f, 6.0f);
                b2Body body3 = m_world.CreateBody(bd3);
                body3.CreateFixture(circle2, 5.0f);

                b2RevoluteJointDef jd1 = new b2RevoluteJointDef();
                jd1.Initialize(body2, body1, bd1.position);
                b2Joint joint1 = m_world.CreateJoint(jd1);

                b2RevoluteJointDef jd2 = new b2RevoluteJointDef();
                jd2.Initialize(body2, body3, bd3.position);
                b2Joint joint2 = m_world.CreateJoint(jd2);

                b2GearJointDef jd4 = new b2GearJointDef();
                jd4.BodyA = body1;
                jd4.BodyB = body3;
                jd4.joint1 = joint1;
                jd4.joint2 = joint2;
                jd4.ratio = circle2.Radius / circle1.Radius;
                m_world.CreateJoint(jd4);
            }

            {
                b2CircleShape circle1 = new b2CircleShape();
                circle1.Radius = 1.0f;

                b2CircleShape circle2 = new b2CircleShape();
                circle2.Radius = 2.0f;

                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(0.5f, 5.0f);

                b2BodyDef bd1  = new b2BodyDef();
                bd1.type = b2BodyType.b2_dynamicBody;
                bd1.position.Set(-3.0f, 12.0f);
                b2Body body1 = m_world.CreateBody(bd1);
                body1.CreateFixture(circle1, 5.0f);

                b2RevoluteJointDef jd1 = new b2RevoluteJointDef();
                jd1.BodyA = ground;
                jd1.BodyB = body1;
                jd1.localAnchorA = ground.GetLocalPoint(bd1.position);
                jd1.localAnchorB = body1.GetLocalPoint(bd1.position);
                jd1.referenceAngle = body1.Angle - ground.Angle;
                m_joint1 = (b2RevoluteJoint) m_world.CreateJoint(jd1);

                b2BodyDef bd2  = new b2BodyDef();
                bd2.type = b2BodyType.b2_dynamicBody;
                bd2.position.Set(0.0f, 12.0f);
                b2Body body2 = m_world.CreateBody(bd2);
                body2.CreateFixture(circle2, 5.0f);

                b2RevoluteJointDef jd2 = new b2RevoluteJointDef();
                jd2.Initialize(ground, body2, bd2.position);
                m_joint2 = (b2RevoluteJoint) m_world.CreateJoint(jd2);

                b2BodyDef bd3  = new b2BodyDef();
                bd3.type = b2BodyType.b2_dynamicBody;
                bd3.position.Set(2.5f, 12.0f);
                b2Body body3 = m_world.CreateBody(bd3);
                body3.CreateFixture(box, 5.0f);

                b2PrismaticJointDef jd3 = new b2PrismaticJointDef();
                jd3.Initialize(ground, body3, bd3.position, new b2Vec2(0.0f, 1.0f));
                jd3.lowerTranslation = -5.0f;
                jd3.upperTranslation = 5.0f;
                jd3.enableLimit = true;

                m_joint3 = (b2PrismaticJoint) m_world.CreateJoint(jd3);

                b2GearJointDef jd4 = new b2GearJointDef();
                jd4.BodyA = body1;
                jd4.BodyB = body2;
                jd4.joint1 = m_joint1;
                jd4.joint2 = m_joint2;
                jd4.ratio = circle2.Radius / circle1.Radius;
                m_joint4 = (b2GearJoint) m_world.CreateJoint(jd4);

                b2GearJointDef jd5 = new b2GearJointDef();
                jd5.BodyA = body2;
                jd5.BodyB = body3;
                jd5.joint1 = m_joint2;
                jd5.joint2 = m_joint3;
                jd5.ratio = -1.0f / circle2.Radius;
                m_joint5 = (b2GearJoint) m_world.CreateJoint(jd5);
            }
        }
Example #7
0
        // SIN REVISAR
        b2Joint j2b2Joint(b2World world, JObject jointValue)
        {
            b2Joint joint = null;

            int bodyIndexA = (int)jointValue["bodyA"];
            int bodyIndexB = (int)jointValue["bodyB"];
            if (bodyIndexA >= m_bodies.Count || bodyIndexB >= m_bodies.Count)
                return null;

            // set features common to all joints
            //var bodyA = m_bodies[bodyIndexA];
            //var bodyB = m_bodies[bodyIndexB];
            //var collideConnected = jointValue["collideConnected"] == null ? false : (bool)jointValue["collideConnected"];

            // keep these in scope after the if/else below
            b2RevoluteJointDef revoluteDef;
            b2PrismaticJointDef prismaticDef;
            b2DistanceJointDef distanceDef;
            b2PulleyJointDef pulleyDef;
            b2MouseJointDef mouseDef;
            b2GearJointDef gearDef;
            //b2WheelJoint wheelDef;
            b2WeldJointDef weldDef;
            b2FrictionJointDef frictionDef;
            b2RopeJointDef ropeDef;
            //MotorJoint motorDef;

            b2JointDef jointDef = null;

            b2Vec2 mouseJointTarget = new b2Vec2(0, 0);

            string type = jointValue["type"].ToString() == null ? "" : jointValue["type"].ToString();

            if (type == "revolute")
            {

                jointDef = revoluteDef = new b2RevoluteJointDef(); // JointFactory.CreateRevoluteJoint(world, bodyA, bodyB, jsonToVec("anchorB", jointValue));
                revoluteDef.localAnchorA = jsonToVec("anchorA", jointValue);
                revoluteDef.localAnchorB = jsonToVec("anchorB", jointValue);
                revoluteDef.referenceAngle = jsonToFloat("refAngle", jointValue);
                revoluteDef.enableLimit = jointValue["enableLimit"] == null ? false : (bool)jointValue["enableLimit"];
                revoluteDef.lowerAngle = jsonToFloat("lowerLimit", jointValue);
                revoluteDef.upperAngle = jsonToFloat("upperLimit", jointValue);
                revoluteDef.enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"];
                revoluteDef.motorSpeed = jsonToFloat("motorSpeed", jointValue);
                revoluteDef.maxMotorTorque = jsonToFloat("maxMotorTorque", jointValue);
            }
            else if (type == "prismatic")
            {
                jointDef = prismaticDef = new b2PrismaticJointDef(); //JointFactory.CreatePrismaticJoint(world, bodyA, bodyB, localAnchorB, localAxis);

                prismaticDef.localAnchorA = jsonToVec("anchorA", jointValue);
                prismaticDef.localAnchorB = jsonToVec("anchorB", jointValue);

                if (jointValue["localAxisA"] != null)
                    prismaticDef.localAxisA = jsonToVec("localAxisA", jointValue);
                else
                    prismaticDef.localAxisA = jsonToVec("localAxis1", jointValue);

                prismaticDef.referenceAngle = jsonToFloat("refAngle", jointValue);

                prismaticDef.enableLimit = jointValue["enableLimit"] == null ? false : (bool)jointValue["enableLimit"];

                prismaticDef.lowerTranslation = jsonToFloat("lowerLimit", jointValue);
                prismaticDef.upperTranslation = jsonToFloat("upperLimit", jointValue);

                prismaticDef.enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"];

                prismaticDef.motorSpeed = jsonToFloat("motorSpeed", jointValue);
                prismaticDef.maxMotorForce = jsonToFloat("maxMotorForce", jointValue);

            }
            else if (type == "distance")
            {

                jointDef = distanceDef = new b2DistanceJointDef();
                distanceDef.localAnchorA = jsonToVec("anchorA", jointValue);
                distanceDef.localAnchorB = jsonToVec("anchorB", jointValue);
                distanceDef.length = jsonToFloat("length", jointValue);
                distanceDef.frequencyHz = jsonToFloat("frequency", jointValue);
                distanceDef.dampingRatio = jsonToFloat("dampingRatio", jointValue);

            }
            else if (type == "pulley")
            {

                jointDef = pulleyDef = new b2PulleyJointDef();
                pulleyDef.groundAnchorA = jsonToVec("groundAnchorA", jointValue);
                pulleyDef.groundAnchorB = jsonToVec("groundAnchorB", jointValue);
                pulleyDef.localAnchorA = jsonToVec("anchorA", jointValue);
                pulleyDef.localAnchorB = jsonToVec("anchorB", jointValue);
                pulleyDef.lengthA = jsonToFloat("lengthA", jointValue);
                pulleyDef.lengthB = jsonToFloat("lengthB", jointValue);
                pulleyDef.ratio = jsonToFloat("ratio", jointValue);

            }
            else if (type == "mouse")
            {
                jointDef = mouseDef = new b2MouseJointDef();
                mouseJointTarget = jsonToVec("target", jointValue);
                mouseDef.target = jsonToVec("anchorB", jointValue);// alter after creating joint
                mouseDef.maxForce = jsonToFloat("maxForce", jointValue);
                mouseDef.frequencyHz = jsonToFloat("frequency", jointValue);
                mouseDef.dampingRatio = jsonToFloat("dampingRatio", jointValue);
            }
            // Gear joints are apparently not implemented in JBox2D yet, but
            // when they are, commenting out the following section should work.

            else if (type == "gear")
            {

                jointDef = gearDef = new b2GearJointDef();  //JointFactory.CreateGearJoint(world, joint1, joint2, ratio);
                int jointIndex1 = (int)jointValue["joint1"];
                int jointIndex2 = (int)jointValue["joint2"];
                var joint1 = m_joints[jointIndex1];
                var joint2 = m_joints[jointIndex2];
                var ratio = jsonToFloat("ratio", jointValue);

                //joint = gearDef = JointFactory.CreateGearJoint(world, joint1, joint2, ratio);

            }

            // Wheel joints are apparently not implemented in JBox2D yet, but
            // when they are, commenting out the following section should work.

            else if (type == "wheel")
            {

                jointDef = revoluteDef = new b2RevoluteJointDef();
                revoluteDef.localAnchorA = jsonToVec("anchorA", jointValue);
                revoluteDef.localAnchorB = jsonToVec("anchorB", jointValue);

                revoluteDef.enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"];

                revoluteDef.motorSpeed = jsonToFloat("motorSpeed", jointValue);
                revoluteDef.maxMotorTorque = jsonToFloat("maxMotorTorque", jointValue);

                //jointDef = wheelDef = new b2WheelJointDef(); //JointFactory.CreateWheelJoint(world, bodyA, bodyB, localAnchorB, localAxisA);

                //var localAnchorA = jsonToVec("anchorA", jointValue);
                //var localAnchorB = (jsonToVec("anchorB", jointValue));
                //var localAxisA = (jsonToVec("localAxisA", jointValue));
                //var enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"];
                //var motorSpeed = jsonToFloat("motorSpeed", jointValue);
                //var maxMotorTorque = jsonToFloat("maxMotorTorque", jointValue);
                //var frequencyHz = jsonToFloat("springFrequency", jointValue);
                //var dampingRatio = jsonToFloat("springDampingRatio", jointValue);

                //wheelDef.LocalAnchorA = localAnchorA;
                //wheelDef.LocalAnchorB = localAnchorB;
                //wheelDef.MotorEnabled = enableMotor;
                //wheelDef.MotorSpeed = motorSpeed;
                //wheelDef.SpringFrequencyHz = frequencyHz;
                //wheelDef.MaxMotorTorque = maxMotorTorque;
                //wheelDef.SpringDampingRatio = dampingRatio;
            }
            else if (type == "weld")
            {
                jointDef = weldDef = new b2WeldJointDef();
                weldDef.localAnchorA = jsonToVec("anchorA", jointValue);
                weldDef.localAnchorB = jsonToVec("anchorB", jointValue);
                weldDef.referenceAngle = 0;

            }
            else if (type == "friction")
            {
                jointDef = frictionDef = new b2FrictionJointDef();
                frictionDef.localAnchorA = jsonToVec("anchorA", jointValue);
                frictionDef.localAnchorB = jsonToVec("anchorB", jointValue);
                frictionDef.maxForce = jsonToFloat("maxForce", jointValue);
                frictionDef.maxTorque = jsonToFloat("maxTorque", jointValue);
            }
            else if (type == "rope")
            {
                jointDef = ropeDef = new b2RopeJointDef();
                ropeDef.localAnchorA = jsonToVec("anchorA", jointValue);
                ropeDef.localAnchorB = jsonToVec("anchorB", jointValue);
                ropeDef.maxLength = jsonToFloat("maxLength", jointValue);
            }

            //else if (type == "motor")
            //{
            //    var maxForce = jsonToFloat("maxForce", jointValue);
            //    var maxMotorTorque = jsonToFloat("maxTorque", jointValue);
            //    var angularOffset = jsonToFloat("refAngle", jointValue);

            //    joint = motorDef = new MotorJoint(bodyA, bodyB);
            //    world.AddJoint(joint);
            //    motorDef.LinearOffset = jsonToVec("anchorA", jointValue);
            //    motorDef.MaxForce = maxForce;
            //    motorDef.MaxTorque = maxMotorTorque;
            //    motorDef.AngularOffset = angularOffset;
            //}

            if (null != jointDef)
            {
                // set features common to all joints
                jointDef.BodyA = m_bodies[bodyIndexA];
                jointDef.BodyB = m_bodies[bodyIndexB];

                jointDef.CollideConnected = jointValue["collideConnected"] == null ? false : (bool)jointValue["collideConnected"];

                joint = world.CreateJoint(jointDef);

                if (type.Equals("mouse"))
                    ((b2MouseJoint)joint).SetTarget(mouseJointTarget);

                String jointName = jointValue["name"] == null ? "" : (string)jointValue["name"];

                if (!jointName.Equals(""))
                {
                    SetJointName(joint, jointName);
                }
            }

            return joint;
        }
Example #8
0
        public SliderCrank()
        {
            b2Body ground;
            {
                b2BodyDef bd  = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                b2Body prevBody = ground;

                // Define crank.
                {
                    b2PolygonShape shape = new b2PolygonShape();
                    shape.SetAsBox(0.5f, 2.0f);

                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(0.0f, 7.0f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    b2RevoluteJointDef rjd = new b2RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new b2Vec2(0.0f, 5.0f));
                    rjd.motorSpeed = 1.0f * b2Settings.b2_pi;
                    rjd.maxMotorTorque = 10000.0f;
                    rjd.enableMotor = true;
                    m_joint1 = (b2RevoluteJoint) m_world.CreateJoint(rjd);

                    prevBody = body;
                }

                // Define follower.
                {
                    b2PolygonShape shape = new b2PolygonShape();
                    shape.SetAsBox(0.5f, 4.0f);

                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(0.0f, 13.0f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    b2RevoluteJointDef rjd = new b2RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new b2Vec2(0.0f, 9.0f));
                    rjd.enableMotor = false;
                    m_world.CreateJoint(rjd);

                    prevBody = body;
                }

                // Define piston
                {
                    b2PolygonShape shape = new b2PolygonShape();
                    shape.SetAsBox(1.5f, 1.5f);

                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.fixedRotation = true;
                    bd.position.Set(0.0f, 17.0f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    b2RevoluteJointDef rjd = new b2RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new b2Vec2(0.0f, 17.0f));
                    m_world.CreateJoint(rjd);

                    b2PrismaticJointDef pjd = new b2PrismaticJointDef();
                    pjd.Initialize(ground, body, new b2Vec2(0.0f, 17.0f), new b2Vec2(0.0f, 1.0f));

                    pjd.maxMotorForce = 1000.0f;
                    pjd.enableMotor = true;

                    m_joint2 = (b2PrismaticJoint) m_world.CreateJoint(pjd);
                }

                // Create a payload
                {
                    b2PolygonShape shape = new b2PolygonShape();
                    shape.SetAsBox(1.5f, 1.5f);

                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(0.0f, 23.0f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);
                }
            }
        }
Example #9
0
        public DumpShell()
        {

            b2Vec2 g = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
            m_world.Gravity = g;
            b2Body[] bodies = new b2Body[3];
            b2Joint[] joints = new b2Joint[2];
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(1.304347801208496e+01f, 2.500000000000000e+00f);
                bd.angle = 0.000000000000000e+00f;
                bd.linearVelocity.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                bd.angularVelocity = 0.000000000000000e+00f;
                bd.linearDamping = 5.000000000000000e-01f;
                bd.angularDamping = 5.000000000000000e-01f;
                bd.allowSleep = true;
                bd.awake = true;
                bd.fixedRotation = false;
                bd.bullet = false;
                bd.active = true;
                bd.gravityScale = 1.000000000000000e+00f;
                bodies[0] = m_world.CreateBody(bd);

                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction = 1.000000000000000e+00f;
                    fd.restitution = 5.000000000000000e-01f;
                    fd.density = 1.000000000000000e+01f;
                    fd.isSensor = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits = 65535;
                    fd.filter.groupIndex = 0;
                    b2PolygonShape shape = new b2PolygonShape();
                    b2Vec2[] vs = new b2Vec2[8];
                    vs[0].Set(-6.900000095367432e+00f, -3.000000119209290e-01f);
                    vs[1].Set(2.000000029802322e-01f, -3.000000119209290e-01f);
                    vs[2].Set(2.000000029802322e-01f, 2.000000029802322e-01f);
                    vs[3].Set(-6.900000095367432e+00f, 2.000000029802322e-01f);
                    shape.Set(vs, 4);

                    fd.shape = shape;

                    bodies[0].CreateFixture(fd);
                }
            }
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(8.478260636329651e-01f, 2.500000000000000e+00f);
                bd.angle = 0.000000000000000e+00f;
                bd.linearVelocity.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                bd.angularVelocity = 0.000000000000000e+00f;
                bd.linearDamping = 5.000000000000000e-01f;
                bd.angularDamping = 5.000000000000000e-01f;
                bd.allowSleep = true;
                bd.awake = true;
                bd.fixedRotation = false;
                bd.bullet = false;
                bd.active = true;
                bd.gravityScale = 1.000000000000000e+00f;
                bodies[1] = m_world.CreateBody(bd);

                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction = 1.000000000000000e+00f;
                    fd.restitution = 5.000000000000000e-01f;
                    fd.density = 1.000000000000000e+01f;
                    fd.isSensor = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits = 65535;
                    fd.filter.groupIndex = 0;
                    b2PolygonShape shape = new b2PolygonShape();
                    b2Vec2[] vs = new b2Vec2[8];
                    vs[0].Set(-3.228000104427338e-01f, -2.957000136375427e-01f);
                    vs[1].Set(6.885900020599365e+00f, -3.641000092029572e-01f);
                    vs[2].Set(6.907599925994873e+00f, 3.271999955177307e-01f);
                    vs[3].Set(-3.228000104427338e-01f, 2.825999855995178e-01f);
                    shape.Set(vs, 4);

                    fd.shape = shape;

                    bodies[1].CreateFixture(fd);
                }
            }

            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_staticBody;
                bd.position.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                bd.angle = 0.000000000000000e+00f;
                bd.linearVelocity.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                bd.angularVelocity = 0.000000000000000e+00f;
                bd.linearDamping = 0.000000000000000e+00f;
                bd.angularDamping = 0.000000000000000e+00f;
                bd.allowSleep = true;
                bd.awake = true;
                bd.fixedRotation = false;
                bd.bullet = false;
                bd.active = true;
                bd.gravityScale = 1.000000000000000e+00f;
                bodies[2] = m_world.CreateBody(bd);

                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction = 1.000000000000000e+01f;
                    fd.restitution = 0.000000000000000e+00f;
                    fd.density = 0.000000000000000e+00f;
                    fd.isSensor = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits = 65535;
                    fd.filter.groupIndex = 0;
                    b2EdgeShape shape = new b2EdgeShape();
                    shape.Radius = 9.999999776482582e-03f;
                    shape.Vertex0 = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex1 = new b2Vec2(4.452173995971680e+01f, 1.669565200805664e+01f);
                    shape.Vertex2 = new b2Vec2(4.452173995971680e+01f, 0.000000000000000e+00f);
                    shape.Vertex3 = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.HasVertex0 = false;
                    shape.HasVertex3 = false;

                    fd.shape = shape;

                    bodies[2].CreateFixture(fd);
                }
                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction = 1.000000000000000e+01f;
                    fd.restitution = 0.000000000000000e+00f;
                    fd.density = 0.000000000000000e+00f;
                    fd.isSensor = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits = 65535;
                    fd.filter.groupIndex = 0;
                    b2EdgeShape shape = new b2EdgeShape();
                    shape.Radius = 9.999999776482582e-03f;
                    shape.Vertex0 = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex1 = new b2Vec2(0.000000000000000e+00f, 1.669565200805664e+01f);
                    shape.Vertex2 = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex3 = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.HasVertex0 = false;
                    shape.HasVertex3 = false;

                    fd.shape = shape;

                    bodies[2].CreateFixture(fd);
                }
                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction = 1.000000000000000e+01f;
                    fd.restitution = 0.000000000000000e+00f;
                    fd.density = 0.000000000000000e+00f;
                    fd.isSensor = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits = 65535;
                    fd.filter.groupIndex = 0;
                    b2EdgeShape shape = new b2EdgeShape();
                    shape.Radius = 9.999999776482582e-03f;
                    shape.Vertex0 = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex1 = new b2Vec2(0.000000000000000e+00f, 1.669565200805664e+01f);
                    shape.Vertex2 = new b2Vec2(4.452173995971680e+01f, 1.669565200805664e+01f);
                    shape.Vertex3 = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.HasVertex0 = false;
                    shape.HasVertex3 = false;

                    fd.shape = shape;

                    bodies[2].CreateFixture(fd);
                }
                {
                    b2FixtureDef fd = new b2FixtureDef();
                    fd.friction = 1.000000000000000e+01f;
                    fd.restitution = 0.000000000000000e+00f;
                    fd.density = 0.000000000000000e+00f;
                    fd.isSensor = false;
                    fd.filter.categoryBits = 1;
                    fd.filter.maskBits = 65535;
                    fd.filter.groupIndex = 0;
                    b2EdgeShape shape = new b2EdgeShape();
                    shape.Radius = 9.999999776482582e-03f;
                    shape.Vertex0 = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex1 = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.Vertex2 = new b2Vec2(4.452173995971680e+01f, 0.000000000000000e+00f);
                    shape.Vertex3 = new b2Vec2(0.000000000000000e+00f, 0.000000000000000e+00f);
                    shape.HasVertex0 = false;
                    shape.HasVertex3 = false;

                    fd.shape = shape;

                    bodies[2].CreateFixture(fd);
                }
            }

            {
                b2PrismaticJointDef jd = new b2PrismaticJointDef();
                jd.BodyA = bodies[1];
                jd.BodyB = bodies[0];
                jd.CollideConnected = false;
                jd.localAnchorA.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                jd.localAnchorB.Set(-1.219565200805664e+01f, 0.000000000000000e+00f);
                jd.localAxisA.Set(-1.219565200805664e+01f, 0.000000000000000e+00f);
                jd.referenceAngle = 0.000000000000000e+00f;
                jd.enableLimit = true;
                jd.lowerTranslation = -2.000000000000000e+01f;
                jd.upperTranslation = 0.000000000000000e+00f;
                jd.enableMotor = true;
                jd.motorSpeed = 0.000000000000000e+00f;
                jd.maxMotorForce = 1.000000000000000e+01f;
                joints[0] = m_world.CreateJoint(jd);
            }
            {
                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.BodyA = bodies[1];
                jd.BodyB = bodies[2];
                jd.CollideConnected = false;
                jd.localAnchorA.Set(0.000000000000000e+00f, 0.000000000000000e+00f);
                jd.localAnchorB.Set(8.478260636329651e-01f, 2.500000000000000e+00f);
                jd.referenceAngle = 0.000000000000000e+00f;
                jd.enableLimit = false;
                jd.lowerAngle = 0.000000000000000e+00f;
                jd.upperAngle = 0.000000000000000e+00f;
                jd.enableMotor = false;
                jd.motorSpeed = 0.000000000000000e+00f;
                jd.maxMotorTorque = 0.000000000000000e+00f;
                joints[1] = m_world.CreateJoint(jd);
            }

        }
Example #10
0
        public BodyTypes()
        {
            b2Body ground = null;
            {
                b2BodyDef bd  = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-20.0f, 0.0f), new b2Vec2(20.0f, 0.0f));

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;

                ground.CreateFixture(fd);
            }

            // Define attachment
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;

                bd.position = new b2Vec2(0.0f, 3.0f);
                m_attachment = m_world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 2.0f);
                m_attachment.CreateFixture(shape, 2.0f);
            }

            // Define platform
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                
                bd.position = new b2Vec2(-4.0f, 5.0f);
                m_platform = m_world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 4.0f, new b2Vec2(4.0f, 0.0f), 0.5f * b2Settings.b2_pi);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                fd.friction = 0.6f;
                fd.density = 2.0f;
                m_platform.CreateFixture(fd);

                b2RevoluteJointDef rjd = new b2RevoluteJointDef();
                rjd.Initialize(m_attachment, m_platform, new b2Vec2(0.0f, 5.0f));
                rjd.maxMotorTorque = 50.0f;
                rjd.enableMotor = true;
                m_world.CreateJoint(rjd);

                b2PrismaticJointDef pjd = new b2PrismaticJointDef();
                pjd.Initialize(ground, m_platform, new b2Vec2(0.0f, 5.0f), new b2Vec2(1.0f, 0.0f));

                pjd.maxMotorForce = 1000.0f;
                pjd.enableMotor = true;
                pjd.lowerTranslation = -10.0f;
                pjd.upperTranslation = 10.0f;
                pjd.enableLimit = true;

                m_world.CreateJoint(pjd);

                m_speed = 3.0f;
            }

            // Create a payload
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position = new b2Vec2(0.0f, 8.0f);
                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.75f, 0.75f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                fd.friction = 0.6f;
                fd.density = 2.0f;

                body.CreateFixture(fd);
            }
        }