Ejemplo n.º 1
0
        ApplyForceTest()
        {
            World.Gravity = Vector2.Zero;

            const double restitution = 0.4f;

            Body ground;
            {
                ground          = BodyFactory.CreateBody(World);
                ground.Position = new Vector2(0.0f, 20.0f);

                EdgeShape shape = new EdgeShape(new Vector2(-20.0f, -20.0f), new Vector2(-20.0f, 20.0f));

                // Left vertical
                Fixture fixture = ground.CreateFixture(shape);
                fixture.Restitution = restitution;

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

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

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

            {
                Alt.FarseerPhysics.Common.Transform xf1 = new Alt.FarseerPhysics.Common.Transform();
                xf1.q.Set(0.3524f * Alt.FarseerPhysics.Settings.Pi);
                xf1.p = Alt.FarseerPhysics.Common.MathUtils.Mul(ref xf1.q, new Vector2(1.0f, 0.0f));

                Vertices vertices = new Vertices(3);
                vertices.Add(Alt.FarseerPhysics.Common.MathUtils.Mul(ref xf1, new Vector2(-1.0f, 0.0f)));
                vertices.Add(Alt.FarseerPhysics.Common.MathUtils.Mul(ref xf1, new Vector2(1.0f, 0.0f)));
                vertices.Add(Alt.FarseerPhysics.Common.MathUtils.Mul(ref xf1, new Vector2(0.0f, 0.5f)));

                PolygonShape poly1 = new PolygonShape(vertices, 4);

                Alt.FarseerPhysics.Common.Transform xf2 = new Alt.FarseerPhysics.Common.Transform();
                xf2.q.Set(-0.3524f * Alt.FarseerPhysics.Settings.Pi);
                xf2.p = Alt.FarseerPhysics.Common.MathUtils.Mul(ref xf2.q, new Vector2(-1.0f, 0.0f));

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

                PolygonShape poly2 = new PolygonShape(vertices, 2);

                _body                 = BodyFactory.CreateBody(World);
                _body.BodyType        = BodyType.Dynamic;
                _body.Position        = new Vector2(0.0f, 2.0f);
                _body.Rotation        = Alt.FarseerPhysics.Settings.Pi;
                _body.AngularDamping  = 5.0f;
                _body.LinearDamping   = 0.8f;
                _body.SleepingAllowed = true;

                _body.CreateFixture(poly1);
                _body.CreateFixture(poly2);
            }

            {
                Vertices     box   = PolygonTools.CreateRectangle(0.5f, 0.5f);
                PolygonShape shape = new PolygonShape(box, 1);

                for (int i = 0; i < 10; ++i)
                {
                    Body body = BodyFactory.CreateBody(World);
                    body.Position = new Vector2(0.0f, 5.0f + 1.54f * i);
                    body.BodyType = BodyType.Dynamic;

                    Fixture fixture = body.CreateFixture(shape);
                    fixture.Friction = 0.3f;

                    const double gravity = 10.0f;
                    double       I       = body.Inertia;
                    double       mass    = body.Mass;

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

                    FrictionJoint jd = new FrictionJoint(ground, body, Vector2.Zero);
                    jd.CollideConnected = true;
                    jd.MaxForce         = mass * gravity;
                    jd.MaxTorque        = mass * radius * gravity;

                    World.AddJoint(jd);
                }
            }
        }
Ejemplo n.º 2
0
        CompoundShapesTest()
        {
            BodyFactory.CreateEdge(World, new Vector2(50.0f, 0.0f), new Vector2(-50.0f, 0.0f));

            {
                CircleShape circle1 = new CircleShape(0.5f, 2);
                circle1.Position = new Vector2(-0.5f, 0.5f);

                CircleShape circle2 = new CircleShape(0.5f, 0);
                circle2.Position = new Vector2(0.5f, 0.5f);

                for (int i = 0; i < 10; ++i)
                {
                    double x = Rand.RandomFloat(-0.1f, 0.1f);

                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(x + 5.0f, 1.05f + 2.5f * i);
                    body.Rotation = Rand.RandomFloat(-Alt.FarseerPhysics.Settings.Pi, Alt.FarseerPhysics.Settings.Pi);

                    body.CreateFixture(circle1);
                    body.CreateFixture(circle2);
                }
            }

            {
                Vertices     box      = PolygonTools.CreateRectangle(0.25f, 0.5f);
                PolygonShape polygon1 = new PolygonShape(box, 2);

                box = PolygonTools.CreateRectangle(0.25f, 0.5f, new Vector2(0.0f, -0.5f), 0.5f * Alt.FarseerPhysics.Settings.Pi);
                PolygonShape polygon2 = new PolygonShape(box, 2);

                for (int i = 0; i < 10; ++i)
                {
                    double x = Rand.RandomFloat(-0.1f, 0.1f);

                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(x - 5.0f, 1.05f + 2.5f * i);
                    body.Rotation = Rand.RandomFloat(-Alt.FarseerPhysics.Settings.Pi, Alt.FarseerPhysics.Settings.Pi);

                    body.CreateFixture(polygon1);
                    body.CreateFixture(polygon2);
                }
            }

            {
                Alt.FarseerPhysics.Common.Transform xf1 = new Alt.FarseerPhysics.Common.Transform();
                xf1.q.Set(0.3524f * Alt.FarseerPhysics.Settings.Pi);
                xf1.p = Alt.FarseerPhysics.Common.MathUtils.Mul(ref xf1.q, new Vector2(1.0f, 0.0f));

                Vertices vertices = new Vertices(3);

                vertices.Add(Alt.FarseerPhysics.Common.MathUtils.Mul(ref xf1, new Vector2(-1.0f, 0.0f)));
                vertices.Add(Alt.FarseerPhysics.Common.MathUtils.Mul(ref xf1, new Vector2(1.0f, 0.0f)));
                vertices.Add(Alt.FarseerPhysics.Common.MathUtils.Mul(ref xf1, new Vector2(0.0f, 0.5f)));

                PolygonShape triangle1 = new PolygonShape(vertices, 2);

                Alt.FarseerPhysics.Common.Transform xf2 = new Alt.FarseerPhysics.Common.Transform();
                xf2.q.Set(-0.3524f * Alt.FarseerPhysics.Settings.Pi);
                xf2.p = MathUtils.Mul(ref xf2.q, new Vector2(-1.0f, 0.0f));

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

                PolygonShape triangle2 = new PolygonShape(vertices, 2);

                for (int i = 0; i < 10; ++i)
                {
                    double x = Rand.RandomFloat(-0.1f, 0.1f);

                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(x, 2.05f + 2.5f * i);

                    body.CreateFixture(triangle1);
                    body.CreateFixture(triangle2);
                }
            }

            {
                Vertices     box    = PolygonTools.CreateRectangle(1.5f, 0.15f);
                PolygonShape bottom = new PolygonShape(box, 4);

                box = PolygonTools.CreateRectangle(0.15f, 2.7f, new Vector2(-1.45f, 2.35f), 0.2f);
                PolygonShape left = new PolygonShape(box, 4);

                box = PolygonTools.CreateRectangle(0.15f, 2.7f, new Vector2(1.45f, 2.35f), -0.2f);
                PolygonShape right = new PolygonShape(box, 4);

                Body body = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(0.0f, 2.0f);

                body.CreateFixture(bottom);
                body.CreateFixture(left);
                body.CreateFixture(right);
            }
        }