Example #1
0
        public CompoundShapes()
        {
            {
                BodyDef bd = new BodyDef();
                bd.position = new Vector2(0.0, 0.0);
                Body body = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vector2(50.0, 0.0), new Vector2(-50.0, 0.0));

                body.CreateFixture(shape, 0.0);
            }

            {
                CircleShape circle1 = new CircleShape();
                circle1._radius = 0.5f;
                circle1._p      = new Vector2(-0.5f, 0.5f);

                CircleShape circle2 = new CircleShape();
                circle2._radius = 0.5f;
                circle2._p      = new Vector2(0.5f, 0.5f);

                for (int i = 0; i < 10; ++i)
                {
                    double  x  = Rand.RandomFloat(-0.1f, 0.1f);
                    BodyDef bd = new BodyDef();
                    bd.type     = BodyType.Dynamic;
                    bd.position = new Vector2(x + 5.0f, 1.05f + 2.5f * i);
                    bd.angle    = Rand.RandomFloat(-Alt.Box2D.Settings.b2_pi, Alt.Box2D.Settings.b2_pi);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(circle1, 2.0f);
                    body.CreateFixture(circle2, 0.0f);
                }
            }

            {
                PolygonShape polygon1 = new PolygonShape();
                polygon1.SetAsBox(0.25f, 0.5f);

                PolygonShape polygon2 = new PolygonShape();
                polygon2.SetAsBox(0.25f, 0.5f, new Vector2(0.0f, -0.5f), 0.5f * Alt.Box2D.Settings.b2_pi);

                for (int i = 0; i < 10; ++i)
                {
                    double  x  = Rand.RandomFloat(-0.1f, 0.1f);
                    BodyDef bd = new BodyDef();
                    bd.type     = BodyType.Dynamic;
                    bd.position = new Vector2(x - 5.0f, 1.05f + 2.5f * i);
                    bd.angle    = Rand.RandomFloat(-Alt.Box2D.Settings.b2_pi, Alt.Box2D.Settings.b2_pi);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(polygon1, 2.0f);
                    body.CreateFixture(polygon2, 2.0f);
                }
            }

            {
                Alt.Box2D.Transform xf1 = new Alt.Box2D.Transform();
                xf1.R.Set(0.3524f * Alt.Box2D.Settings.b2_pi);
                xf1.Position = MathUtils.Multiply(ref xf1.R, new Vector2(1.0f, 0.0f));

                Vector2[] vertices = new Vector2[3];

                PolygonShape triangle1 = new PolygonShape();
                vertices[0] = MathUtils.Multiply(ref xf1, new Vector2(-1.0f, 0.0f));
                vertices[1] = MathUtils.Multiply(ref xf1, new Vector2(1.0f, 0.0f));
                vertices[2] = MathUtils.Multiply(ref xf1, new Vector2(0.0f, 0.5f));
                triangle1.Set(vertices, 3);

                Alt.Box2D.Transform xf2 = new Alt.Box2D.Transform();
                xf2.R.Set(-0.3524f * Alt.Box2D.Settings.b2_pi);
                xf2.Position = MathUtils.Multiply(ref xf2.R, new Vector2(-1.0f, 0.0f));

                PolygonShape triangle2 = new PolygonShape();
                vertices[0] = MathUtils.Multiply(ref xf2, new Vector2(-1.0f, 0.0f));
                vertices[1] = MathUtils.Multiply(ref xf2, new Vector2(1.0f, 0.0f));
                vertices[2] = MathUtils.Multiply(ref xf2, new Vector2(0.0f, 0.5f));
                triangle2.Set(vertices, 3);

                for (int i = 0; i < 10; ++i)
                {
                    double  x  = Rand.RandomFloat(-0.1f, 0.1f);
                    BodyDef bd = new BodyDef();
                    bd.type     = BodyType.Dynamic;
                    bd.position = new Vector2(x, 2.05f + 2.5f * i);
                    bd.angle    = 0.0f;
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(triangle1, 2.0f);
                    body.CreateFixture(triangle2, 2.0f);
                }
            }

            {
                PolygonShape bottom = new PolygonShape();
                bottom.SetAsBox(1.5f, 0.15f);

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

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

                BodyDef bd = new BodyDef();
                bd.type     = BodyType.Dynamic;
                bd.position = new Vector2(0.0f, 2.0f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(bottom, 4.0f);
                body.CreateFixture(left, 4.0f);
                body.CreateFixture(right, 4.0f);
            }
        }
Example #2
0
        public ApplyForce()
        {
            _world.Gravity = new Vector2(0.0, 0.0);

            const double k_restitution = 0.4;

            Body ground;
            {
                BodyDef bd = new BodyDef();
                bd.position = new Vector2(0.0f, 20.0f);
                ground      = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();

                FixtureDef sd = new FixtureDef();
                sd.shape       = shape;
                sd.density     = 0.0f;
                sd.restitution = k_restitution;

                // Left vertical
                shape.SetAsEdge(new Vector2(-20.0f, -20.0f), new Vector2(-20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Right vertical
                shape.SetAsEdge(new Vector2(20.0f, -20.0f), new Vector2(20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Top horizontal
                shape.SetAsEdge(new Vector2(-20.0f, 20.0f), new Vector2(20.0f, 20.0f));
                ground.CreateFixture(sd);

                // Bottom horizontal
                shape.SetAsEdge(new Vector2(-20.0f, -20.0f), new Vector2(20.0f, -20.0f));
                ground.CreateFixture(sd);
            }

            {
                Alt.Box2D.Transform xf1 = new Alt.Box2D.Transform();
                xf1.R.Set(0.3524 * Alt.Box2D.Settings.b2_pi);
                xf1.Position = MathUtils.Multiply(ref xf1.R, new Vector2(1.0f, 0.0f));

                Vector2[] vertices = new Vector2[3];
                vertices[0] = MathUtils.Multiply(ref xf1, new Vector2(-1.0f, 0.0f));
                vertices[1] = MathUtils.Multiply(ref xf1, new Vector2(1.0f, 0.0f));
                vertices[2] = MathUtils.Multiply(ref xf1, new Vector2(0.0f, 0.5f));

                PolygonShape poly1 = new PolygonShape();
                poly1.Set(vertices, 3);

                FixtureDef sd1 = new FixtureDef();
                sd1.shape   = poly1;
                sd1.density = 4.0f;

                Alt.Box2D.Transform xf2 = new Alt.Box2D.Transform();
                xf2.R.Set(-0.3524 * Alt.Box2D.Settings.b2_pi);
                xf2.Position = MathUtils.Multiply(ref xf2.R, new Vector2(-1.0f, 0.0f));

                vertices[0] = MathUtils.Multiply(ref xf2, new Vector2(-1.0f, 0.0f));
                vertices[1] = MathUtils.Multiply(ref xf2, new Vector2(1.0f, 0.0f));
                vertices[2] = MathUtils.Multiply(ref xf2, new Vector2(0.0f, 0.5f));

                PolygonShape poly2 = new PolygonShape();
                poly2.Set(vertices, 3);

                FixtureDef sd2 = new FixtureDef();
                sd2.shape   = poly2;
                sd2.density = 2.0f;

                BodyDef bd = new BodyDef();
                bd.type           = BodyType.Dynamic;
                bd.angularDamping = 5.0f;
                bd.linearDamping  = 0.1f;

                bd.position   = new Vector2(0.0f, 2.0f);
                bd.angle      = Alt.Box2D.Settings.b2_pi;
                bd.allowSleep = false;
                _body         = _world.CreateBody(bd);
                _body.CreateFixture(sd1);
                _body.CreateFixture(sd2);
            }

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

                FixtureDef fd = new FixtureDef();
                fd.shape    = shape;
                fd.density  = 1.0f;
                fd.friction = 0.3f;

                for (int i = 0; i < 10; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;

                    bd.position = new Vector2(0.0f, 5.0f + 1.54f * i);
                    Body body = _world.CreateBody(bd);

                    body.CreateFixture(fd);

                    double gravity = 10.0f;
                    double I       = body.GetInertia();
                    double mass    = body.GetMass();

                    // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m)
                    double radius = (double)Math.Sqrt(2.0 * (double)(I / mass));

                    FrictionJointDef jd = new FrictionJointDef();
                    jd.localAnchorA     = Vector2.Zero;
                    jd.localAnchorB     = Vector2.Zero;
                    jd.bodyA            = ground;
                    jd.bodyB            = body;
                    jd.collideConnected = true;
                    jd.maxForce         = mass * gravity;
                    jd.maxTorque        = mass * radius * gravity;

                    _world.CreateJoint(jd);
                }
            }
        }