public b2RevoluteJoint(b2RevoluteJointDef def)
            : base(def)
        {
            m_localAnchorA   = def.localAnchorA;
            m_localAnchorB   = def.localAnchorB;
            m_referenceAngle = def.referenceAngle;

            m_impulse.SetZero();
            m_motorImpulse = 0.0f;

            m_lowerAngle     = def.lowerAngle;
            m_upperAngle     = def.upperAngle;
            m_maxMotorTorque = def.maxMotorTorque;
            m_motorSpeed     = def.motorSpeed;
            m_enableLimit    = def.enableLimit;
            m_enableMotor    = def.enableMotor;
            m_limitState     = b2LimitState.e_inactiveLimit;
        }
Example #2
0
        public b2RevoluteJoint(b2RevoluteJointDef def)
            : base(def)
        {
            m_localAnchorA = def.localAnchorA;
            m_localAnchorB = def.localAnchorB;
            m_referenceAngle = def.referenceAngle;

            m_impulse.SetZero();
            m_motorImpulse = 0.0f;

            m_lowerAngle = def.lowerAngle;
            m_upperAngle = def.upperAngle;
            m_maxMotorTorque = def.maxMotorTorque;
            m_motorSpeed = def.motorSpeed;
            m_enableLimit = def.enableLimit;
            m_enableMotor = def.enableMotor;
            m_limitState = b2LimitState.e_inactiveLimit;
        }
Example #3
0
        public Chain()
        {
            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(0.6f, 0.125f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                fd.friction = 0.2f;

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.CollideConnected = false;

                const float y = 25.0f;
                b2Body prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(0.5f + i, y);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    b2Vec2 anchor = new b2Vec2(i, y);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
Example #4
0
        public Tumbler()
        {
            b2Body ground;
            {
                b2BodyDef bd  = new b2BodyDef();
                ground = m_world.CreateBody(bd);
            }

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

                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.5f, 10.0f, new b2Vec2(10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(0.5f, 10.0f, new b2Vec2(-10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(10.0f, 0.5f, new b2Vec2(0.0f, 10.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(10.0f, 0.5f, new b2Vec2(0.0f, -10.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.BodyA = ground;
                jd.BodyB = body;
                jd.localAnchorA.Set(0.0f, 10.0f);
                jd.localAnchorB.Set(0.0f, 0.0f);
                jd.referenceAngle = 0.0f;
                jd.motorSpeed = 0.05f * b2Settings.b2_pi;
                jd.maxMotorTorque = 1e8f;
                jd.enableMotor = true;
                m_joint = (b2RevoluteJoint) m_world.CreateJoint(jd);
            }

            m_count = 0;
        }
Example #5
0
        public Car()
        {
            m_hz = 4.0f;
            m_zeta = 0.7f;
            m_speed = 50.0f;

            b2Body ground = null;
            {
                b2BodyDef bd  = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2EdgeShape shape = new b2EdgeShape();

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

                shape.Set(new b2Vec2(-20.0f, 0.0f), new b2Vec2(20.0f, 0.0f));
                ground.CreateFixture(fd);

                float[] hs = {0.25f, 1.0f, 4.0f, 0.0f, 0.0f, -1.0f, -2.0f, -2.0f, -1.25f, 0.0f};

                float x = 20.0f, y1 = 0.0f, dx = 5.0f;

                for (int i = 0; i < 10; ++i)
                {
                    float y2 = hs[i];
                    shape.Set(new b2Vec2(x, y1), new b2Vec2(x + dx, y2));
                    ground.CreateFixture(fd);
                    y1 = y2;
                    x += dx;
                }

                for (int i = 0; i < 10; ++i)
                {
                    float y2 = hs[i];
                    shape.Set(new b2Vec2(x, y1), new b2Vec2(x + dx, y2));
                    ground.CreateFixture(fd);
                    y1 = y2;
                    x += dx;
                }

                shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 80.0f;
                shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 40.0f;
                shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 10.0f, 5.0f));
                ground.CreateFixture(fd);

                x += 20.0f;
                shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 40.0f;
                shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x, 20.0f));
                ground.CreateFixture(fd);
            }

            // Teeter
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.position.Set(140.0f, 1.0f);
                bd.type = b2BodyType.b2_dynamicBody;
                b2Body body = m_world.CreateBody(bd);

                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(10.0f, 0.25f);
                body.CreateFixture(box, 1.0f);

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.Initialize(ground, body, body.Position);
                jd.lowerAngle = -8.0f * b2Settings.b2_pi / 180.0f;
                jd.upperAngle = 8.0f * b2Settings.b2_pi / 180.0f;
                jd.enableLimit = true;
                m_world.CreateJoint(jd);

                body.ApplyAngularImpulse(100.0f);
            }

            // Bridge
            {
                int N = 20;
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(1.0f, 0.125f);

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

                b2RevoluteJointDef jd = new b2RevoluteJointDef();

                b2Body prevBody = ground;
                for (int i = 0; i < N; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(161.0f + 2.0f * i, -0.125f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    b2Vec2 anchor = new b2Vec2(160.0f + 2.0f * i, -0.125f);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    prevBody = body;
                }

                b2Vec2 anchor1 = new b2Vec2(160.0f + 2.0f * N, -0.125f);
                jd.Initialize(prevBody, ground, anchor1);
                m_world.CreateJoint(jd);
            }

            // Boxes
            {
                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(0.5f, 0.5f);

                b2Body body = null;
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;

                bd.position.Set(230.0f, 0.5f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.position.Set(230.0f, 1.5f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.position.Set(230.0f, 2.5f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.position.Set(230.0f, 3.5f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.position.Set(230.0f, 4.5f);
                body = m_world.CreateBody(bd);
                body.CreateFixture(box, 0.5f);
            }

            // Car
            {
                b2PolygonShape chassis = new b2PolygonShape();
                b2Vec2[] vertices = new b2Vec2[8];
                vertices[0].Set(-1.5f, -0.5f);
                vertices[1].Set(1.5f, -0.5f);
                vertices[2].Set(1.5f, 0.0f);
                vertices[3].Set(0.0f, 0.9f);
                vertices[4].Set(-1.15f, 0.9f);
                vertices[5].Set(-1.5f, 0.2f);
                chassis.Set(vertices, 6);

                b2CircleShape circle = new b2CircleShape();
                circle.Radius = 0.4f;

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(0.0f, 1.0f);
                m_car = m_world.CreateBody(bd);
                m_car.CreateFixture(chassis, 1.0f);

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

                bd.position.Set(-1.0f, 0.35f);
                m_wheel1 = m_world.CreateBody(bd);
                m_wheel1.CreateFixture(fd);

                bd.position.Set(1.0f, 0.4f);
                m_wheel2 = m_world.CreateBody(bd);
                m_wheel2.CreateFixture(fd);

                b2WheelJointDef jd = new b2WheelJointDef();
                b2Vec2 axis = new b2Vec2(0.0f, 1.0f);

                jd.Initialize(m_car, m_wheel1, m_wheel1.Position, axis);
                jd.motorSpeed = 0.0f;
                jd.maxMotorTorque = 20.0f;
                jd.enableMotor = true;
                jd.frequencyHz = m_hz;
                jd.dampingRatio = m_zeta;
                m_spring1 = (b2WheelJoint) m_world.CreateJoint(jd);

                jd.Initialize(m_car, m_wheel2, m_wheel2.Position, axis);
                jd.motorSpeed = 0.0f;
                jd.maxMotorTorque = 10.0f;
                jd.enableMotor = false;
                jd.frequencyHz = m_hz;
                jd.dampingRatio = m_zeta;
                m_spring2 = (b2WheelJoint) m_world.CreateJoint(jd);
            }
        }
            public Wheel(
                b2World b2world,

                double x,
                double y,
                double width,
                double length,
                bool revolving,
                bool powered
                )
            {
                this.revolving = revolving;
                this.powered = powered;

                this.Initialize = car =>
                {

                    /*
                    wheel object 

                    pars:

                    car - car this wheel belongs to
                    x - horizontal position in meters relative to car's center
                    y - vertical position in meters relative to car's center
                    width - width in meters
                    length - length in meters
                    revolving - does this wheel revolve when steering?
                    powered - is this wheel powered?
                    */

                    var position = new double[] { x, y };
                    //this.car=pars.car;
                    //this.revolving=pars.revolving;
                    //this.powered=pars.powered;

                    //initialize body
                    var def = new b2BodyDef();
                    def.type = b2Body.b2_dynamicBody;
                    def.position = car.body.GetWorldPoint(new b2Vec2(position[0], position[1]));
                    def.angle = car.body.GetAngle();
                    this.body = b2world.CreateBody(def);

                    //initialize shape
                    var fixdef = new b2FixtureDef();
                    fixdef.density = 1;
                    fixdef.isSensor = true; //wheel does not participate in collision calculations: resulting complications are unnecessary

                    var fixdef_shape = new b2PolygonShape();

                    fixdef.shape = fixdef_shape;
                    fixdef_shape.SetAsBox(width / 2, length / 2);
                    body.CreateFixture(fixdef);

                    var jointdef = new b2RevoluteJointDef();

                    //create joint to connect wheel to body
                    if (revolving)
                    {
                        jointdef.Initialize(car.body, body, body.GetWorldCenter());
                        jointdef.enableMotor = false; //we'll be controlling the wheel's angle manually
                    }
                    else
                    {
                        jointdef.Initialize(car.body, body, body.GetWorldCenter()
                            //, new b2Vec2(1, 0)
                            );
                        jointdef.enableLimit = true;


                        //jointdef.lowerTranslation = 0;
                        //jointdef.upperTranslation = 0;
                    }
                    b2world.CreateJoint(jointdef);

                    #region setAngle
                    this.setAngle =
                        (angle) =>
                        {
                            /*
                            angle - wheel angle relative to car, in degrees
                            */
                            body.SetAngle(car.body.GetAngle() + angle.DegreesToRadians());
                        };
                    #endregion


                    #region getLocalVelocity
                    Func<double[]> getLocalVelocity = delegate
                    {
                        /*returns get velocity vector relative to car
                        */
                        var res = car.body.GetLocalVector(car.body.GetLinearVelocityFromLocalPoint(new b2Vec2(position[0], position[1])));
                        return new double[] { res.x, res.y };
                    };
                    #endregion



                    #region getDirectionVector
                    Func<double[]> getDirectionVector = delegate
                    {
                        /*
                        returns a world unit vector pointing in the direction this wheel is moving
                        */

                        if (getLocalVelocity()[1] > 0)
                            return vectors.rotate(new double[] { 0, 1 }, body.GetAngle());
                        else
                            return vectors.rotate(new double[] { 0, -1 }, body.GetAngle());
                    };
                    #endregion


                    #region getKillVelocityVector
                    Func<double[]> getKillVelocityVector = delegate
                    {
                        /*
                        substracts sideways velocity from this wheel's velocity vector and returns the remaining front-facing velocity vector
                        */
                        var velocity = body.GetLinearVelocity();
                        var sideways_axis = getDirectionVector();
                        var dotprod = vectors.dot(new[] { velocity.x, velocity.y }, sideways_axis);
                        return new double[] { sideways_axis[0] * dotprod, sideways_axis[1] * dotprod };
                    };
                    #endregion

                    #region killSidewaysVelocity
                    this.killSidewaysVelocity = delegate
                    {
                        /*
                        removes all sideways velocity from this wheels velocity
                        */
                        var kv = getKillVelocityVector();

                        body.SetLinearVelocity(new b2Vec2(kv[0], kv[1]));

                    };
                    #endregion
                };

            }
Example #7
0
        public void CreateLeg(float s, b2Vec2 wheelAnchor)
        {
            b2Vec2 p1 = new b2Vec2(5.4f * s, -6.1f);
            b2Vec2 p2 = new b2Vec2(7.2f * s, -1.2f);
            b2Vec2 p3 = new b2Vec2(4.3f * s, -1.9f);
            b2Vec2 p4 = new b2Vec2(3.1f * s, 0.8f);
            b2Vec2 p5 = new b2Vec2(6.0f * s, 1.5f);
            b2Vec2 p6 = new b2Vec2(2.5f * s, 3.7f);

            b2FixtureDef fd1 = new b2FixtureDef();
            b2FixtureDef fd2 = new b2FixtureDef();
            fd1.filter.groupIndex = -1;
            fd2.filter.groupIndex = -1;
            fd1.density = 1.0f;
            fd2.density = 1.0f;

            b2PolygonShape poly1 = new b2PolygonShape();
            b2PolygonShape poly2 = new b2PolygonShape();

            if (s > 0.0f)
            {
                b2Vec2[] vertices = new b2Vec2[3];

                vertices[0] = p1;
                vertices[1] = p2;
                vertices[2] = p3;
                poly1.Set(vertices, 3);

                vertices[0] = b2Vec2.Zero;
                vertices[1] = p5 - p4;
                vertices[2] = p6 - p4;
                poly2.Set(vertices, 3);
            }
            else
            {
                b2Vec2[] vertices = new b2Vec2[3];

                vertices[0] = p1;
                vertices[1] = p3;
                vertices[2] = p2;
                poly1.Set(vertices, 3);

                vertices[0] = b2Vec2.Zero;
                vertices[1] = p6 - p4;
                vertices[2] = p5 - p4;
                poly2.Set(vertices, 3);
            }

            fd1.shape = poly1;
            fd2.shape = poly2;

            b2BodyDef bd1  = new b2BodyDef();
            b2BodyDef bd2  = new b2BodyDef();
            bd1.type = b2BodyType.b2_dynamicBody;
            bd2.type = b2BodyType.b2_dynamicBody;
            bd1.position = m_offset;
            bd2.position = p4 + m_offset;

            bd1.angularDamping = 10.0f;
            bd2.angularDamping = 10.0f;

            b2Body body1 = m_world.CreateBody(bd1);
            b2Body body2 = m_world.CreateBody(bd2);

            body1.CreateFixture(fd1);
            body2.CreateFixture(fd2);

            b2DistanceJointDef djd = new b2DistanceJointDef();

            // Using a soft distance constraint can reduce some jitter.
            // It also makes the structure seem a bit more fluid by
            // acting like a suspension system.
            djd.dampingRatio = 0.5f;
            djd.frequencyHz = 10.0f;

            djd.Initialize(body1, body2, p2 + m_offset, p5 + m_offset);
            m_world.CreateJoint(djd);

            djd.Initialize(body1, body2, p3 + m_offset, p4 + m_offset);
            m_world.CreateJoint(djd);

            djd.Initialize(body1, m_wheel, p3 + m_offset, wheelAnchor + m_offset);
            m_world.CreateJoint(djd);

            djd.Initialize(body2, m_wheel, p6 + m_offset, wheelAnchor + m_offset);
            m_world.CreateJoint(djd);

            b2RevoluteJointDef rjd = new b2RevoluteJointDef();

            rjd.Initialize(body2, m_chassis, p4 + m_offset);
            m_world.CreateJoint(rjd);
        }
Example #8
0
        public Revolute()
        {
            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));

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                //fd.filter.categoryBits = 2;

                ground.CreateFixture(fd);
            }

            {
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 0.5f;

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;

                b2RevoluteJointDef rjd = new b2RevoluteJointDef();

                bd.position.Set(-10.0f, 20.0f);
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                float w = 100.0f;
                body.AngularVelocity = w;
                body.LinearVelocity = new b2Vec2(-8.0f * w, 0.0f);

                rjd.Initialize(ground, body, new b2Vec2(-10.0f, 12.0f));
                rjd.motorSpeed = 1.0f * b2Settings.b2_pi;
                rjd.maxMotorTorque = 10000.0f;
                rjd.enableMotor = false;
                rjd.lowerAngle = -0.25f * b2Settings.b2_pi;
                rjd.upperAngle = 0.5f * b2Settings.b2_pi;
                rjd.enableLimit = true;
                rjd.CollideConnected = true;

                m_joint = (b2RevoluteJoint) m_world.CreateJoint(rjd);
            }

            {
                b2CircleShape circle_shape = new b2CircleShape();
                circle_shape.Radius = 3.0f;

                b2BodyDef circle_bd  = new b2BodyDef();
                circle_bd.type = b2BodyType.b2_dynamicBody;
                circle_bd.position.Set(5.0f, 30.0f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.density = 5.0f;
                fd.filter.maskBits = 1;
                fd.shape = circle_shape;

                m_ball = m_world.CreateBody(circle_bd);
                m_ball.CreateFixture(fd);

                b2PolygonShape polygon_shape = new b2PolygonShape();
                polygon_shape.SetAsBox(10.0f, 0.2f, new b2Vec2(-10.0f, 0.0f), 0.0f);

                b2BodyDef polygon_bd  = new b2BodyDef();
                polygon_bd.position.Set(20.0f, 10.0f);
                polygon_bd.type = b2BodyType.b2_dynamicBody;
                polygon_bd.bullet = true;
                b2Body polygon_body = m_world.CreateBody(polygon_bd);
                polygon_body.CreateFixture(polygon_shape, 2.0f);

                b2RevoluteJointDef rjd = new b2RevoluteJointDef();
                rjd.Initialize(ground, polygon_body, new b2Vec2(20.0f, 10.0f));
                rjd.lowerAngle = -0.25f * b2Settings.b2_pi;
                rjd.upperAngle = 0.0f * b2Settings.b2_pi;
                rjd.enableLimit = true;
                m_world.CreateJoint(rjd);
            }

            // Tests mass computation of a small object far from the origin
            {
                b2BodyDef bodyDef  = new b2BodyDef();
                bodyDef.type = b2BodyType.b2_dynamicBody;
                b2Body body = m_world.CreateBody(bodyDef);

                b2PolygonShape polyShape = new b2PolygonShape();
                b2Vec2[] verts = new b2Vec2[3];
                verts[0].Set(17.63f, 36.31f);
                verts[1].Set(17.52f, 36.69f);
                verts[2].Set(17.19f, 36.36f);
                polyShape.Set(verts, 3);

                b2FixtureDef polyFixtureDef = new b2FixtureDef();
                polyFixtureDef.shape = polyShape;
                polyFixtureDef.density = 1;

                body.CreateFixture(polyFixtureDef); //assertion hits inside here
            }

        }
Example #9
0
        public RopeJoint()
        {
            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);
            }

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

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                fd.friction = 0.2f;
                fd.filter.categoryBits = 0x0001;
                fd.filter.maskBits = 0xFFFF & ~0x0002;

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.CollideConnected = false;

                const int N = 10;
                const float y = 15.0f;
                m_ropeDef.localAnchorA.Set(0.0f, y);

                b2Body prevBody = ground;
                for (int i = 0; i < N; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(0.5f + 1.0f * i, y);
                    if (i == N - 1)
                    {
                        shape.SetAsBox(1.5f, 1.5f);
                        fd.density = 100.0f;
                        fd.filter.categoryBits = 0x0002;
                        bd.position.Set(1.0f * i, y);
                        bd.angularDamping = 0.4f;
                    }

                    b2Body body = m_world.CreateBody(bd);

                    body.CreateFixture(fd);

                    b2Vec2 anchor = new b2Vec2(i, y);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    prevBody = body;
                }

                m_ropeDef.localAnchorB.SetZero();

                float extraLength = 0.01f;
                m_ropeDef.maxLength = N - 1.0f + extraLength;
                m_ropeDef.BodyB = prevBody;
            }

            {
                m_ropeDef.BodyA = ground;
                m_rope = m_world.CreateJoint(m_ropeDef);
            }
        }
Example #10
0
        public TheoJansen()
        {
            m_offset.Set(0.0f, 8.0f);
            m_motorSpeed = 2.0f;
            m_motorOn = true;
            b2Vec2 pivot = new b2Vec2(0.0f, 0.8f);

            // Ground
            {
                b2BodyDef bd  = new b2BodyDef();
                b2Body 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);

                shape.Set(new b2Vec2(-50.0f, 0.0f), new b2Vec2(-50.0f, 10.0f));
                ground.CreateFixture(shape, 0.0f);

                shape.Set(new b2Vec2(50.0f, 0.0f), new b2Vec2(50.0f, 10.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            // Balls
            for (int i = 0; i < 40; ++i)
            {
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 0.25f;

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-40.0f + 2.0f * i, 0.5f);

                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 1.0f);
            }

            // Chassis
            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(2.5f, 1.0f);

                b2FixtureDef sd = new b2FixtureDef();
                sd.density = 1.0f;
                sd.shape = shape;
                sd.filter.groupIndex = -1;
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position = pivot + m_offset;
                m_chassis = m_world.CreateBody(bd);
                m_chassis.CreateFixture(sd);
            }

            {
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 1.6f;

                b2FixtureDef sd = new b2FixtureDef();
                sd.density = 1.0f;
                sd.shape = shape;
                sd.filter.groupIndex = -1;
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position = pivot + m_offset;
                m_wheel = m_world.CreateBody(bd);
                m_wheel.CreateFixture(sd);
            }

            {
                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.Initialize(m_wheel, m_chassis, pivot + m_offset);
                jd.CollideConnected = false;
                jd.motorSpeed = m_motorSpeed;
                jd.maxMotorTorque = 400.0f;
                jd.enableMotor = m_motorOn;
                m_motorJoint = (b2RevoluteJoint) m_world.CreateJoint(jd);
            }

            b2Vec2 wheelAnchor = new b2Vec2();

            wheelAnchor = pivot + new b2Vec2(0.0f, -0.8f);

            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            m_wheel.SetTransform(m_wheel.Position, 120.0f * b2Settings.b2_pi / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            m_wheel.SetTransform(m_wheel.Position, -120.0f * b2Settings.b2_pi / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);
        }
Example #11
0
        public Pinball()
        {
            // Ground body
            b2Body ground;
            {
                b2BodyDef bd  = new b2BodyDef();
                ground = m_world.CreateBody(bd);

                b2Vec2[] vs = new b2Vec2[5];
                vs[0].Set(0.0f, -2.0f);
                vs[1].Set(8.0f, 6.0f);
                vs[2].Set(8.0f, 20.0f);
                vs[3].Set(-8.0f, 20.0f);
                vs[4].Set(-8.0f, 6.0f);

                b2ChainShape loop = new b2ChainShape();
                loop.CreateLoop(vs, 5);
                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = loop;
                fd.density = 0.0f;
                ground.CreateFixture(fd);
            }

            // Flippers
            {
                b2Vec2 p1 = new b2Vec2(-2.0f, 0.0f);
                b2Vec2 p2 = new b2Vec2(2.0f, 0.0f);

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;

                bd.position = p1;
                b2Body leftFlipper = m_world.CreateBody(bd);

                bd.position = p2;
                b2Body rightFlipper = m_world.CreateBody(bd);

                b2PolygonShape box = new b2PolygonShape();
                box.SetAsBox(1.75f, 0.1f);

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

                leftFlipper.CreateFixture(fd);
                rightFlipper.CreateFixture(fd);

                b2RevoluteJointDef jd = new b2RevoluteJointDef();
                jd.BodyA = ground;
                jd.localAnchorB.SetZero();
                jd.enableMotor = true;
                jd.maxMotorTorque = 1000.0f;
                jd.enableLimit = true;

                jd.motorSpeed = 0.0f;
                jd.localAnchorA = p1;
                jd.BodyB = leftFlipper;
                jd.lowerAngle = -30.0f * b2Settings.b2_pi / 180.0f;
                jd.upperAngle = 5.0f * b2Settings.b2_pi / 180.0f;
                m_leftJoint = (b2RevoluteJoint) m_world.CreateJoint(jd);

                jd.motorSpeed = 0.0f;
                jd.localAnchorA = p2;
                jd.BodyB = rightFlipper;
                jd.lowerAngle = -5.0f * b2Settings.b2_pi / 180.0f;
                jd.upperAngle = 30.0f * b2Settings.b2_pi / 180.0f;
                m_rightJoint = (b2RevoluteJoint) m_world.CreateJoint(jd);
            }

            // Circle character
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.position.Set(1.0f, 15.0f);
                bd.type = b2BodyType.b2_dynamicBody;
                bd.bullet = true;

                m_ball = m_world.CreateBody(bd);

                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 0.2f;

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                fd.density = 1.0f;
                m_ball.CreateFixture(fd);
            }

            m_button = false;
        }
Example #12
0
        public Dominos()
        {
            b2Body b1;
            {
                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));

                b2BodyDef bd  = new b2BodyDef();
                b1 = m_world.CreateBody(bd);
                b1.CreateFixture(shape, 0.0f);
            }

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

                b2BodyDef bd  = new b2BodyDef();
                bd.position.Set(-1.5f, 10.0f);
                b2Body ground = m_world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

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

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                fd.friction = 0.1f;

                for (int i = 0; i < 10; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(-6.0f + 1.0f * i, 11.25f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);
                }
            }

            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(7.0f, 0.25f, b2Vec2.Zero, 0.3f);

                b2BodyDef bd  = new b2BodyDef();
                bd.position.Set(1.0f, 6.0f);
                b2Body ground = m_world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

            b2Body b2;
            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.25f, 1.5f);

                b2BodyDef bd  = new b2BodyDef();
                bd.position.Set(-7.0f, 4.0f);
                b2 = m_world.CreateBody(bd);
                b2.CreateFixture(shape, 0.0f);
            }

            b2Body b3;
            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(6.0f, 0.125f);

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-0.9f, 1.0f);
                bd.angle = -0.15f;

                b3 = m_world.CreateBody(bd);
                b3.CreateFixture(shape, 10.0f);
            }

            b2RevoluteJointDef jd = new b2RevoluteJointDef();
            b2Vec2 anchor = new b2Vec2();

            anchor.Set(-2.0f, 1.0f);
            jd.Initialize(b1, b3, anchor);
            jd.CollideConnected = true;
            m_world.CreateJoint(jd);

            b2Body b4;
            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.25f, 0.25f);

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-10.0f, 15.0f);
                b4 = m_world.CreateBody(bd);
                b4.CreateFixture(shape, 10.0f);
            }

            anchor.Set(-7.0f, 15.0f);
            jd.Initialize(b2, b4, anchor);
            m_world.CreateJoint(jd);

            b2Body b5;
            {
                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(6.5f, 3.0f);
                b5 = m_world.CreateBody(bd);

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

                fd.shape = shape;
                fd.density = 10.0f;
                fd.friction = 0.1f;

                shape.SetAsBox(1.0f, 0.1f, new b2Vec2(0.0f, -0.9f), 0.0f);
                b5.CreateFixture(fd);

                shape.SetAsBox(0.1f, 1.0f, new b2Vec2(-0.9f, 0.0f), 0.0f);
                b5.CreateFixture(fd);

                shape.SetAsBox(0.1f, 1.0f, new b2Vec2(0.9f, 0.0f), 0.0f);
                b5.CreateFixture(fd);
            }

            anchor.Set(6.0f, 2.0f);
            jd.Initialize(b1, b5, anchor);
            m_world.CreateJoint(jd);

            b2Body b6;
            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(1.0f, 0.1f);

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(6.5f, 4.1f);
                b6 = m_world.CreateBody(bd);
                b6.CreateFixture(shape, 30.0f);
            }

            anchor.Set(7.5f, 4.0f);
            jd.Initialize(b5, b6, anchor);
            m_world.CreateJoint(jd);

            b2Body b7;
            {
                b2PolygonShape shape = new b2PolygonShape();
                shape.SetAsBox(0.1f, 1.0f);

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(7.4f, 1.0f);

                b7 = m_world.CreateBody(bd);
                b7.CreateFixture(shape, 10.0f);
            }

            b2DistanceJointDef djd = new b2DistanceJointDef();
            djd.BodyA = b3;
            djd.BodyB = b7;
            djd.localAnchorA.Set(6.0f, 0.0f);
            djd.localAnchorB.Set(0.0f, -1.0f);
            b2Vec2 d = djd.BodyB.GetWorldPoint(djd.localAnchorB) - djd.BodyA.GetWorldPoint(djd.localAnchorA);
            djd.length = d.Length;
            m_world.CreateJoint(djd);

            {
                float radius = 0.2f;

                b2CircleShape shape = new b2CircleShape();
                shape.Radius = radius;

                for (int i = 0; i < 4; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position.Set(5.9f + 2.0f * radius * i, 2.4f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 10.0f);
                }
            }
        }
Example #13
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 #14
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 #15
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 #16
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 #17
0
        public CircleStress()
        {
            b2Body leftWall = null;
            b2Body rightWall = null;
            {
                // Ground
                b2PolygonShape sd = new b2PolygonShape();
                sd.SetAsBox(50.0f, 10.0f);
                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_staticBody;
                bd.position = new b2Vec2(0.0f, -10.0f);
                b2Body b = m_world.CreateBody(bd);
                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = sd;
                fd.friction = 1.0f;
                b.CreateFixture(fd);

                // Walls
                sd.SetAsBox(3.0f, 50.0f);
                bd = new b2BodyDef();
                bd.position = new b2Vec2(45.0f, 25.0f);
                rightWall = m_world.CreateBody(bd);
                rightWall.CreateFixture(sd, 0);
                bd.position = new b2Vec2(-45.0f, 25.0f);
                leftWall = m_world.CreateBody(bd);
                leftWall.CreateFixture(sd, 0);

                // Corners
                bd = new b2BodyDef();
                sd.SetAsBox(20.0f, 3.0f);
                bd.angle = (float)(-Math.PI / 4.0);
                bd.position = new b2Vec2(-35f, 8.0f);
                b2Body myBod = m_world.CreateBody(bd);
                myBod.CreateFixture(sd, 0);
                bd.angle = (float)(Math.PI / 4.0);
                bd.position = new b2Vec2(35f, 8.0f);
                myBod = m_world.CreateBody(bd);
                myBod.CreateFixture(sd, 0);

                // top
                sd.SetAsBox(50.0f, 10.0f);
                bd.type = b2BodyType.b2_staticBody;
                bd.angle = 0;
                bd.position = new b2Vec2(0.0f, 75.0f);
                b = m_world.CreateBody(bd);
                fd.shape = sd;
                fd.friction = 1.0f;
                b.CreateFixture(fd);
            }

            {
                b2CircleShape cd;
                b2FixtureDef fd = new b2FixtureDef();

                b2BodyDef bd = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                int numPieces = 5;
                float radius = 6f;
                bd.position = new b2Vec2(0.0f, 10.0f);
                b2Body body = m_world.CreateBody(bd);
                for (int i = 0; i < numPieces; i++)
                {
                    cd = new b2CircleShape();
                    cd.Radius = 1.2f;
                    fd.shape = cd;
                    fd.density = 25;
                    fd.friction = .1f;
                    fd.restitution = .9f;
                    float xPos = radius * (float) Math.Cos(2f * Math.PI * (i / (float) (numPieces)));
                    float yPos = radius * (float) Math.Sin(2f * Math.PI * (i / (float) (numPieces)));
                    cd.Position.Set(xPos, yPos);

                    body.CreateFixture(fd);
                }

                body.SetBullet(false);

                b2RevoluteJointDef rjd = new b2RevoluteJointDef();
                rjd.Initialize(body, m_groundBody, body.Position);
                rjd.motorSpeed = (float) Math.PI;
                rjd.maxMotorTorque = 1000000.0f;
                rjd.enableMotor = true;
                joint = (b2RevoluteJoint) m_world.CreateJoint(rjd);
            }

            {
                int loadSize = 41;

                for (int j = 0; j < 15; j++)
                {
                    for (int i = 0; i < loadSize; i++)
                    {
                        b2CircleShape circ = new b2CircleShape();
                        b2BodyDef bod = new b2BodyDef();
                        bod.type = b2BodyType.b2_dynamicBody;
                        circ.Radius = 1.0f + (i % 2 == 0 ? 1.0f : -1.0f) * .5f * Rand.RandomFloat(.5f, 1f);
                        b2FixtureDef fd2 = new b2FixtureDef();
                        fd2.shape = circ;
                        fd2.density = circ.Radius * 1.5f;
                        fd2.friction = 0.5f;
                        fd2.restitution = 0.7f;
                        float xPos = -39f + 2 * i;
                        float yPos = 50f + j;
                        bod.position = new b2Vec2(xPos, yPos);
                        b2Body myBody = m_world.CreateBody(bod);
                        myBody.CreateFixture(fd2);

                    }
                }

            }

            m_world.Gravity = new b2Vec2(0, -50);
        }
Example #18
0
        public Bridge()
        {
            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(0.5f, 0.125f);

                b2FixtureDef fd = new b2FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                fd.friction = 0.2f;

                b2RevoluteJointDef jd = new b2RevoluteJointDef();

                b2Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    b2BodyDef bd  = new b2BodyDef();
                    bd.type = b2BodyType.b2_dynamicBody;
                    bd.position = new b2Vec2(-14.5f + 1.0f * i, 5.0f);
                    b2Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    b2Vec2 anchor = new b2Vec2(-15.0f + 1.0f * i, 5.0f);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    if (i == (e_count >> 1))
                    {
                        m_middle = body;
                    }
                    prevBody = body;
                }

                b2Vec2 anchor1 = new b2Vec2(-15.0f + 1.0f * e_count, 5.0f);
                jd.Initialize(prevBody, ground, anchor1);
                m_world.CreateJoint(jd);
            }

            for (int i = 0; i < 2; ++i)
            {
                b2Vec2[] vertices = new b2Vec2[3];
                vertices[0].Set(-0.5f, 0.0f);
                vertices[1].Set(0.5f, 0.0f);
                vertices[2].Set(0.0f, 1.5f);

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

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

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position = new b2Vec2(-8.0f + 8.0f * i, 12.0f);
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }

            for (int i = 0; i < 3; ++i)
            {
                b2CircleShape shape = new b2CircleShape();
                shape.Radius = 0.5f;

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

                b2BodyDef bd  = new b2BodyDef();
                bd.type = b2BodyType.b2_dynamicBody;
                bd.position.Set(-6.0f + 6.0f * i, 10.0f);
                b2Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }
        }
Example #19
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);
            }
        }