Exemplo n.º 1
0
        public b2FrictionJoint(b2FrictionJointDef def)
            : base(def)
        {
            m_localAnchorA = def.localAnchorA;
            m_localAnchorB = def.localAnchorB;

            m_linearImpulse.SetZero();
            m_angularImpulse = 0.0f;

            m_maxForce = def.maxForce;
            m_maxTorque = def.maxTorque;
        }
Exemplo n.º 2
0
        public b2FrictionJoint(b2FrictionJointDef def)
            : base(def)
        {
            m_localAnchorA = def.localAnchorA;
            m_localAnchorB = def.localAnchorB;

            m_linearImpulse.SetZero();
            m_angularImpulse = 0.0f;

            m_maxForce  = def.maxForce;
            m_maxTorque = def.maxTorque;
        }
Exemplo n.º 3
0
        public ApplyForce()
        {
            m_world.Gravity = new b2Vec2(0.0f, 0.0f);

            float k_restitution = 0.4f;

            b2Body ground;
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.position = new b2Vec2(0.0f, 20.0f);
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();

                b2FixtureDef sd = new b2FixtureDef();
                sd.shape = shape;
                sd.density = 0.0f;
                sd.restitution = k_restitution;

                // Left vertical
                shape.Set(new b2Vec2(-20.0f, -20.0f), new b2Vec2(-20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Right vertical
                shape.Set(new b2Vec2(20.0f, -20.0f), new b2Vec2(20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Top horizontal
                shape.Set(new b2Vec2(-20.0f, 20.0f), new b2Vec2(20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Bottom horizontal
                shape.Set(new b2Vec2(-20.0f, -20.0f), new b2Vec2(20.0f, -20.0f));
                ground.CreateFixture(sd);
            }

            {
                b2Transform xf1;
                xf1.q = new b2Rot(0.3524f * b2Settings.b2_pi);
                xf1.p = xf1.q.GetXAxis();

                b2Vec2[] vertices = new b2Vec2[3];
                vertices[0] = b2Math.b2Mul(xf1, new b2Vec2(-1.0f, 0.0f));
                vertices[1] = b2Math.b2Mul(xf1, new b2Vec2(1.0f, 0.0f));
                vertices[2] = b2Math.b2Mul(xf1, new b2Vec2(0.0f, 0.5f));

                b2PolygonShape poly1 = new b2PolygonShape();
                poly1.Set(vertices, 3);

                b2FixtureDef sd1 = new b2FixtureDef();
                sd1.shape = poly1;
                sd1.density = 4.0f;

                b2Transform xf2 = new b2Transform();
                xf2.q.Set(-0.3524f * b2Settings.b2_pi);
                xf2.p = -xf2.q.GetXAxis();

                vertices[0] = b2Math.b2Mul(xf2, new b2Vec2(-1.0f, 0.0f));
                vertices[1] = b2Math.b2Mul(xf2, new b2Vec2(1.0f, 0.0f));
                vertices[2] = b2Math.b2Mul(xf2, new b2Vec2(0.0f, 0.5f));

                b2PolygonShape poly2 = new b2PolygonShape();
                poly2.Set(vertices, 3);

                b2FixtureDef sd2 = new b2FixtureDef();
                sd2.shape = poly2;
                sd2.density = 2.0f;

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.angularDamping = 5.0f;
                bd.linearDamping = 0.1f;

                bd.position.Set(0.0f, 2.0f);
                bd.angle = b2Settings.b2_pi;
                bd.allowSleep = false;
                m_body = m_world.CreateBody(bd);
                m_body.CreateFixture(sd1);
                m_body.CreateFixture(sd2);
            }

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

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                fd.density = 1.0f;
                fd.friction = 0.3f;

                for (int i = 0; i < 10; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;

                    bd.position = new b2Vec2(0.0f, 5.0f + 1.54f * i);
                    b2Body body = m_world.CreateBody(bd);

                    body.CreateFixture(fd);

                    float gravity = 10.0f;
                    float I = body.Inertia;
                    float mass = body.Mass;

                    // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m)
                    float radius = b2Math.b2Sqrt(2.0f * I / mass);

                    b2FrictionJointDef jd = new b2FrictionJointDef();
                    jd.localAnchorA = b2Vec2.Zero;
                    jd.localAnchorB = b2Vec2.Zero;
                    jd.BodyA = ground;
                    jd.BodyB = body;
                    jd.CollideConnected = true;
                    jd.maxForce = mass * gravity;
                    jd.maxTorque = mass * radius * gravity;

                    m_world.CreateJoint(jd);
                }
            }
        }
Exemplo n.º 4
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;
        }