Ejemplo n.º 1
0
        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);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        void CreateLeg(float s, Vec2 wheelAnchor)
        {
            Vec2 p1 = new Vec2(5.4f * s, -6.1f);
            Vec2 p2 = new Vec2(7.2f * s, -1.2f);
            Vec2 p3 = new Vec2(4.3f * s, -1.9f);
            Vec2 p4 = new Vec2(3.1f * s, 0.8f);
            Vec2 p5 = new Vec2(6.0f * s, 1.5f);
            Vec2 p6 = new Vec2(2.5f * s, 3.7f);

            FixtureDef fd1 = new FixtureDef(), fd2 = new FixtureDef();
            fd1.Filter.GroupIndex = fd2.Filter.GroupIndex = -1;
            fd1.Density = 1.0f;
            fd2.Density = 1.0f;

            PolygonShape poly1 = new PolygonShape(), poly2 = new PolygonShape();

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

                vertices[0] = p1;
                vertices[1] = p2;
                vertices[2] = p3;
                poly1.Vertices = vertices;

                vertices[0] = Vec2.Empty;
                vertices[1] = p5 - p4;
                vertices[2] = p6 - p4;
                poly2.Vertices = vertices;
            }
            else
            {
                Vec2[] vertices = new Vec2[3];

                vertices[0] = p1;
                vertices[1] = p3;
                vertices[2] = p2;
                poly1.Vertices = vertices;

                vertices[0] = Vec2.Empty;
                vertices[1] = p6 - p4;
                vertices[2] = p5 - p4;
                poly2.Vertices = vertices;
            }

            fd1.Shape = poly1;
            fd2.Shape = poly2;

            BodyDef bd1 = new BodyDef(), bd2 = new BodyDef();
            bd1.BodyType = BodyType.Dynamic;
            bd2.BodyType = BodyType.Dynamic;
            bd1.Position = m_offset;
            bd2.Position = p4 + m_offset;

            bd1.AngularDamping = 10.0f;
            bd2.AngularDamping = 10.0f;

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

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

            DistanceJointDef djd = new DistanceJointDef();

            // 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);

            RevoluteJointDef rjd = new RevoluteJointDef();

            rjd.Initialize(body2, m_chassis, p4 + m_offset);
            m_world.CreateJoint(rjd);
        }