示例#1
0
        public Breakable()
        {
            // Ground body
            {
                BodyDef bd = new BodyDef();
                Body 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);
            }

            // Breakable dynamic body
            /*{
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0.0f, 40.0f);
                bd.Angle = (float)(0.25 * Math.PI);
                var m_body1 = m_world.CreateBody(bd);

                m_shape1.SetAsBox(0.5f, 0.5f, new Vec2(-0.5f, 0.0f), 0.0f);
                var m_piece1 = m_body1.CreateFixture(m_shape1, 1.0f);

                m_shape2.SetAsBox(0.5f, 0.5f, new Vec2(0.5f, 0.0f), 0.0f);
                var m_piece2 = m_body1.CreateFixture(m_shape2, 1.0f);

                m_breaks.Add(new BodyBreaker(m_world, m_body1, m_piece1, m_piece2));
            }*/
            float y = 2.0f;

            for (int i = 0; i < 3; ++i)
            {
                PolygonShape bottom = new PolygonShape();
                bottom.SetAsBox(1.5f, 0.15f);

                PolygonShape left = new PolygonShape();
                left.SetAsBox(0.15f, 2.7f, new Vec2(-1.45f, 2.35f), 0.2f);

                PolygonShape right = new PolygonShape();
                right.SetAsBox(0.15f, 2.7f, new Vec2(1.45f, 2.35f), -0.2f);

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0.0f, y += 3.5f);
                Body bottom_body = m_world.CreateBody(bd);
                var bf = bottom_body.CreateFixture(bottom, 4.0f);
                Body left_body = m_world.CreateBody(bd);
                var lf = left_body.CreateFixture(left, 4.0f);
                Body right_body = m_world.CreateBody(bd);
                var rf = right_body.CreateFixture(right, 4.0f);

                WeldJointDef wjd = new WeldJointDef();
                wjd.Initialize(bottom_body, left_body, bd.Position);
                wjd.CollideConnected = false;

                WeldJoint wj1 = (WeldJoint)m_world.CreateJoint(wjd);

                wjd.Initialize(bottom_body, right_body, bd.Position);
                WeldJoint wj2 = (WeldJoint)m_world.CreateJoint(wjd);

                m_breaks.Add(new BreakableJoint(wj1, 100));
                m_breaks.Add(new BreakableJoint(wj2, 100));
            }
        }
示例#2
0
文件: Cloth.cs 项目: RubisetCie/box2c
        public Cloth()
        {
            FixtureDef boxFix = new FixtureDef(new CircleShape(ClothBodySize), 0.2f);
            BodyDef boxBod = new BodyDef(BodyType.Dynamic, Vec2.Empty);

            boxFix.Filter.GroupIndex = -1;
            boxBod.Position = new Vec2(-ClothTotalWidth / 2, 30);

            Body bar;
            {
                bar = m_world.CreateBody(new BodyDef(BodyType.Static, new Vec2(-ClothBodySpacingWidth / 2, 30)));

                var fd = new FixtureDef(new PolygonShape((ClothTotalWidth / 2) + ClothBodySpacingWidth, 0.25f));
                fd.Filter.GroupIndex = -1;
                bar.CreateFixture(fd);
            }

            for (int y = 0; y < ClothSegmentsHeight; ++y)
            {
                for (int x = 0; x < ClothSegmentsWidth; ++x)
                {
                    Body body = m_world.CreateBody(boxBod);
                    boxBod.Position += new Vec2(ClothBodySpacingWidth, 0);

                    body.CreateFixture(boxFix);

                    if (y == 0)
                    {
                        WeldJointDef wjd = new WeldJointDef();
                        wjd.Initialize(body, bar, body.WorldCenter);
                        m_world.CreateJoint(wjd);
                    }

                    cloth[x, y] = body;
                }

                boxBod.Position = new Vec2(-ClothTotalWidth / 2, boxBod.Position.Y - ClothBodySpacingWidth);
            }

            for (int y = 0; y < ClothSegmentsHeight; ++y)
            {
                for (int x = 0; x < ClothSegmentsWidth; ++x)
                {
                    Body leftBody, rightBody;

                    DistanceJointDef djd = new DistanceJointDef();
                    djd.FrequencyHz = 15 + Rand.RandomFloat(0, 6);
                    djd.DampingRatio = 0.11f + Rand.RandomFloat(0.01f, 0.15f);

                    // connect to right
                    if (x != ClothSegmentsWidth - 1)
                    {
                        leftBody = cloth[x, y];
                        rightBody = cloth[x + 1, y];

                        djd.Initialize(leftBody, rightBody, leftBody.WorldCenter, rightBody.WorldCenter);
                        m_world.CreateJoint(djd);
                    }

                    // connect to up
                    if (y != 0)
                    {
                        leftBody = cloth[x, y];
                        rightBody = cloth[x, y - 1];

                        djd.Initialize(leftBody, rightBody, leftBody.WorldCenter, rightBody.WorldCenter);
                        m_world.CreateJoint(djd);
                    }
                }
            }
        }
示例#3
0
        public Cantilever()
        {
            Body ground;
            {
                ground = m_world.CreateBody(new BodyDef());

                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(0.5f, 0.125f);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 20.0f;

                WeldJointDef jd = new WeldJointDef();

                Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(-14.5f + 1.0f * i, 5.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

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

                    prevBody = body;
                }
            }

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

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 20.0f;

                WeldJointDef jd = new WeldJointDef();

                Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(-14.5f + 1.0f * i, 15.0f);
                    bd.InertiaScale = 10.0f;
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

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

                    prevBody = body;
                }
            }

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

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 20.0f;

                WeldJointDef jd = new WeldJointDef();

                Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(-4.5f + 1.0f * i, 5.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    if (i > 0)
                    {
                        Vec2 anchor = new Vec2(-5.0f + 1.0f * i, 5.0f);
                        jd.Initialize(prevBody, body, anchor);
                        m_world.CreateJoint(jd);
                    }

                    prevBody = body;
                }
            }

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

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 20.0f;

                WeldJointDef jd = new WeldJointDef();

                Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(5.5f + 1.0f * i, 10.0f);
                    bd.InertiaScale = 10.0f;
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    if (i > 0)
                    {
                        Vec2 anchor = new Vec2(5.0f + 1.0f * i, 10.0f);
                        jd.Initialize(prevBody, body, anchor);
                        m_world.CreateJoint(jd);
                    }

                    prevBody = body;
                }
            }

            for (int i = 0; i < 2; ++i)
            {
                Vec2[] vertices = new Vec2[]
                {
                    new Vec2(-0.5f, 0.0f),
                    new Vec2(0.5f, 0.0f),
                    new Vec2(0.0f, 1.5f)
                };

                PolygonShape shape = new PolygonShape();
                shape.Vertices = vertices;

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 1.0f;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-8.0f + 8.0f * i, 12.0f);
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }

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

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 1.0f;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-6.0f + 6.0f * i, 10.0f);
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }
        }