コード例 #1
0
    internal b2PrismaticJoint(b2PrismaticJointDef def) : base(def)
    {
        m_localAnchorA = def.localAnchorA;


        m_localAnchorB = def.localAnchorB;


        m_localXAxisA = def.localAxisA;
        m_localXAxisA.Normalize();
        m_localYAxisA    = Utils.b2Cross(1.0f, m_localXAxisA);
        m_referenceAngle = def.referenceAngle;

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

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

        m_axis.SetZero();
        m_perp.SetZero();
    }
コード例 #2
0
        public Prismatic()
        {
            b2Body ground = null;
            {
                b2BodyDef bd = new b2BodyDef();
                ground = m_world.CreateBody(bd);

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

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

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

                b2PrismaticJointDef pjd = new b2PrismaticJointDef();

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

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

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

                m_joint = (b2PrismaticJoint)m_world.CreateJoint(pjd);
            }
        }
コード例 #3
0
ファイル: Gears.cs プロジェクト: krashman/cocos2d-xna
        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);
            }
        }
コード例 #4
0
        public CollisionFiltering()
        {
            // Ground body
            {
                b2EdgeShape shape = new b2EdgeShape();
                shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f));

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

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

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

            polygon.Set(vertices, 3);

            b2FixtureDef triangleShapeDef = new b2FixtureDef();

            triangleShapeDef.shape   = polygon;
            triangleShapeDef.density = 1.0f;

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

            b2BodyDef triangleBodyDef = new b2BodyDef();

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

            b2Body body1 = m_world.CreateBody(triangleBodyDef);

            body1.CreateFixture(triangleShapeDef);

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

            b2Body body2 = m_world.CreateBody(triangleBodyDef);

            body2.CreateFixture(triangleShapeDef);

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

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

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

                m_world.CreateJoint(jd);
            }

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

            boxShapeDef.shape       = polygon;
            boxShapeDef.density     = 1.0f;
            boxShapeDef.restitution = 0.1f;

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

            b2BodyDef boxBodyDef = new b2BodyDef();

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

            b2Body body3 = m_world.CreateBody(boxBodyDef);

            body3.CreateFixture(boxShapeDef);

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

            b2Body body4 = m_world.CreateBody(boxBodyDef);

            body4.CreateFixture(boxShapeDef);

            // Small circle
            b2CircleShape circle = new b2CircleShape();

            circle.Radius = 1.0f;

            b2FixtureDef circleShapeDef = new b2FixtureDef();

            circleShapeDef.shape   = circle;
            circleShapeDef.density = 1.0f;

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

            b2BodyDef circleBodyDef = new b2BodyDef();

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

            b2Body body5 = m_world.CreateBody(circleBodyDef);

            body5.CreateFixture(circleShapeDef);

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

            b2Body body6 = m_world.CreateBody(circleBodyDef);

            body6.CreateFixture(circleShapeDef);
        }
コード例 #5
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);
            }
        }
コード例 #6
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(b2PrismaticJointDef obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
コード例 #7
0
ファイル: BodyTypes.cs プロジェクト: womandroid/cocos2d-xna
        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);
            }
        }
コード例 #8
0
        public SliderCrank()
        {
            b2Body ground;
            {
                b2BodyDef bd = new b2BodyDef();
                ground = m_world.CreateBody(bd);

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

            {
                b2Body prevBody = ground;

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

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

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

                    prevBody = body;
                }

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

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

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

                    prevBody = body;
                }

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

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

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

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

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

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

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

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