示例#1
0
        public ShapeEditing()
        {
            {
                var bd     = new BodyDef();
                var ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(0.0f, 10.0f);
                _body = World.CreateBody(bd);

                var shape = new PolygonShape();
                shape.SetAsBox(4.0f, 4.0f, new Vector2(0.0f, 0.0f), 0.0f);
                _fixture1 = _body.CreateFixture(shape, 10.0f);
            }
            _fixture2 = null;
            _sensor   = false;
        }
        public GearJoint()
        {
            Body ground;
            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(50.0f, 0.0f), new Vector2(-50.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

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

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

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

                var bd1 = new BodyDef();
                bd1.BodyType = BodyType.StaticBody;
                bd1.Position.Set(10.0f, 9.0f);
                var body1 = World.CreateBody(bd1);
                body1.CreateFixture(circle1, 5.0f);

                var bd2 = new BodyDef();
                bd2.BodyType = BodyType.DynamicBody;
                bd2.Position.Set(10.0f, 8.0f);
                var body2 = World.CreateBody(bd2);
                body2.CreateFixture(box, 5.0f);

                var bd3 = new BodyDef();
                bd3.BodyType = BodyType.DynamicBody;
                bd3.Position.Set(10.0f, 6.0f);
                var body3 = World.CreateBody(bd3);
                body3.CreateFixture(circle2, 5.0f);

                var jd1 = new RevoluteJointDef();
                jd1.Initialize(body1, body2, bd1.Position);
                var joint1 = World.CreateJoint(jd1);

                var jd2 = new RevoluteJointDef();
                jd2.Initialize(body2, body3, bd3.Position);
                var joint2 = World.CreateJoint(jd2);

                var jd4 = new GearJointDef();
                jd4.BodyA  = body1;
                jd4.BodyB  = body3;
                jd4.Joint1 = joint1;
                jd4.Joint2 = joint2;
                jd4.Ratio  = circle2.Radius / circle1.Radius;
                World.CreateJoint(jd4);
            }

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

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

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

                var bd1 = new BodyDef();
                bd1.BodyType = BodyType.DynamicBody;
                bd1.Position.Set(-3.0f, 12.0f);
                var body1 = World.CreateBody(bd1);
                body1.CreateFixture(circle1, 5.0f);

                var jd1 = new RevoluteJointDef();
                jd1.BodyA          = ground;
                jd1.BodyB          = body1;
                jd1.LocalAnchorA   = ground.GetLocalPoint(bd1.Position);
                jd1.LocalAnchorB   = body1.GetLocalPoint(bd1.Position);
                jd1.ReferenceAngle = body1.GetAngle() - ground.GetAngle();
                _joint1            = (RevoluteJoint)World.CreateJoint(jd1);

                var bd2 = new BodyDef();
                bd2.BodyType = BodyType.DynamicBody;
                bd2.Position.Set(0.0f, 12.0f);
                var body2 = World.CreateBody(bd2);
                body2.CreateFixture(circle2, 5.0f);

                var jd2 = new RevoluteJointDef();
                jd2.Initialize(ground, body2, bd2.Position);
                _joint2 = (RevoluteJoint)World.CreateJoint(jd2);

                var bd3 = new BodyDef();
                bd3.BodyType = BodyType.DynamicBody;
                bd3.Position.Set(2.5f, 12.0f);
                var body3 = World.CreateBody(bd3);
                body3.CreateFixture(box, 5.0f);

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

                _joint3 = (PrismaticJoint)World.CreateJoint(jd3);

                var jd4 = new GearJointDef();
                jd4.BodyA  = body1;
                jd4.BodyB  = body2;
                jd4.Joint1 = _joint1;
                jd4.Joint2 = _joint2;
                jd4.Ratio  = circle2.Radius / circle1.Radius;
                _joint4    = (Box2DSharp.Dynamics.Joints.GearJoint)World.CreateJoint(jd4);

                var jd5 = new GearJointDef();
                jd5.BodyA  = body2;
                jd5.BodyB  = body3;
                jd5.Joint1 = _joint2;
                jd5.Joint2 = _joint3;
                jd5.Ratio  = -1.0f / circle2.Radius;
                _joint5    = (Box2DSharp.Dynamics.Joints.GearJoint)World.CreateJoint(jd5);
            }
        }
示例#3
0
 public EdgeAndPolygonContact(Fixture fA, int indexA, Fixture fB, int indexB) : base(fA, indexA, fB, indexB)
 {
     edgeA    = (EdgeShape)m_fixtureA.Shape;
     polygonB = (PolygonShape)m_fixtureB.Shape;
 }
示例#4
0
        public ApplyForce()
        {
            World.Gravity = new Vector2(0.0f, 0.0f);

            const float restitution = 0.4f;

            Body ground;
            {
                var bd = new BodyDef();
                bd.Position.Set(0.0f, 20.0f);
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();

                var sd = new FixtureDef();
                sd.Shape       = shape;
                sd.Density     = 0.0f;
                sd.Restitution = restitution;

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

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

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

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

            {
                var xf1 = new Transform();
                xf1.Rotation.Set(0.3524f * Settings.Pi);
                xf1.Position = xf1.Rotation.GetXAxis();

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

                var poly1 = new PolygonShape();
                poly1.Set(vertices);

                var sd1 = new FixtureDef();
                sd1.Shape   = poly1;
                sd1.Density = 2.0f;

                var xf2 = new Transform();
                xf2.Rotation.Set(-0.3524f * Settings.Pi);
                xf2.Position = -xf2.Rotation.GetXAxis();

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

                var poly2 = new PolygonShape();
                poly2.Set(vertices);

                var sd2 = new FixtureDef();
                sd2.Shape   = poly2;
                sd2.Density = 2.0f;

                var bd = new BodyDef();
                bd.BodyType   = BodyType.DynamicBody;
                bd.Position   = new Vector2(0.0f, 3.0f);
                bd.Angle      = Settings.Pi;
                bd.AllowSleep = false;
                _body         = World.CreateBody(bd);
                _body.CreateFixture(sd1);
                _body.CreateFixture(sd2);

                var gravity = 10.0f;
                var I       = _body.Inertia;
                var mass    = _body.Mass;

                // Compute an effective radius that can be used to
                // set the max torque for a friction joint
                // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m)
                var radius = (float)Math.Sqrt(2.0f * I / mass);

                FrictionJointDef jd = new FrictionJointDef();
                jd.BodyA = ground;
                jd.BodyB = _body;
                jd.LocalAnchorA.SetZero();
                jd.LocalAnchorB     = _body.GetLocalCenter();
                jd.CollideConnected = true;
                jd.MaxForce         = 0.5f * mass * gravity;
                jd.MaxTorque        = 0.2f * mass * radius * gravity;

                World.CreateJoint(jd);
            }

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

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

                for (var i = 0; i < 10; ++i)
                {
                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;

                    bd.Position.Set(0.0f, 7.0f + 1.54f * i);
                    var body = World.CreateBody(bd);

                    body.CreateFixture(fd);

                    var gravity = 10.0f;
                    var I       = body.Inertia;
                    var mass    = body.Mass;

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

                    var jd = new FrictionJointDef();
                    jd.LocalAnchorA.SetZero();
                    jd.LocalAnchorB.SetZero();
                    jd.BodyA            = ground;
                    jd.BodyB            = body;
                    jd.CollideConnected = true;
                    jd.MaxForce         = mass * gravity;
                    jd.MaxTorque        = 0.1f * mass * radius * gravity;

                    World.CreateJoint(jd);
                }
            }
        }
示例#5
0
        public Bridge()
        {
            Body ground;

            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

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

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

                var jd = new RevoluteJointDef();

                var prevBody = ground;
                for (var i = 0; i < Count; ++i)
                {
                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position.Set(-14.5f + 1.0f * i, 5.0f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(fd);

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

                    if (i == Count >> 1)
                    {
                        _middle = body;
                    }

                    prevBody = body;
                }

                {
                    var anchor = new Vector2(-15.0f + 1.0f * Count, 5.0f);
                    jd.Initialize(prevBody, ground, anchor);
                    World.CreateJoint(jd);
                }
            }

            for (var i = 0; i < 2; ++i)
            {
                var vertices = new Vector2[3];
                vertices[0].Set(-0.5f, 0.0f);
                vertices[1].Set(0.5f, 0.0f);
                vertices[2].Set(0.0f, 1.5f);

                var shape = new PolygonShape();
                shape.Set(vertices);

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

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(-8.0f + 8.0f * i, 12.0f);
                var body = World.CreateBody(bd);
                body.CreateFixture(fd);
            }

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

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

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(-6.0f + 6.0f * i, 10.0f);
                var body = World.CreateBody(bd);
                body.CreateFixture(fd);
            }
        }
        private BridgeTest()
        {
            Body ground;
            {
                ground = BodyFactory.CreateBody(World);

                EdgeShape shape = new EdgeShape(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape);
            }
            {
                Vertices     box   = PolygonUtils.CreateRectangle(0.5f, 0.125f);
                PolygonShape shape = new PolygonShape(box, 20);

                Body prevBody = ground;
                for (int i = 0; i < Count; ++i)
                {
                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(-14.5f + 1.0f * i, 5.0f);

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

                    Vector2       anchor = new Vector2(-15f + 1.0f * i, 5.0f);
                    RevoluteJoint jd     = new RevoluteJoint(prevBody, body, anchor, true);
                    World.AddJoint(jd);

                    prevBody = body;
                }

                Vector2       anchor2 = new Vector2(-15.0f + 1.0f * Count, 5.0f);
                RevoluteJoint jd2     = new RevoluteJoint(ground, prevBody, anchor2, true);
                World.AddJoint(jd2);
            }

            Vertices vertices = new Vertices(3);

            vertices.Add(new Vector2(-0.5f, 0.0f));
            vertices.Add(new Vector2(0.5f, 0.0f));
            vertices.Add(new Vector2(0.0f, 1.5f));

            for (int i = 0; i < 2; ++i)
            {
                PolygonShape shape = new PolygonShape(vertices, 1);

                Body body = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(-8.0f + 8.0f * i, 12.0f);

                body.CreateFixture(shape);
            }

            for (int i = 0; i < 3; ++i)
            {
                CircleShape shape = new CircleShape(0.5f, 1);

                Body body = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(-6.0f + 6.0f * i, 10.0f);

                body.CreateFixture(shape);
            }
        }
示例#7
0
        public Fixture CreateEdge(Vector2 start, Vector2 end)
        {
            EdgeShape edgeShape = new EdgeShape(start, end);

            return(CreateFixture(edgeShape));
        }
示例#8
0
        protected override void Create()
        {
            _hz    = 4.0f;
            _zeta  = 0.7f;
            _speed = 50.0f;

            Body ground;
            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();

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

                shape.Set(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));
                ground.CreateFixture(fd);

                float[] hs = { 0.25f, 1.0f, 4.0f, 0.0f, 0.0f, -1.0f, -2.0f, -2.0f, -1.25f, 0.0f };

                float x = 20.0f, y1 = 0.0f, dx = 5.0f;

                for (var i = 0; i < 10; ++i)
                {
                    var y2 = hs[i];
                    shape.Set(new Vector2(x, y1), new Vector2(x + dx, y2));
                    ground.CreateFixture(fd);
                    y1 = y2;
                    x += dx;
                }

                for (var i = 0; i < 10; ++i)
                {
                    var y2 = hs[i];
                    shape.Set(new Vector2(x, y1), new Vector2(x + dx, y2));
                    ground.CreateFixture(fd);
                    y1 = y2;
                    x += dx;
                }

                shape.Set(new Vector2(x, 0.0f), new Vector2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 80.0f;
                shape.Set(new Vector2(x, 0.0f), new Vector2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 40.0f;
                shape.Set(new Vector2(x, 0.0f), new Vector2(x + 10.0f, 5.0f));
                ground.CreateFixture(fd);

                x += 20.0f;
                shape.Set(new Vector2(x, 0.0f), new Vector2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 40.0f;
                shape.Set(new Vector2(x, 0.0f), new Vector2(x, 20.0f));
                ground.CreateFixture(fd);
            }

            // Teeter
            {
                var bd = new BodyDef();
                bd.Position.Set(140.0f, 1.0f);
                bd.BodyType = BodyType.DynamicBody;
                var body = World.CreateBody(bd);

                var box = new PolygonShape();
                box.SetAsBox(10.0f, 0.25f);
                body.CreateFixture(box, 1.0f);

                var jd = new RevoluteJointDef();
                jd.Initialize(ground, body, body.GetPosition());
                jd.LowerAngle  = -8.0f * Settings.Pi / 180.0f;
                jd.UpperAngle  = 8.0f * Settings.Pi / 180.0f;
                jd.EnableLimit = true;
                World.CreateJoint(jd);

                body.ApplyAngularImpulse(100.0f, true);
            }

            // Bridge
            {
                var N     = 20;
                var shape = new PolygonShape();
                shape.SetAsBox(1.0f, 0.125f);

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

                var jd = new RevoluteJointDef();

                var prevBody = ground;
                for (var i = 0; i < N; ++i)
                {
                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position.Set(161.0f + 2.0f * i, -0.125f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(fd);

                    var anchor = new Vector2(160.0f + 2.0f * i, -0.125f);
                    jd.Initialize(prevBody, body, anchor);
                    World.CreateJoint(jd);

                    prevBody = body;
                }

                {
                    var anchor = new Vector2(160.0f + 2.0f * N, -0.125f);
                    jd.Initialize(prevBody, ground, anchor);
                    World.CreateJoint(jd);
                }
            }

            // Boxes
            {
                var box = new PolygonShape();
                box.SetAsBox(0.5f, 0.5f);

                Body body = null;
                var  bd   = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;

                bd.Position.Set(230.0f, 0.5f);
                body = World.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.Position.Set(230.0f, 1.5f);
                body = World.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.Position.Set(230.0f, 2.5f);
                body = World.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.Position.Set(230.0f, 3.5f);
                body = World.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.Position.Set(230.0f, 4.5f);
                body = World.CreateBody(bd);
                body.CreateFixture(box, 0.5f);
            }

            // Car
            {
                var chassis  = new PolygonShape();
                var vertices = new Vector2[8];
                vertices[0].Set(-1.5f, -0.5f);
                vertices[1].Set(1.5f, -0.5f);
                vertices[2].Set(1.5f, 0.0f);
                vertices[3].Set(0.0f, 0.9f);
                vertices[4].Set(-1.15f, 0.9f);
                vertices[5].Set(-1.5f, 0.2f);
                chassis.Set(vertices);

                var circle = new CircleShape();
                circle.Radius = 0.4f;

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(0.0f, 1.0f);
                _car = World.CreateBody(bd);
                _car.CreateFixture(chassis, 1.0f);

                var fd = new FixtureDef();
                fd.Shape    = circle;
                fd.Density  = 1.0f;
                fd.Friction = 0.9f;

                bd.Position.Set(-1.0f, 0.35f);
                _wheel1 = World.CreateBody(bd);
                _wheel1.CreateFixture(fd);

                bd.Position.Set(1.0f, 0.4f);
                _wheel2 = World.CreateBody(bd);
                _wheel2.CreateFixture(fd);

                var jd   = new WheelJointDef();
                var axis = new Vector2(0.0f, 1.0f);

                jd.Initialize(_car, _wheel1, _wheel1.GetPosition(), axis);
                jd.MotorSpeed     = 0.0f;
                jd.MaxMotorTorque = 20.0f;
                jd.EnableMotor    = true;
                jd.FrequencyHz    = _hz;
                jd.DampingRatio   = _zeta;
                _spring1          = (WheelJoint)World.CreateJoint(jd);

                jd.Initialize(_car, _wheel2, _wheel2.GetPosition(), axis);
                jd.MotorSpeed     = 0.0f;
                jd.MaxMotorTorque = 10.0f;
                jd.EnableMotor    = false;
                jd.FrequencyHz    = _hz;
                jd.DampingRatio   = _zeta;
                _spring2          = (WheelJoint)World.CreateJoint(jd);
            }
        }
示例#9
0
        public Cantilever()
        {
            Body ground;

            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.Set(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

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

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

                var jd = new WeldJointDef();

                var prevBody = ground;
                for (var i = 0; i < Count; ++i)
                {
                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position.Set(-14.5f + 1.0f * i, 5.0f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(fd);

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

                    prevBody = body;
                }
            }

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

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

                var jd = new WeldJointDef();
                jd.FrequencyHz  = 5.0f;
                jd.DampingRatio = 0.7f;

                var prevBody = ground;
                for (var i = 0; i < 3; ++i)
                {
                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position.Set(-14.0f + 2.0f * i, 15.0f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(fd);

                    var anchor = new Vector2(-15.0f + 2.0f * i, 15.0f);
                    jd.Initialize(prevBody, body, anchor);
                    World.CreateJoint(jd);

                    prevBody = body;
                }
            }

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

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

                var jd = new WeldJointDef();

                var prevBody = ground;
                for (var i = 0; i < Count; ++i)
                {
                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position.Set(-4.5f + 1.0f * i, 5.0f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(fd);

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

                    prevBody = body;
                }
            }

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

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

                var jd = new WeldJointDef();
                jd.FrequencyHz  = 8.0f;
                jd.DampingRatio = 0.7f;

                var prevBody = ground;
                for (var i = 0; i < Count; ++i)
                {
                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position.Set(5.5f + 1.0f * i, 10.0f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(fd);

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

                    prevBody = body;
                }
            }

            for (var i = 0; i < 2; ++i)
            {
                var vertices = new Vector2 [3];
                vertices[0].Set(-0.5f, 0.0f);
                vertices[1].Set(0.5f, 0.0f);
                vertices[2].Set(0.0f, 1.5f);

                var shape = new PolygonShape();
                shape.Set(vertices);

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

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(-8.0f + 8.0f * i, 12.0f);
                var body = World.CreateBody(bd);
                body.CreateFixture(fd);
            }

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

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

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(-6.0f + 6.0f * i, 10.0f);
                var body = World.CreateBody(bd);
                body.CreateFixture(fd);
            }
        }
示例#10
0
        public CompoundShapes()
        {
            {
                BodyDef bd = new BodyDef();
                bd.Position.Set(0.0f, 0.0f);
                Body body = World.CreateBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(50.0f, 0.0f), new Vector2(-50.0f, 0.0f));

                body.CreateFixture(shape, 0.0f);
            }

            // Table 1
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(-15.0f, 1.0f);
                m_table1 = World.CreateBody(bd);

                PolygonShape top = new PolygonShape();
                top.SetAsBox(3.0f, 0.5f, new Vector2(0.0f, 3.5f), 0.0f);

                PolygonShape leftLeg = new PolygonShape();
                leftLeg.SetAsBox(0.5f, 1.5f, new Vector2(-2.5f, 1.5f), 0.0f);

                PolygonShape rightLeg = new PolygonShape();
                rightLeg.SetAsBox(0.5f, 1.5f, new Vector2(2.5f, 1.5f), 0.0f);

                m_table1.CreateFixture(top, 2.0f);
                m_table1.CreateFixture(leftLeg, 2.0f);
                m_table1.CreateFixture(rightLeg, 2.0f);
            }

            // Table 2
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(-5.0f, 1.0f);
                m_table2 = World.CreateBody(bd);

                PolygonShape top = new PolygonShape();
                top.SetAsBox(3.0f, 0.5f, new Vector2(0.0f, 3.5f), 0.0f);

                PolygonShape leftLeg = new PolygonShape();
                leftLeg.SetAsBox(0.5f, 2.0f, new Vector2(-2.5f, 2.0f), 0.0f);

                PolygonShape rightLeg = new PolygonShape();
                rightLeg.SetAsBox(0.5f, 2.0f, new Vector2(2.5f, 2.0f), 0.0f);

                m_table2.CreateFixture(top, 2.0f);
                m_table2.CreateFixture(leftLeg, 2.0f);
                m_table2.CreateFixture(rightLeg, 2.0f);
            }

            // Spaceship 1
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(5.0f, 1.0f);
                m_ship1 = World.CreateBody(bd);

                Vector2[] vertices = new Vector2[3];

                PolygonShape left = new PolygonShape();
                vertices[0].Set(-2.0f, 0.0f);
                vertices[1].Set(0.0f, 4.0f / 3.0f);
                vertices[2].Set(0.0f, 4.0f);
                left.Set(vertices, 3);

                PolygonShape right = new PolygonShape();
                vertices[0].Set(2.0f, 0.0f);
                vertices[1].Set(0.0f, 4.0f / 3.0f);
                vertices[2].Set(0.0f, 4.0f);
                right.Set(vertices, 3);

                m_ship1.CreateFixture(left, 2.0f);
                m_ship1.CreateFixture(right, 2.0f);
            }

            // Spaceship 2
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(15.0f, 1.0f);
                m_ship2 = World.CreateBody(bd);

                Vector2[] vertices = new Vector2[3];

                PolygonShape left = new PolygonShape();
                vertices[0].Set(-2.0f, 0.0f);
                vertices[1].Set(1.0f, 2.0f);
                vertices[2].Set(0.0f, 4.0f);
                left.Set(vertices, 3);

                PolygonShape right = new PolygonShape();
                vertices[0].Set(2.0f, 0.0f);
                vertices[1].Set(-1.0f, 2.0f);
                vertices[2].Set(0.0f, 4.0f);
                right.Set(vertices, 3);

                m_ship2.CreateFixture(left, 2.0f);
                m_ship2.CreateFixture(right, 2.0f);
            }
        }
示例#11
0
        public static Fixture AttachEdge(Vector2 start, Vector2 end, Body body)
        {
            EdgeShape edgeShape = new EdgeShape(start, end);

            return(body.CreateFixture(edgeShape, null));
        }
        public static World CreateWorld()
        {
            var gravity = new Vector2(0.000000000000000e+00f, -1.000000000000000e+01f);
            var world   = new World(gravity);
            var bodies  = new Body[2];

            {
                BodyDef bd = new BodyDef();
                bd.type            = BodyType.Static;
                bd.position        = new Vector2(0.000000000000000e+00f, 0.000000000000000e+00f);
                bd.angle           = 0.000000000000000e+00f;
                bd.linearVelocity  = new Vector2(0.000000000000000e+00f, 0.000000000000000e+00f);
                bd.angularVelocity = 0.000000000000000e+00f;
                bd.linearDamping   = 0.000000000000000e+00f;
                bd.angularDamping  = 0.000000000000000e+00f;
                bd.allowSleep      = true;
                bd.awake           = true;
                bd.fixedRotation   = false;
                bd.bullet          = false;
                //bd.active          = true;
                bd.gravityScale = 1.000000000000000e+00f;
                bodies[0]       = world.CreateBody(bd);

                {
                    FixtureDef fd = new FixtureDef();
                    fd.friction            = 2.000000029802322e-01f;
                    fd.restitution         = 2.000000029802322e-01f;
                    fd.density             = 1.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = (1);
                    fd.filter.maskBits     = (65535);
                    fd.filter.groupIndex   = (0);
                    PolygonShape shape = new PolygonShape();
                    Vector2[]    vs    = new Vector2[4];
                    vs[0] = new Vector2(1.622321891784668e+01f, 4.735573291778564e+00f);
                    vs[1] = new Vector2(1.601119422912598e+01f, 4.735989570617676e+00f);
                    vs[2] = new Vector2(1.602764320373535e+01f, 3.814474105834961e+00f);
                    vs[3] = new Vector2(1.621816253662109e+01f, 3.825283050537109e+00f);
                    shape.Set(vs);

                    fd.shape = shape;

                    bodies[0].CreateFixture(fd);
                }
                {
                    FixtureDef fd = new FixtureDef();
                    fd.friction            = 2.000000029802322e-01f;
                    fd.restitution         = 0.000000000000000e+00f;
                    fd.density             = 0.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = (1);
                    fd.filter.maskBits     = (65535);
                    fd.filter.groupIndex   = (0);
                    EdgeShape shape = new EdgeShape();
                    Vector2[] vs    = new Vector2[4];
                    ;
                    vs[0] = new Vector2(1.918307685852051e+01f, 3.306496143341064e+00f);
                    vs[1] = new Vector2(1.703903770446777e+01f, 2.196289300918579e+00f);

                    vs[2] = new Vector2(4.565430396770254e-41f, 0.000000000000000e+00f);
                    vs[3] = new Vector2(0.000000000000000e+00f, 1.216642260551453e-01f);

                    shape.SetTwoSided(vs[0], vs[1]);
                    ;

                    fd.shape = shape;

                    bodies[0].CreateFixture(fd);
                }
                {
                    FixtureDef fd = new FixtureDef();
                    fd.friction            = 2.000000029802322e-01f;
                    fd.restitution         = 0.000000000000000e+00f;
                    fd.density             = 0.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = (1);
                    fd.filter.maskBits     = (65535);
                    fd.filter.groupIndex   = (0);
                    EdgeShape shape = new EdgeShape();
                    Vector2[] vs    = new Vector2[4];
                    ;
                    vs[0] = new Vector2(2.044397354125977e+01f, 4.096230506896973e+00f);
                    vs[1] = new Vector2(1.918307685852051e+01f, 3.306496143341064e+00f);

                    vs[2] = new Vector2(4.565430396770254e-41f, 0.000000000000000e+00f);
                    vs[3] = new Vector2(0.000000000000000e+00f, 1.216642260551453e-01f);

                    shape.SetTwoSided(vs[0], vs[1]);
                    ;

                    fd.shape = shape;

                    bodies[0].CreateFixture(fd);
                }
                {
                    FixtureDef fd = new FixtureDef();
                    fd.friction            = 2.000000029802322e-01f;
                    fd.restitution         = 0.000000000000000e+00f;
                    fd.density             = 0.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = (1);
                    fd.filter.maskBits     = (65535);
                    fd.filter.groupIndex   = (0);
                    EdgeShape shape = new EdgeShape();
                    Vector2[] vs    = new Vector2[4];
                    ;
                    vs[0] = new Vector2(2.176111412048340e+01f, 5.379695892333984e+00f);
                    vs[1] = new Vector2(2.115560913085938e+01f, 4.700156211853027e+00f);

                    vs[2] = new Vector2(4.565430396770254e-41f, 0.000000000000000e+00f);
                    vs[3] = new Vector2(0.000000000000000e+00f, 1.216642260551453e-01f);

                    shape.SetTwoSided(vs[0], vs[1]);
                    ;

                    fd.shape = shape;

                    bodies[0].CreateFixture(fd);
                }
                {
                    FixtureDef fd = new FixtureDef();
                    fd.friction            = 2.000000029802322e-01f;
                    fd.restitution         = 0.000000000000000e+00f;
                    fd.density             = 0.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = (1);
                    fd.filter.maskBits     = (65535);
                    fd.filter.groupIndex   = (0);
                    EdgeShape shape = new EdgeShape();
                    Vector2[] vs    = new Vector2[4];
                    ;
                    vs[0] = new Vector2(2.206690788269043e+01f, 5.190674781799316e+00f);
                    vs[1] = new Vector2(2.176111412048340e+01f, 5.379695892333984e+00f);

                    vs[2] = new Vector2(4.565430396770254e-41f, 0.000000000000000e+00f);
                    vs[3] = new Vector2(0.000000000000000e+00f, 1.216642260551453e-01f);

                    shape.SetTwoSided(vs[0], vs[1]);
                    ;

                    fd.shape = shape;

                    bodies[0].CreateFixture(fd);
                }
                {
                    FixtureDef fd = new FixtureDef();
                    fd.friction            = 2.000000029802322e-01f;
                    fd.restitution         = 0.000000000000000e+00f;
                    fd.density             = 0.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = (1);
                    fd.filter.maskBits     = (65535);
                    fd.filter.groupIndex   = (0);
                    EdgeShape shape = new EdgeShape();
                    Vector2[] vs    = new Vector2[4];
                    ;
                    vs[0] = new Vector2(1.703903770446777e+01f, 2.196289300918579e+00f);
                    vs[1] = new Vector2(1.560352897644043e+01f, 1.560027003288269e+00f);

                    vs[2] = new Vector2(4.565430396770254e-41f, 0.000000000000000e+00f);
                    vs[3] = new Vector2(0.000000000000000e+00f, 1.216642260551453e-01f);

                    shape.SetTwoSided(vs[0], vs[1]);
                    ;

                    fd.shape = shape;

                    bodies[0].CreateFixture(fd);
                }
                {
                    FixtureDef fd = new FixtureDef();
                    fd.friction            = 2.000000029802322e-01f;
                    fd.restitution         = 0.000000000000000e+00f;
                    fd.density             = 0.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = (1);
                    fd.filter.maskBits     = (65535);
                    fd.filter.groupIndex   = (0);
                    EdgeShape shape = new EdgeShape();
                    Vector2[] vs    = new Vector2[4];
                    ;
                    vs[0] = new Vector2(1.560352897644043e+01f, 1.560027003288269e+00f);
                    vs[1] = new Vector2(1.428396320343018e+01f, 1.043329477310181e+00f);

                    vs[2] = new Vector2(4.565430396770254e-41f, 0.000000000000000e+00f);
                    vs[3] = new Vector2(0.000000000000000e+00f, 1.216642260551453e-01f);

                    shape.SetTwoSided(vs[0], vs[1]);
                    ;

                    fd.shape = shape;

                    bodies[0].CreateFixture(fd);
                }
                {
                    FixtureDef fd = new FixtureDef();
                    fd.friction            = 2.000000029802322e-01f;
                    fd.restitution         = 0.000000000000000e+00f;
                    fd.density             = 0.000000000000000e+00f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = (1);
                    fd.filter.maskBits     = (65535);
                    fd.filter.groupIndex   = (0);
                    EdgeShape shape = new EdgeShape();
                    Vector2[] vs    = new Vector2[4];
                    ;
                    vs[0] = new Vector2(2.115560913085938e+01f, 4.700156211853027e+00f);
                    vs[1] = new Vector2(2.044397354125977e+01f, 4.096230506896973e+00f);

                    vs[2] = new Vector2(4.565430396770254e-41f, 0.000000000000000e+00f);
                    vs[3] = new Vector2(0.000000000000000e+00f, 1.216642260551453e-01f);

                    shape.SetTwoSided(vs[0], vs[1]);
                    ;

                    fd.shape = shape;

                    bodies[0].CreateFixture(fd);
                }
            }
            {
                BodyDef bd = new BodyDef();
                bd.type            = BodyType.Dynamic;
                bd.position        = new Vector2(2.079453659057617e+01f, 7.627632617950439e+00f);
                bd.angle           = 4.814267158508301e-03f;
                bd.linearVelocity  = new Vector2(0.000000000000000e+00f, 0.000000000000000e+00f);
                bd.angularVelocity = 0.000000000000000e+00f;
                bd.linearDamping   = 0.000000000000000e+00f;
                bd.angularDamping  = 0.000000000000000e+00f;
                bd.allowSleep      = true;
                bd.awake           = true;
                bd.fixedRotation   = false;
                bd.bullet          = true;
                //bd.active          = true;
                bd.gravityScale = 1.000000000000000e+00f;
                bodies[1]       = world.CreateBody(bd);

                {
                    FixtureDef fd = new FixtureDef();
                    fd.friction            = 5.000000000000000e-01f;
                    fd.restitution         = 6.999999880790710e-01f;
                    fd.density             = 5.000000074505806e-02f;
                    fd.isSensor            = false;
                    fd.filter.categoryBits = (1);
                    fd.filter.maskBits     = (65535);
                    fd.filter.groupIndex   = (0);
                    PolygonShape shape = new PolygonShape();
                    Vector2[]    vs    = new Vector2[4];
                    vs[0] = new Vector2(1.193307399749756e+00f, -7.795047014951706e-02f);
                    vs[1] = new Vector2(1.193307399749756e+00f, 9.368468075990677e-02f);
                    vs[2] = new Vector2(-1.108393430709839e+00f, 1.137156039476395e-01f);
                    vs[3] = new Vector2(-1.108393430709839e+00f, -9.785395860671997e-02f);
                    shape.Set(vs);

                    fd.shape = shape;

                    bodies[1].CreateFixture(fd);
                }
            }

            return(world);
        }
示例#13
0
        EdgeShapesTest()
        {
            // Ground body
            {
                Body ground = BodyFactory.CreateBody(World);

                double x1 = -20.0f;
                double y1 = 2.0f * (double)Math.Cos(x1 / 10.0f * (double)Math.PI);
                for (int i = 0; i < 80; ++i)
                {
                    double x2 = x1 + 0.5f;
                    double y2 = 2.0f * (double)Math.Cos(x2 / 10.0f * (double)Math.PI);

                    EdgeShape shape = new EdgeShape(new Vector2(x1, y1), new Vector2(x2, y2));
                    ground.CreateFixture(shape);

                    x1 = x2;
                    y1 = y2;
                }
            }

            {
                Vertices vertices = new Vertices(3);
                vertices.Add(new Vector2(-0.5f, 0.0f));
                vertices.Add(new Vector2(0.5f, 0.0f));
                vertices.Add(new Vector2(0.0f, 1.5f));
                _polygons[0] = new PolygonShape(vertices, 20);
            }

            {
                Vertices vertices = new Vertices(3);
                vertices.Add(new Vector2(-0.1f, 0.0f));
                vertices.Add(new Vector2(0.1f, 0.0f));
                vertices.Add(new Vector2(0.0f, 1.5f));
                _polygons[1] = new PolygonShape(vertices, 20);
            }

            {
                const double w = 1.0f;
                double       b = w / (2.0f + (double)Math.Sqrt(2.0f));
                double       s = (double)Math.Sqrt(2.0f) * b;

                Vertices vertices = new Vertices(8);
                vertices.Add(new Vector2(0.5f * s, 0.0f));
                vertices.Add(new Vector2(0.5f * w, b));
                vertices.Add(new Vector2(0.5f * w, b + s));
                vertices.Add(new Vector2(0.5f * s, w));
                vertices.Add(new Vector2(-0.5f * s, w));
                vertices.Add(new Vector2(-0.5f * w, b + s));
                vertices.Add(new Vector2(-0.5f * w, b));
                vertices.Add(new Vector2(-0.5f * s, 0.0f));
                _polygons[2] = new PolygonShape(vertices, 20);
            }

            {
                _polygons[3]          = new PolygonShape(20);
                _polygons[3].Vertices = PolygonTools.CreateRectangle(0.5f, 0.5f);
            }

            {
                _circle = new CircleShape(0.5f, 1);
            }

            _bodyIndex = 0;
            _angle     = 0.0f;
        }
示例#14
0
        public RayCast()
        {
            // Ground body
            {
                BodyDef bd     = new BodyDef();
                Body    ground = m_world.CreateBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                shape.Density = 0;
                ground.CreateFixture(shape);
            }

            {
                Vec2[] vertices = new Vec2[3];
                vertices[0].Set(-0.5f, 0.0f);
                vertices[1].Set(0.5f, 0.0f);
                vertices[2].Set(0.0f, 1.5f);
                m_polygons[0].Set(vertices, 3);
            }

            {
                Vec2[] vertices = new Vec2[3];
                vertices[0].Set(-0.1f, 0.0f);
                vertices[1].Set(0.1f, 0.0f);
                vertices[2].Set(0.0f, 1.5f);
                m_polygons[1].Set(vertices, 3);
            }

            {
                float w = 1.0f;
                float b = w / (2.0f + (float)Math.Sqrt(2.0f));
                float s = (float)Math.Sqrt(2.0f) * b;

                Vec2[] vertices = new Vec2[8];
                vertices[0].Set(0.5f * s, 0.0f);
                vertices[1].Set(0.5f * w, b);
                vertices[2].Set(0.5f * w, b + s);
                vertices[3].Set(0.5f * s, w);
                vertices[4].Set(-0.5f * s, w);
                vertices[5].Set(-0.5f * w, b + s);
                vertices[6].Set(-0.5f * w, b);
                vertices[7].Set(-0.5f * s, 0.0f);

                m_polygons[2].Set(vertices, 8);
            }

            {
                m_polygons[3].SetAsBox(0.5f, 0.5f);
            }

            {
                m_circle.m_radius = 0.5f;
            }

            m_bodyIndex = 0;

            Array.Clear(m_bodies, 0, m_bodies.Length);

            m_angle = 0.0f;

            m_mode = Mode.e_closest;
        }
示例#15
0
        public static IEdge SetShape(this IEdge myIEdge, EdgeShape myEdgeShape)
        {
            if (myIEdge == null)
                throw new ArgumentNullException("myIEdge must not be null!");

            myIEdge.Shape = myEdgeShape;

            return myIEdge;
        }
        public override BodyContainer Process(List <RawBodyTemplate> input, ContentProcessorContext context)
        {
            if (ScaleFactor < 1)
            {
                throw new Exception("Pixel to meter ratio must be greater than zero.");
            }

            if (BezierIterations < 1)
            {
                throw new Exception("Cubic bézier iterations must be greater than zero.");
            }

            Matrix        matScale = Matrix.CreateScale(_scaleFactor, _scaleFactor, 1f);
            SVGPathParser parser   = new SVGPathParser(_bezierIterations);
            BodyContainer bodies   = new BodyContainer();

            foreach (RawBodyTemplate rawBody in input)
            {
                if (rawBody.Name == "importer_default_path_container")
                {
                    continue;
                }

                BodyTemplate currentBody = new BodyTemplate();
                currentBody.Mass     = rawBody.Mass;
                currentBody.BodyType = rawBody.BodyType;
                foreach (RawFixtureTemplate rawFixture in rawBody.Fixtures)
                {
                    List <Polygon> paths = parser.ParseSVGPath(rawFixture.Path, rawFixture.Transformation * matScale);
                    for (int i = 0; i < paths.Count; i++)
                    {
                        if (paths[i].Closed)
                        {
                            List <Vertices> partition = Triangulate.ConvexPartition(paths[i].Vertices, TriangulationAlgorithm.Bayazit);
                            foreach (Vertices v in partition)
                            {
                                currentBody.Fixtures.Add(new FixtureTemplate()
                                {
                                    Shape       = new PolygonShape(v, rawFixture.Density),
                                    Restitution = rawFixture.Restitution,
                                    Friction    = rawFixture.Friction,
                                    Name        = rawFixture.Name
                                });
                            }
                        }
                        else
                        {
                            Shape shape;
                            if (paths[i].Vertices.Count > 2)
                            {
                                shape = new ChainShape(paths[i].Vertices);
                            }
                            else
                            {
                                shape = new EdgeShape(paths[i].Vertices[0], paths[i].Vertices[1]);
                            }
                            currentBody.Fixtures.Add(new FixtureTemplate()
                            {
                                Shape       = shape,
                                Restitution = rawFixture.Restitution,
                                Friction    = rawFixture.Friction,
                                Name        = rawFixture.Name
                            });
                        }
                    }
                }
                if (currentBody.Fixtures.Count > 0)
                {
                    bodies[rawBody.Name] = currentBody;
                    currentBody          = null;
                }
            }
            return(bodies);
        }
示例#17
0
        public SliderCrank()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                shape.Density = 0;
                ground.CreateFixture(shape);
            }

            {
                Body prevBody = ground;

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

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType._dynamicBody;
                    bd.Position.Set(0.0f, 7.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape);

                    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);
                    shape.Density = 2;

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType._dynamicBody;
                    bd.Position.Set(0.0f, 13.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape);

                    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);
                    shape.Density = 2;

                    BodyDef bd = new BodyDef();
                    bd.type          = BodyType._dynamicBody;
                    bd.fixedRotation = true;
                    bd.Position.Set(0.0f, 17.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape);

                    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.type = BodyType._dynamicBody;
                    bd.Position.Set(0.0f, 23.0f);
                    Body body = m_world.CreateBody(bd);
                    shape.Density = 2;
                    body.CreateFixture(shape);
                }
            }
        }
示例#18
0
 /// <summary>
 ///     Compute contact points for edge versus circle.
 ///     This accounts for edge connectivity.
 ///     计算边缘和圆的碰撞点
 /// </summary>
 /// <param name="manifold"></param>
 /// <param name="edgeA"></param>
 /// <param name="xfA"></param>
 /// <param name="circleB"></param>
 /// <param name="xfB"></param>
 public static void CollideEdgeAndCircle(
     ref Manifold manifold,
     EdgeShape edgeA,
     in Transform xfA,
示例#19
0
 public Edge setShape(EdgeShape shape)
 {
     this.shape = shape;
     return(this);
 }
示例#20
0
        public async Task TestBoxStack()
        {
            var server = StartServer();
            await server.WaitIdleAsync();

            var   mapManager          = server.ResolveDependency <IMapManager>();
            var   entitySystemManager = server.ResolveDependency <IEntitySystemManager>();
            var   physicsSystem       = entitySystemManager.GetEntitySystem <SharedPhysicsSystem>();
            MapId mapId;

            var columnCount = 1;
            var rowCount    = 15;

            PhysicsComponent[] bodies   = new PhysicsComponent[columnCount * rowCount];
            Vector2            firstPos = Vector2.Zero;

            await server.WaitPost(() =>
            {
                mapId = mapManager.CreateMap();
                physicsSystem.Maps[mapId].Gravity = new Vector2(0, -9.8f);

                var entityManager = IoCManager.Resolve <IEntityManager>();

                // TODO: Need a blank entity we can spawn for testbed.
                var ground = entityManager.SpawnEntity("BlankEntity", new MapCoordinates(0, 0, mapId)).AddComponent <PhysicsComponent>();

                var horizontal        = new EdgeShape(new Vector2(-20, 0), new Vector2(20, 0));
                var horizontalFixture = new Fixture(ground, horizontal)
                {
                    CollisionLayer = 1,
                    CollisionMask  = 1,
                    Hard           = true
                };
                ground.AddFixture(horizontalFixture);

                var vertical        = new EdgeShape(new Vector2(10, 0), new Vector2(10, 10));
                var verticalFixture = new Fixture(ground, vertical)
                {
                    CollisionLayer = 1,
                    CollisionMask  = 1,
                    Hard           = true
                };
                ground.AddFixture(verticalFixture);

                var xs = new[]
                {
                    0.0f, -10.0f, -5.0f, 5.0f, 10.0f
                };

                PolygonShape shape;

                for (var j = 0; j < columnCount; j++)
                {
                    for (var i = 0; i < rowCount; i++)
                    {
                        var x = 0.0f;

                        var box = entityManager.SpawnEntity("BlankEntity",
                                                            new MapCoordinates(new Vector2(xs[j] + x, 0.55f + 2.1f * i), mapId)).AddComponent <PhysicsComponent>();

                        box.BodyType        = BodyType.Dynamic;
                        box.SleepingAllowed = false;
                        shape = new PolygonShape(0.001f)
                        {
                            Vertices = new List <Vector2>()
                            {
                                new(0.5f, -0.5f),
                                new(0.5f, 0.5f),
                                new(-0.5f, 0.5f),
                                new(-0.5f, -0.5f),
                            }
                        };
                        box.FixedRotation = true;
                        // TODO: Need to detect shape and work out if we need to use fixedrotation

                        var fixture = new Fixture(box, shape)
                        {
                            CollisionMask  = 1,
                            CollisionLayer = 1,
                            Hard           = true,
                        };
                        box.AddFixture(fixture);

                        bodies[j *rowCount + i] = box;
                    }
                }

                firstPos = bodies[0].Owner.Transform.WorldPosition;
            });

            await server.WaitRunTicks(1);

            // Check that gravity workin
            await server.WaitAssertion(() =>
            {
                Assert.That(firstPos != bodies[0].Owner.Transform.WorldPosition);
            });

            // Assert

            await server.WaitRunTicks(150);

            // Assert settled, none below 0, etc.
            await server.WaitAssertion(() =>
            {
                for (var j = 0; j < columnCount; j++)
                {
                    for (var i = 0; i < bodies.Length; i++)
                    {
                        var body     = bodies[j *columnCount + i];
                        var worldPos = body.Owner.Transform.WorldPosition;

                        // TODO: Multi-column support but I cbf right now
                        // Can't be more exact as some level of sinking is allowed.
                        Assert.That(worldPos.EqualsApprox(new Vector2(0.0f, i + 0.5f), 0.1f), $"Expected y-value of {i + 0.5f} but found {worldPos.Y}");
                    }
                }
            });
        }
示例#21
0
        public async Task TestBoxStack()
        {
            var server = StartServer();
            await server.WaitIdleAsync();

            var   entityManager       = server.ResolveDependency <IEntityManager>();
            var   mapManager          = server.ResolveDependency <IMapManager>();
            var   entitySystemManager = server.ResolveDependency <IEntitySystemManager>();
            var   fixtureSystem       = entitySystemManager.GetEntitySystem <FixtureSystem>();
            MapId mapId;

            const int columnCount = 1;
            const int rowCount    = 15;

            PhysicsComponent[] bodies   = new PhysicsComponent[columnCount * rowCount];
            Vector2            firstPos = Vector2.Zero;

            await server.WaitPost(() =>
            {
                mapId = mapManager.CreateMap();

                EntityUid tempQualifier2 = mapManager.GetMapEntityId(mapId);
                IoCManager.Resolve <IEntityManager>().GetComponent <SharedPhysicsMapComponent>(tempQualifier2).Gravity = new Vector2(0, -9.8f);

                EntityUid tempQualifier = entityManager.SpawnEntity(null, new MapCoordinates(0, 0, mapId));
                var ground = IoCManager.Resolve <IEntityManager>().AddComponent <PhysicsComponent>(tempQualifier);

                var horizontal        = new EdgeShape(new Vector2(-20, 0), new Vector2(20, 0));
                var horizontalFixture = new Fixture(ground, horizontal)
                {
                    CollisionLayer = 1,
                    CollisionMask  = 1,
                    Hard           = true
                };

                fixtureSystem.CreateFixture(ground, horizontalFixture);

                var vertical        = new EdgeShape(new Vector2(10, 0), new Vector2(10, 10));
                var verticalFixture = new Fixture(ground, vertical)
                {
                    CollisionLayer = 1,
                    CollisionMask  = 1,
                    Hard           = true
                };

                fixtureSystem.CreateFixture(ground, verticalFixture);

                var xs = new[]
                {
                    0.0f, -10.0f, -5.0f, 5.0f, 10.0f
                };

                for (var j = 0; j < columnCount; j++)
                {
                    for (var i = 0; i < rowCount; i++)
                    {
                        var x = 0.0f;

                        EntityUid tempQualifier1 = entityManager.SpawnEntity(null,
                                                                             new MapCoordinates(new Vector2(xs[j] + x, 0.55f + 2.1f * i), mapId));
                        var box = IoCManager.Resolve <IEntityManager>().AddComponent <PhysicsComponent>(tempQualifier1);

                        box.BodyType = BodyType.Dynamic;
                        var poly     = new PolygonShape(0.001f);
                        poly.SetVertices(new List <Vector2>()
                        {
                            new(0.5f, -0.5f),
                            new(0.5f, 0.5f),
                            new(-0.5f, 0.5f),
                            new(-0.5f, -0.5f),
                        });

                        var fixture = new Fixture(box, poly)
                        {
                            CollisionMask  = 1,
                            CollisionLayer = 1,
                            Hard           = true,
                        };

                        fixtureSystem.CreateFixture(box, fixture);

                        bodies[j *rowCount + i] = box;
                    }
        private CharacterCollisionTest()
        {
            //Ground body
            Body ground = BodyFactory.CreateEdge(World, new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));

            // Collinear edges with no adjacency information.
            // This shows the problematic case where a box shape can hit
            // an internal vertex.
            EdgeShape shape = new EdgeShape(new Vector2(-8.0f, 1.0f), new Vector2(-6.0f, 1.0f));

            ground.CreateFixture(shape);
            shape = new EdgeShape(new Vector2(-6.0f, 1.0f), new Vector2(-4.0f, 1.0f));
            ground.CreateFixture(shape);
            shape = new EdgeShape(new Vector2(-4.0f, 1.0f), new Vector2(-2.0f, 1.0f));
            ground.CreateFixture(shape);

            // Chain shape
            {
                Vertices vs = new Vertices(4);
                vs.Add(new Vector2(5.0f, 7.0f));
                vs.Add(new Vector2(6.0f, 8.0f));
                vs.Add(new Vector2(7.0f, 8.0f));
                vs.Add(new Vector2(8.0f, 7.0f));

                Body body = BodyFactory.CreateChainShape(World, vs);
                body.Rotation = 0.25f * MathConstants.Pi;
            }

            // Square tiles. This shows that adjacency shapes may
            // have non-smooth collision. There is no solution
            // to this problem.
            PolygonShape tile = new PolygonShape(1);

            tile.Vertices = PolygonUtils.CreateRectangle(1.0f, 1.0f, new Vector2(4.0f, 3.0f), 0.0f);
            ground.CreateFixture(tile);
            tile.Vertices = PolygonUtils.CreateRectangle(1.0f, 1.0f, new Vector2(6.0f, 3.0f), 0.0f);
            ground.CreateFixture(tile);
            tile.Vertices = PolygonUtils.CreateRectangle(1.0f, 1.0f, new Vector2(8.0f, 3.0f), 0.0f);
            ground.CreateFixture(tile);

            // Square made from an edge loop. Collision should be smooth.
            Vertices vertices = new Vertices(4);

            vertices.Add(new Vector2(-1.0f, 3.0f));
            vertices.Add(new Vector2(1.0f, 3.0f));
            vertices.Add(new Vector2(1.0f, 5.0f));
            vertices.Add(new Vector2(-1.0f, 5.0f));
            FixtureFactory.AttachLoopShape(vertices, ground);

            // Edge loop. Collision should be smooth.
            vertices = new Vertices(10);
            vertices.Add(new Vector2(0.0f, 0.0f));
            vertices.Add(new Vector2(6.0f, 0.0f));
            vertices.Add(new Vector2(6.0f, 2.0f));
            vertices.Add(new Vector2(4.0f, 1.0f));
            vertices.Add(new Vector2(2.0f, 2.0f));
            vertices.Add(new Vector2(0.0f, 2.0f));
            vertices.Add(new Vector2(-2.0f, 2.0f));
            vertices.Add(new Vector2(-4.0f, 3.0f));
            vertices.Add(new Vector2(-6.0f, 2.0f));
            vertices.Add(new Vector2(-6.0f, 0.0f));
            BodyFactory.CreateLoopShape(World, vertices, new Vector2(-10, 4));

            // Square character 1
            Body squareCharacter = BodyFactory.CreateRectangle(World, 1, 1, 20, new Vector2(-3.0f, 8.0f));

            squareCharacter.BodyType        = BodyType.Dynamic;
            squareCharacter.FixedRotation   = true;
            squareCharacter.SleepingAllowed = false;

            squareCharacter.OnCollision  += CharacterOnCollision;
            squareCharacter.OnSeparation += CharacterOnSeparation;

            // Square character 2
            Body squareCharacter2 = BodyFactory.CreateRectangle(World, 0.5f, 0.5f, 20, new Vector2(-5.0f, 5.0f));

            squareCharacter2.BodyType        = BodyType.Dynamic;
            squareCharacter2.FixedRotation   = true;
            squareCharacter2.SleepingAllowed = false;

            // Hexagon character
            float       angle = 0.0f;
            const float delta = MathConstants.Pi / 3.0f;

            vertices = new Vertices(6);

            for (int i = 0; i < 6; ++i)
            {
                vertices.Add(new Vector2(0.5f * (float)Math.Cos(angle), 0.5f * (float)Math.Sin(angle)));
                angle += delta;
            }

            Body hexCharacter = BodyFactory.CreatePolygon(World, vertices, 20, new Vector2(-5.0f, 8.0f));

            hexCharacter.BodyType        = BodyType.Dynamic;
            hexCharacter.FixedRotation   = true;
            hexCharacter.SleepingAllowed = false;

            // Circle character
            Body circleCharacter = BodyFactory.CreateCircle(World, 0.5f, 20, new Vector2(3.0f, 5.0f));

            circleCharacter.BodyType        = BodyType.Dynamic;
            circleCharacter.FixedRotation   = true;
            circleCharacter.SleepingAllowed = false;

            // Circle character
            _character                 = BodyFactory.CreateCircle(World, 0.25f, 20, new Vector2(-7.0f, 6.0f));
            _character.BodyType        = BodyType.Dynamic;
            _character.Friction        = 1.0f;
            _character.SleepingAllowed = false;
        }
示例#23
0
        private WebTest()
        {
            Body ground;
            {
                BodyDef bd = new BodyDef();
                ground = BodyFactory.CreateFromDef(World, bd);

                EdgeShape shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.AddFixture(shape);
            }

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

                BodyDef bd = new BodyDef();
                bd.Type = BodyType.Dynamic;

                bd.Position = new Vector2(-5.0f, 5.0f);
                _bodies[0]  = BodyFactory.CreateFromDef(World, bd);
                _bodies[0].AddFixture(shape);

                bd.Position = new Vector2(5.0f, 5.0f);
                _bodies[1]  = BodyFactory.CreateFromDef(World, bd);
                _bodies[1].AddFixture(shape);

                bd.Position = new Vector2(5.0f, 15.0f);
                _bodies[2]  = BodyFactory.CreateFromDef(World, bd);
                _bodies[2].AddFixture(shape);

                bd.Position = new Vector2(-5.0f, 15.0f);
                _bodies[3]  = BodyFactory.CreateFromDef(World, bd);
                _bodies[3].AddFixture(shape);

                DistanceJointDef jd = new DistanceJointDef();
                Vector2          p1, p2, d;

                float frequencyHz  = 2.0f;
                float dampingRatio = 0.0f;

                jd.BodyA        = ground;
                jd.BodyB        = _bodies[0];
                jd.LocalAnchorA = new Vector2(-10.0f, 0.0f);
                jd.LocalAnchorB = new Vector2(-0.5f, -0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out float stiffness, out float damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[0]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = ground;
                jd.BodyB        = _bodies[1];
                jd.LocalAnchorA = new Vector2(10.0f, 0.0f);
                jd.LocalAnchorB = new Vector2(0.5f, -0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[1]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = ground;
                jd.BodyB        = _bodies[2];
                jd.LocalAnchorA = new Vector2(10.0f, 20.0f);
                jd.LocalAnchorB = new Vector2(0.5f, 0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[2]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = ground;
                jd.BodyB        = _bodies[3];
                jd.LocalAnchorA = new Vector2(-10.0f, 20.0f);
                jd.LocalAnchorB = new Vector2(-0.5f, 0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[3]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = _bodies[0];
                jd.BodyB        = _bodies[1];
                jd.LocalAnchorA = new Vector2(0.5f, 0.0f);
                jd.LocalAnchorB = new Vector2(-0.5f, 0.0f);

                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[4]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = _bodies[1];
                jd.BodyB        = _bodies[2];
                jd.LocalAnchorA = new Vector2(0.0f, 0.5f);
                jd.LocalAnchorB = new Vector2(0.0f, -0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[5]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = _bodies[2];
                jd.BodyB        = _bodies[3];
                jd.LocalAnchorA = new Vector2(-0.5f, 0.0f);
                jd.LocalAnchorB = new Vector2(0.5f, 0.0f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[6]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = _bodies[3];
                jd.BodyB        = _bodies[0];
                jd.LocalAnchorA = new Vector2(0.0f, -0.5f);
                jd.LocalAnchorB = new Vector2(0.0f, 0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[7]   = JointFactory.CreateFromDef(World, jd);
            }
        }
示例#24
0
        public Revolute()
        {
            Body ground;
            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

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

                //fd.filter.categoryBits = 2;

                ground.CreateFixture(fd);
            }

            {
                var shape = new CircleShape();
                shape.Radius = 0.5f;

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;

                var rjd = new RevoluteJointDef();

                bd.Position.Set(-10.0f, 20.0f);
                var body = World.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                var w = 100.0f;
                body.SetAngularVelocity(w);
                body.SetLinearVelocity(new Vector2(-8.0f * w, 0.0f));

                rjd.Initialize(ground, body, new Vector2(-10.0f, 12.0f));
                rjd.MotorSpeed       = 1.0f * Settings.Pi;
                rjd.MaxMotorTorque   = 10000.0f;
                rjd.EnableMotor      = false;
                rjd.LowerAngle       = -0.25f * Settings.Pi;
                rjd.UpperAngle       = 0.5f * Settings.Pi;
                rjd.EnableLimit      = true;
                rjd.CollideConnected = true;

                m_joint = (RevoluteJoint)World.CreateJoint(rjd);
            }

            {
                var circle_shape = new CircleShape();
                circle_shape.Radius = 3.0f;

                var circle_bd = new BodyDef();
                circle_bd.BodyType = BodyType.DynamicBody;
                circle_bd.Position.Set(5.0f, 30.0f);

                var fd = new FixtureDef();
                fd.Density = 5.0f;
                var filter = fd.Filter;
                filter.MaskBits = 1;
                fd.Filter       = filter;
                fd.Shape        = circle_shape;

                m_ball = World.CreateBody(circle_bd);
                m_ball.CreateFixture(fd);

                var polygon_shape = new PolygonShape();
                polygon_shape.SetAsBox(10.0f, 0.2f, new Vector2(-10.0f, 0.0f), 0.0f);

                var polygon_bd = new BodyDef();
                polygon_bd.Position.Set(20.0f, 10.0f);
                polygon_bd.BodyType = BodyType.DynamicBody;
                polygon_bd.Bullet   = true;
                var polygon_body = World.CreateBody(polygon_bd);
                polygon_body.CreateFixture(polygon_shape, 2.0f);

                var rjd = new RevoluteJointDef();
                rjd.Initialize(ground, polygon_body, new Vector2(20.0f, 10.0f));
                rjd.LowerAngle  = -0.25f * Settings.Pi;
                rjd.UpperAngle  = 0.0f * Settings.Pi;
                rjd.EnableLimit = true;
                World.CreateJoint(rjd);
            }

            // Tests mass computation of a small object far from the origin
            {
                var bodyDef = new BodyDef();
                bodyDef.BodyType = BodyType.DynamicBody;
                var body = World.CreateBody(bodyDef);

                var polyShape = new PolygonShape();
                var verts     = new Vector2[3];
                verts[0].Set(17.63f, 36.31f);
                verts[1].Set(17.52f, 36.69f);
                verts[2].Set(17.19f, 36.36f);
                polyShape.Set(verts);

                var polyFixtureDef = new FixtureDef();
                polyFixtureDef.Shape   = polyShape;
                polyFixtureDef.Density = 1;

                body.CreateFixture(polyFixtureDef); //assertion hits inside here
            }
        }
示例#25
0
        public override void Draw(GameTime gameTime)
        {
            //position(body.GetPosition().X * game.physics_scale, body.GetPosition().Y * game.physics_scale);

            Vector2 light_origin     = new Vector2(x, y);
            float   current_rotation = body.GetAngle();

            while (current_rotation < 0)
            {
                current_rotation += (float)Math.PI * 2;
            }
            current_rotation = current_rotation % ((float)Math.PI * 2);

            //thing!
            rotation = current_rotation;

            List <Vector2> testPoints = new List <Vector2>();
            SortedList <float, Vector2> intersectionPoints = new SortedList <float, Vector2>();

            //firstly, cast out 9 rays around the circle/cone; this will define the base shape of this light
            //and ensure that lack of collision targets doesn't lead to rendering holes
            for (int i = -rays_to_cast / 2; i <= rays_to_cast / 2; i++)
            {
                Vector2 target = Vector2.Transform(new Vector2(0, -ray_length), Matrix.CreateRotationZ((light_spread_angle / rays_to_cast) * i + current_rotation)) + light_origin;
                testPoints.Add(target);
            }

            //Gather a list of all nearby fixtures; we'll use these to generate test points and cast rays
            AABB aabb;

            aabb.lowerBound = body.GetPosition() - new Vector2(ray_length, ray_length);
            aabb.upperBound = body.GetPosition() + new Vector2(ray_length, ray_length);
            overlapping_fixtures.Clear();
            game.world.QueryAABB(QueryCallback, ref aabb);

            //Console.WriteLine(overlapping_fixtures.Count());

            //Gather a list of all test points in the scene
            foreach (Fixture f in overlapping_fixtures)
            {
                Type shapeType = f.GetType();

                if (f.GetShape() is PolygonShape)
                {
                    PolygonShape polygon = (PolygonShape)f.GetShape();

                    for (int curVert = 0; curVert < polygon.GetVertexCount(); curVert++)
                    {
                        //transform this point based on the body transforms
                        Vector2 target = Vector2.Transform(polygon.GetVertex(curVert), Matrix.CreateRotationZ(f.GetBody().GetAngle())) + f.GetBody().GetPosition();

                        if (Vector2.DistanceSquared(light_origin, target) <= ray_length * ray_length * 2)
                        {
                            float ray_angle = (float)Math.Atan2((target - light_origin).Y, (target - light_origin).X) + (float)Math.PI / 2;
                            float light_min = current_rotation - light_spread_angle / 2;
                            float light_max = current_rotation + light_spread_angle / 2;

                            while (ray_angle > Math.PI)
                            {
                                ray_angle -= (float)Math.PI * 2;
                            }
                            while (ray_angle < light_min)
                            {
                                ray_angle += (float)Math.PI * 2;
                            }

                            if (ray_angle < light_max)
                            {
                                //Add it to the list for processing
                                testPoints.Add(target);
                            }
                        }
                    }
                }
                else if (f.GetShape() is EdgeShape)
                {
                    //Do the same thing, except for edge shapes

                    //only v1 and v2 will count here
                    EdgeShape line = (EdgeShape)f.GetShape();

                    Vector2 target1 = Vector2.Transform(line._vertex1, Matrix.CreateRotationZ(f.GetBody().GetAngle())) + f.GetBody().GetPosition();
                    if (Vector2.DistanceSquared(light_origin, target1) <= ray_length * ray_length * 2)
                    {
                        float ray_angle = (float)Math.Atan2((target1 - light_origin).Y, (target1 - light_origin).X) + (float)Math.PI / 2;
                        float light_min = current_rotation - light_spread_angle / 2;
                        float light_max = current_rotation + light_spread_angle / 2;

                        while (ray_angle > Math.PI)
                        {
                            ray_angle -= (float)Math.PI * 2;
                        }
                        while (ray_angle < light_min)
                        {
                            ray_angle += (float)Math.PI * 2;
                        }

                        if (ray_angle < light_max)
                        {
                            testPoints.Add(target1);
                        }
                    }

                    Vector2 target2 = Vector2.Transform(line._vertex2, Matrix.CreateRotationZ(f.GetBody().GetAngle())) + f.GetBody().GetPosition();
                    if (Vector2.DistanceSquared(light_origin, target2) <= ray_length * ray_length * 2)
                    {
                        float ray_angle = (float)Math.Atan2((target2 - light_origin).Y, (target2 - light_origin).X) + (float)Math.PI / 2;
                        float light_min = current_rotation - light_spread_angle / 2;
                        float light_max = current_rotation + light_spread_angle / 2;

                        while (ray_angle < light_min)
                        {
                            ray_angle += (float)Math.PI * 2;
                        }

                        if (ray_angle < light_max)
                        {
                            testPoints.Add(target2);
                        }
                    }
                }
            }

            //For every test point, calculate the closest intersection
            foreach (Vector2 point in testPoints)
            {
                Vector2 target = point;

                //normalize this point, then multiply it by the length; this will give us a
                //line originating from the light source, and cast "toward" this corner point
                //that is exactly the distance of the light's normal max range
                target -= light_origin;
                target.Normalize();
                target = target * ray_length;

                //cast two more rays at slight angle offsets, to deal with corner edge cases
                //(only 2, so everything after this is doubled, because loops are silly for only 2 elements)
                Vector2 target_neg = Vector2.Transform(target, Matrix.CreateRotationZ(-0.0001f));
                Vector2 target_pos = Vector2.Transform(target, Matrix.CreateRotationZ(0.0001f));

                target_neg += light_origin;
                target_pos += light_origin;

                //perform the ray cast, and figure out what to do about the result
                float closestFractionNeg = Math.Min(rayCast(light_origin, target_neg), 1f);
                float closestFractionPos = Math.Min(rayCast(light_origin, target_pos), 1f);

                Vector2 intersectPointPos = light_origin + closestFractionPos * (target_pos - light_origin);
                Vector2 intersectPointNeg = light_origin + closestFractionNeg * (target_neg - light_origin);

                if (game.input.DevMode)
                {
                    game.drawLine(light_origin, intersectPointNeg, (Color.White));
                    game.drawLine(light_origin, intersectPointPos, (Color.White));
                }

                intersectionPoints[(float)Math.Atan2((float)(intersectPointNeg - light_origin).Y, (float)(intersectPointNeg - light_origin).X)] = intersectPointNeg;
                intersectionPoints[(float)Math.Atan2((float)(intersectPointPos - light_origin).Y, (float)(intersectPointPos - light_origin).X)] = intersectPointPos;
            }

            //now, attempt (poorly) to create a triangle mesh based on our hopefully sorted points
            GraphicsDevice gd = game.GraphicsDevice;

            //VertexPositionColor[] vertex_list = new VertexPositionColor[_max_rays * 3];

            ArrayList vertex_list = new ArrayList();

            Color originColor  = sprite_color;
            Color outsideColor = Color.FromNonPremultiplied(sprite_color.R, sprite_color.G, sprite_color.B, 0);
            Color intersectionColor;

            bool first = true;

            foreach (var intersection in intersectionPoints)
            {
                intersectionColor = Color.Lerp(originColor, outsideColor, Math.Min(1.0f, ((intersection.Value - light_origin).Length() / (float)ray_length)));
                if (!first)
                {
                    //finish the last triangle
                    vertex_list.Add(new VertexPositionColor(new Vector3(intersection.Value.X, intersection.Value.Y, 0), intersectionColor));
                }
                //start a new triangle leading with this edge
                vertex_list.Add(new VertexPositionColor(new Vector3(light_origin.X, light_origin.Y, 0), sprite_color));
                vertex_list.Add(new VertexPositionColor(new Vector3(intersection.Value.X, intersection.Value.Y, 0), intersectionColor));

                first = false;
            }

            intersectionColor = Color.Lerp(originColor, outsideColor, Math.Min(1.0f, ((intersectionPoints.First().Value - light_origin).Length() / (float)ray_length)));

            //finish the very last triangle (it loops to the beginning)
            vertex_list.Add(new VertexPositionColor(new Vector3(intersectionPoints.First().Value.X, intersectionPoints.First().Value.Y, 0), intersectionColor));

            buffer.SetData <VertexPositionColor>((VertexPositionColor[])vertex_list.ToArray(typeof(VertexPositionColor)));
            gd.SetVertexBuffer(buffer);

            //make sure the light shader is set up
            light_shader.World = Matrix.CreateTranslation(new Vector3(-game.camera.X / 10, -game.camera.Y / 10, 0));
            light_shader.CurrentTechnique.Passes[0].Apply();

            //draw them primitives!
            gd.DrawPrimitives(PrimitiveType.TriangleList, 0, intersectionPoints.Count);
        }
示例#26
0
        public CollisionProcessing()
        {
            // Ground body
            {
                EdgeShape shape = new EdgeShape();
                shape.Set(new Vec2(-50.0f, 0.0f), new Vec2(50.0f, 0.0f));

                FixtureDef sd = new FixtureDef();
                sd.shape = shape;;

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

            float xLo = -5.0f, xHi = 5.0f;
            float yLo = 2.0f, yHi = 35.0f;

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

            PolygonShape polygon = new PolygonShape();

            polygon.Set(vertices, 3);

            FixtureDef triangleShapeDef = new FixtureDef();

            triangleShapeDef.shape   = polygon;
            triangleShapeDef.Density = 1.0f;

            BodyDef triangleBodyDef = new BodyDef();

            triangleBodyDef.type = BodyType._dynamicBody;
            triangleBodyDef.Position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));

            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.Set(vertices, 3);

            triangleBodyDef.Position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));

            Body body2 = m_world.CreateBody(triangleBodyDef);

            body2.CreateFixture(triangleShapeDef);

            // Small box
            polygon.SetAsBox(1.0f, 0.5f);

            FixtureDef boxShapeDef = new FixtureDef();

            boxShapeDef.shape   = polygon;
            boxShapeDef.Density = 1.0f;

            BodyDef boxBodyDef = new BodyDef();

            boxBodyDef.type = BodyType._dynamicBody;
            boxBodyDef.Position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));

            Body body3 = m_world.CreateBody(boxBodyDef);

            body3.CreateFixture(boxShapeDef);

            // Large box (recycle definitions)
            polygon.SetAsBox(2.0f, 1.0f);
            boxBodyDef.Position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));

            Body body4 = m_world.CreateBody(boxBodyDef);

            body4.CreateFixture(boxShapeDef);

            // Small circle
            CircleShape circle = new CircleShape();

            circle.m_radius = 1.0f;

            FixtureDef circleShapeDef = new FixtureDef();

            circleShapeDef.shape   = circle;
            circleShapeDef.Density = 1.0f;

            BodyDef circleBodyDef = new BodyDef();

            circleBodyDef.type = BodyType._dynamicBody;
            circleBodyDef.Position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));

            Body body5 = m_world.CreateBody(circleBodyDef);

            body5.CreateFixture(circleShapeDef);

            // Large circle
            circle.m_radius *= 2.0f;
            circleBodyDef.Position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi));

            Body body6 = m_world.CreateBody(circleBodyDef);

            body6.CreateFixture(circleShapeDef);
        }
示例#27
0
        public SliderCrank()
        {
            Body ground;
            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                var prevBody = ground;

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

                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position = new Vector2(0.0f, 7.0f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    var rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(0.0f, 5.0f));
                    rjd.MotorSpeed     = 1.0f * Settings.Pi;
                    rjd.MaxMotorTorque = 10000.0f;
                    rjd.EnableMotor    = true;
                    _joint1            = (RevoluteJoint)World.CreateJoint(rjd);

                    prevBody = body;
                }

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

                    var bd = new BodyDef {
                        BodyType = BodyType.DynamicBody, Position = new Vector2(0.0f, 13.0f)
                    };
                    var body = World.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    var rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(0.0f, 9.0f));
                    rjd.EnableMotor = false;
                    World.CreateJoint(rjd);

                    prevBody = body;
                }

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

                    var bd = new BodyDef
                    {
                        BodyType = BodyType.DynamicBody, FixedRotation = true,
                        Position = new Vector2(0.0f, 17.0f)
                    };
                    var body = World.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    var rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(0.0f, 17.0f));
                    World.CreateJoint(rjd);

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

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

                    _joint2 = (PrismaticJoint)World.CreateJoint(pjd);
                }

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

                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position = new Vector2(0.0f, 23.0f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);
                }
            }
        }
示例#28
0
        public static Fixture AttachEdge(Vector2 start, Vector2 end, Body body, object userData)
        {
            EdgeShape edgeShape = new EdgeShape(start, end);

            return(body.CreateFixture(edgeShape, userData));
        }
示例#29
0
        public Web()
        {
            Body ground;
            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.Set(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

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

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;

                bd.Position.Set(-5.0f, 5.0f);
                _bodies[0] = World.CreateBody(bd);
                _bodies[0].CreateFixture(shape, 5.0f);

                bd.Position.Set(5.0f, 5.0f);
                _bodies[1] = World.CreateBody(bd);
                _bodies[1].CreateFixture(shape, 5.0f);

                bd.Position.Set(5.0f, 15.0f);
                _bodies[2] = World.CreateBody(bd);
                _bodies[2].CreateFixture(shape, 5.0f);

                bd.Position.Set(-5.0f, 15.0f);
                _bodies[3] = World.CreateBody(bd);
                _bodies[3].CreateFixture(shape, 5.0f);

                var     jd = new DistanceJointDef();
                Vector2 p1;
                Vector2 p2;
                Vector2 d;

                jd.FrequencyHz  = 2.0f;
                jd.DampingRatio = 0.0f;

                jd.BodyA = ground;
                jd.BodyB = _bodies[0];
                jd.LocalAnchorA.Set(-10.0f, 0.0f);
                jd.LocalAnchorB.Set(-0.5f, -0.5f);
                p1         = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2         = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d          = p2 - p1;
                jd.Length  = d.Length();
                _joints[0] = World.CreateJoint(jd);

                jd.BodyA = ground;
                jd.BodyB = _bodies[1];
                jd.LocalAnchorA.Set(10.0f, 0.0f);
                jd.LocalAnchorB.Set(0.5f, -0.5f);
                p1         = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2         = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d          = p2 - p1;
                jd.Length  = d.Length();
                _joints[1] = World.CreateJoint(jd);

                jd.BodyA = ground;
                jd.BodyB = _bodies[2];
                jd.LocalAnchorA.Set(10.0f, 20.0f);
                jd.LocalAnchorB.Set(0.5f, 0.5f);
                p1         = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2         = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d          = p2 - p1;
                jd.Length  = d.Length();
                _joints[2] = World.CreateJoint(jd);

                jd.BodyA = ground;
                jd.BodyB = _bodies[3];
                jd.LocalAnchorA.Set(-10.0f, 20.0f);
                jd.LocalAnchorB.Set(-0.5f, 0.5f);
                p1         = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2         = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d          = p2 - p1;
                jd.Length  = d.Length();
                _joints[3] = World.CreateJoint(jd);

                jd.BodyA = _bodies[0];
                jd.BodyB = _bodies[1];
                jd.LocalAnchorA.Set(0.5f, 0.0f);
                jd.LocalAnchorB.Set(-0.5f, 0.0f);
                ;
                p1         = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2         = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d          = p2 - p1;
                jd.Length  = d.Length();
                _joints[4] = World.CreateJoint(jd);

                jd.BodyA = _bodies[1];
                jd.BodyB = _bodies[2];
                jd.LocalAnchorA.Set(0.0f, 0.5f);
                jd.LocalAnchorB.Set(0.0f, -0.5f);
                p1         = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2         = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d          = p2 - p1;
                jd.Length  = d.Length();
                _joints[5] = World.CreateJoint(jd);

                jd.BodyA = _bodies[2];
                jd.BodyB = _bodies[3];
                jd.LocalAnchorA.Set(-0.5f, 0.0f);
                jd.LocalAnchorB.Set(0.5f, 0.0f);
                p1         = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2         = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d          = p2 - p1;
                jd.Length  = d.Length();
                _joints[6] = World.CreateJoint(jd);

                jd.BodyA = _bodies[3];
                jd.BodyB = _bodies[0];
                jd.LocalAnchorA.Set(0.0f, -0.5f);
                jd.LocalAnchorB.Set(0.0f, 0.5f);
                p1         = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2         = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d          = p2 - p1;
                jd.Length  = d.Length();
                _joints[7] = World.CreateJoint(jd);
            }
        }
示例#30
0
        private ApplyForceTest()
        {
            World.Gravity = Vector2.Zero;

            const float 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);
            }

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

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

                PolygonShape poly1 = new PolygonShape(vertices, 4);

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

                vertices[0] = MathUtils.Mul(ref xf2, new Vector2(-1.0f, 0.0f));
                vertices[1] = MathUtils.Mul(ref xf2, new Vector2(1.0f, 0.0f));
                vertices[2] = 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        = Settings.Pi;
                _body.AngularDamping  = 5.0f;
                _body.LinearDamping   = 0.8f;
                _body.SleepingAllowed = true;

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

            {
                Vertices     box   = PolygonUtils.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 float gravity = 10.0f;
                    float       I       = body.Inertia;
                    float       mass    = body.Mass;

                    // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m)
                    float radius = (float)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);
                }
            }
        }
示例#31
0
        public CharacterCollision()
        {
            // Ground body
            {
                var bd     = new BodyDef();
                var ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.Set(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            // Collinear edges with no adjacency information.
            // This shows the problematic case where a box shape can hit
            // an internal vertex.
            {
                var bd     = new BodyDef();
                var ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.Set(new Vector2(-8.0f, 1.0f), new Vector2(-6.0f, 1.0f));
                ground.CreateFixture(shape, 0.0f);
                shape.Set(new Vector2(-6.0f, 1.0f), new Vector2(-4.0f, 1.0f));
                ground.CreateFixture(shape, 0.0f);
                shape.Set(new Vector2(-4.0f, 1.0f), new Vector2(-2.0f, 1.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            // Chain shape
            {
                var bd = new BodyDef {
                    Angle = 0.25f * Settings.Pi
                };
                var ground = World.CreateBody(bd);

                var vs = new Vector2[4]
                {
                    new Vector2(5.0f, 7.0f),
                    new Vector2(6.0f, 8.0f),
                    new Vector2(7.0f, 8.0f),
                    new Vector2(8.0f, 7.0f)
                };
                var shape = new ChainShape();
                shape.CreateChain(vs);
                ground.CreateFixture(shape, 0.0f);
            }

            // Square tiles. This shows that adjacency shapes may
            // have non-smooth collision. There is no solution
            // to this problem.
            {
                var bd     = new BodyDef();
                var ground = World.CreateBody(bd);

                var shape = new PolygonShape();
                shape.SetAsBox(1.0f, 1.0f, new Vector2(4.0f, 3.0f), 0.0f);
                ground.CreateFixture(shape, 0.0f);
                shape.SetAsBox(1.0f, 1.0f, new Vector2(6.0f, 3.0f), 0.0f);
                ground.CreateFixture(shape, 0.0f);
                shape.SetAsBox(1.0f, 1.0f, new Vector2(8.0f, 3.0f), 0.0f);
                ground.CreateFixture(shape, 0.0f);
            }

            // Square made from an edge loop. Collision should be smooth.
            {
                var bd     = new BodyDef();
                var ground = World.CreateBody(bd);

                var vs = new Vector2[4]
                {
                    new Vector2(-1.0f, 3.0f),
                    new Vector2(1.0f, 3.0f),
                    new Vector2(1.0f, 5.0f),
                    new Vector2(-1.0f, 5.0f)
                };

                var shape = new ChainShape();
                shape.CreateLoop(vs);
                ground.CreateFixture(shape, 0.0f);
            }

            // Edge loop. Collision should be smooth.
            {
                var bd = new BodyDef {
                    Position = new Vector2(-10.0f, 4.0f)
                };
                var ground = World.CreateBody(bd);

                var vs = new Vector2[10]
                {
                    new Vector2(0.0f, 0.0f),
                    new Vector2(6.0f, 0.0f),
                    new Vector2(6.0f, 2.0f),
                    new Vector2(4.0f, 1.0f),
                    new Vector2(2.0f, 2.0f),
                    new Vector2(0.0f, 2.0f),
                    new Vector2(-2.0f, 2.0f),
                    new Vector2(-4.0f, 3.0f),
                    new Vector2(-6.0f, 2.0f),
                    new Vector2(-6.0f, 0.0f)
                };
                var shape = new ChainShape();
                shape.CreateLoop(vs);
                ground.CreateFixture(shape, 0.0f);
            }

            // Square character 1
            {
                var bd = new BodyDef
                {
                    Position      = new Vector2(-3.0f, 8.0f),
                    BodyType      = BodyType.DynamicBody,
                    FixedRotation = true,
                    AllowSleep    = false
                };

                var body = World.CreateBody(bd);

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

                var fd = new FixtureDef {
                    Shape = shape, Density = 20.0f
                };
                body.CreateFixture(fd);
            }

            // Square character 2
            {
                var bd = new BodyDef
                {
                    Position      = new Vector2(-5.0f, 5.0f),
                    BodyType      = BodyType.DynamicBody,
                    FixedRotation = true,
                    AllowSleep    = false
                };

                var body = World.CreateBody(bd);

                var shape = new PolygonShape();
                shape.SetAsBox(0.25f, 0.25f);

                var fd = new FixtureDef {
                    Shape = shape, Density = 20.0f
                };
                body.CreateFixture(fd);
            }

            // Hexagon character
            {
                var bd = new BodyDef
                {
                    Position      = new Vector2(-5.0f, 8.0f),
                    BodyType      = BodyType.DynamicBody,
                    FixedRotation = true,
                    AllowSleep    = false
                };

                var body = World.CreateBody(bd);

                var         angle    = 0.0f;
                const float delta    = Settings.Pi / 3.0f;
                var         vertices = new Vector2[6];
                for (var i = 0; i < 6; ++i)
                {
                    vertices[i].Set(0.5f * (float)Math.Cos(angle), 0.5f * (float)Math.Sin(angle));
                    angle += delta;
                }

                var shape = new PolygonShape();
                shape.Set(vertices);

                var fd = new FixtureDef {
                    Shape = shape, Density = 20.0f
                };
                body.CreateFixture(fd);
            }

            // Circle character
            {
                var bd = new BodyDef
                {
                    Position      = new Vector2(3.0f, 5.0f),
                    BodyType      = BodyType.DynamicBody,
                    FixedRotation = true,
                    AllowSleep    = false
                };

                var body = World.CreateBody(bd);

                var shape = new CircleShape {
                    Radius = 0.5f
                };

                var fd = new FixtureDef {
                    Shape = shape, Density = 20.0f
                };
                body.CreateFixture(fd);
            }

            // Circle character
            {
                var bd = new BodyDef
                {
                    Position   = new Vector2(-7.0f, 6.0f),
                    BodyType   = BodyType.DynamicBody,
                    AllowSleep = false
                };

                _character = World.CreateBody(bd);

                var shape = new CircleShape {
                    Radius = 0.25f
                };

                var fd = new FixtureDef
                {
                    Shape    = shape,
                    Density  = 20.0f,
                    Friction = 1.0f
                };
                _character.CreateFixture(fd);
            }
        }