Пример #1
0
        public Prismatic()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

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

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-10.0f, 10.0f);
                bd.Angle = 0.5f * (float)Math.PI;
                bd.AllowSleep = false;
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                PrismaticJointDef pjd = new PrismaticJointDef();

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

                // Non-bouncy limit
                //pjd.Initialize(ground, body, new Vec2(-10.0f, 10.0f), new Vec2(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 = false;

                m_joint = (PrismaticJoint)m_world.CreateJoint(pjd);
            }
        }
Пример #2
0
        public BodyTypes()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;

                ground.CreateFixture(fd);
            }

            // Define attachment
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0.0f, 3.0f);
                m_attachment = m_world.CreateBody(bd);

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

            // Define platform
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-4.0f, 5.0f);
                m_platform = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.5f, 4.0f, new Vec2(4.0f, 0.0f), 0.5f * (float)Math.PI);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Friction = 0.6f;
                fd.Density = 2.0f;
                m_platform.CreateFixture(fd);

                RevoluteJointDef rjd = new RevoluteJointDef();
                rjd.Initialize(m_attachment, m_platform, new Vec2(0.0f, 5.0f));
                rjd.MaxMotorTorque = 50.0f;
                rjd.EnableMotor = true;
                m_world.CreateJoint(rjd);

                PrismaticJointDef pjd = new PrismaticJointDef();
                pjd.Initialize(ground, m_platform, new Vec2(0.0f, 5.0f), new Vec2(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
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0.0f, 8.0f);
                m_payload = m_world.CreateBody(bd);

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

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Friction = 0.6f;
                fd.Density = 2.0f;

                m_payload.CreateFixture(fd);
            }
        }
Пример #3
0
        public Gears()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(50.0f, 0.0f), new Vec2(-50.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

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

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

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

                BodyDef bd1 = new BodyDef();
                bd1.BodyType = BodyType.Dynamic;
                bd1.Position = new Vec2(-3.0f, 12.0f);
                Body body1 = m_world.CreateBody(bd1);
                body1.CreateFixture(circle1, 5.0f);

                RevoluteJointDef jd1 = new RevoluteJointDef();
                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 = (RevoluteJoint)m_world.CreateJoint(jd1);

                BodyDef bd2 = new BodyDef();
                bd2.BodyType = BodyType.Dynamic;
                bd2.Position = new Vec2(0.0f, 12.0f);
                Body body2 = m_world.CreateBody(bd2);
                body2.CreateFixture(circle2, 5.0f);

                RevoluteJointDef jd2 = new RevoluteJointDef();
                jd2.Initialize(ground, body2, bd2.Position);
                m_joint2 = (RevoluteJoint)m_world.CreateJoint(jd2);

                BodyDef bd3 = new BodyDef();
                bd3.BodyType = BodyType.Dynamic;
                bd3.Position = new Vec2(2.5f, 12.0f);
                Body body3 = m_world.CreateBody(bd3);
                body3.CreateFixture(box, 5.0f);

                PrismaticJointDef jd3 = new PrismaticJointDef();
                jd3.Initialize(ground, body3, bd3.Position, new Vec2(0.0f, 1.0f));
                jd3.LowerTranslation = -5.0f;
                jd3.UpperTranslation = 5.0f;
                jd3.EnableLimit = true;

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

                GearJointDef jd4 = new GearJointDef();
                jd4.BodyA = body1;
                jd4.BodyB = body2;
                jd4.JointA = m_joint1;
                jd4.JointB = m_joint2;
                jd4.Ratio = circle2.Radius / circle1.Radius;
                m_joint4 = (GearJoint)m_world.CreateJoint(jd4);

                GearJointDef jd5 = new GearJointDef();
                jd5.BodyA = body2;
                jd5.BodyB = body3;
                jd5.JointA = m_joint2;
                jd5.JointB = m_joint3;
                jd5.Ratio = -1.0f / circle2.Radius;
                m_joint5 = (GearJoint)m_world.CreateJoint(jd5);
            }
        }
Пример #4
0
        public CollisionFiltering()
        {
            // Ground body
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));

                FixtureDef sd = new FixtureDef();
                sd.Shape = shape;
                sd.Friction = 0.3f;

                BodyDef bd = new BodyDef();
                Body ground = m_world.CreateBody(bd);
                ground.CreateFixture(sd);
            }

            // Small triangle
            Vec2[] vertices = new Vec2[3];
            vertices[0] = new Vec2(-1.0f, 0.0f);
            vertices[1] = new Vec2(1.0f, 0.0f);
            vertices[2] = new Vec2(0.0f, 2.0f);

            PolygonShape polygon = new PolygonShape(vertices);

            FixtureDef triangleShapeDef = new FixtureDef();
            triangleShapeDef.Shape = polygon;
            triangleShapeDef.Density = 1.0f;

            triangleShapeDef.Filter = new FilterData(k_triangleCategory, k_triangleMask, k_smallGroup);

            BodyDef triangleBodyDef = new BodyDef();
            triangleBodyDef.BodyType = BodyType.Dynamic;
            triangleBodyDef.Position = new Vec2(-5.0f, 2.0f);

            Body 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.Vertices = vertices;
            triangleShapeDef.Filter = new FilterData(triangleShapeDef.Filter.CategoryBits, triangleShapeDef.Filter.MaskBits, k_largeGroup);
            triangleBodyDef.Position = new Vec2(-5.0f, 6.0f);
            triangleBodyDef.FixedRotation = true; // look at me!

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

            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-5.0f, 10.0f);
                Body body = m_world.CreateBody(bd);

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

                PrismaticJointDef jd = new PrismaticJointDef();
                jd.BodyA = body2;
                jd.BodyB = body;
                jd.EnableLimit = true;
                jd.LocalAnchorA = new Vec2(0.0f, 4.0f);
                jd.LocalAnchorB = Vec2.Empty;
                jd.LocalAxis = new Vec2(0.0f, 1.0f);
                jd.LowerTranslation = -1.0f;
                jd.UpperTranslation = 1.0f;

                m_world.CreateJoint(jd);
            }

            // Small box
            polygon.SetAsBox(1.0f, 0.5f);
            FixtureDef boxShapeDef = new FixtureDef();
            boxShapeDef.Shape = polygon;
            boxShapeDef.Density = 1.0f;
            boxShapeDef.Restitution = 0.1f;

            boxShapeDef.Filter = new FilterData(k_boxCategory, k_boxMask, k_smallGroup);

            BodyDef boxBodyDef = new BodyDef();
            boxBodyDef.BodyType = BodyType.Dynamic;
            boxBodyDef.Position = new Vec2(0.0f, 2.0f);

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

            // Large box (recycle definitions)
            polygon.SetAsBox(2.0f, 1.0f);
            boxShapeDef.Filter = new FilterData(k_boxCategory, k_boxMask, k_largeGroup);
            boxBodyDef.Position = new Vec2(0.0f, 6.0f);

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

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

            FixtureDef circleShapeDef = new FixtureDef();
            circleShapeDef.Shape = circle;
            circleShapeDef.Density = 1.0f;

            circleShapeDef.Filter = new FilterData(k_circleCategory, k_circleMask, k_smallGroup);

            BodyDef circleBodyDef = new BodyDef();
            circleBodyDef.BodyType = BodyType.Dynamic;
            circleBodyDef.Position = new Vec2(5.0f, 2.0f);

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

            // Large circle
            circle.Radius *= 2.0f;
            circleShapeDef.Filter = new FilterData(k_circleCategory, k_circleMask, k_largeGroup);
            circleBodyDef.Position = new Vec2(5.0f, 6.0f);

            Body body6 = m_world.CreateBody(circleBodyDef);
            body6.CreateFixture(circleShapeDef);
        }
Пример #5
0
        public SliderCrank()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                Body prevBody = ground;

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

                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(0.0f, 7.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vec2(0.0f, 5.0f));
                    rjd.MotorSpeed = 1.0f * (float)Math.PI;
                    rjd.MaxMotorTorque = 10000.0f;
                    rjd.EnableMotor = true;
                    m_joint1 = (RevoluteJoint)m_world.CreateJoint(rjd);

                    prevBody = body;
                }

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

                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(0.0f, 13.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vec2(0.0f, 9.0f));
                    rjd.EnableMotor = false;
                    m_world.CreateJoint(rjd);

                    prevBody = body;
                }

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

                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(0.0f, 17.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

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

                    PrismaticJointDef pjd = new PrismaticJointDef();
                    pjd.Initialize(ground, body, new Vec2(0.0f, 17.0f), new Vec2(0.0f, 1.0f));

                    pjd.MaxMotorForce = 1000.0f;
                    pjd.EnableMotor = true;

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

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

                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(0.0f, 23.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);
                }
            }
        }