A fixture definition is used to create a fixture. This class defines an abstract fixture definition. You can reuse fixture definitions safely.
Beispiel #1
0
        /// Creates a fixture and attach it to this body. Use this function if you need
        /// to set some fixture parameters, like friction. Otherwise you can create the
        /// fixture directly from a shape.
        /// This function automatically updates the mass of the body.
        /// @param def the fixture definition.
        /// @warning This function is locked during callbacks.
        public Fixture CreateFixture(FixtureDef def)
        {
            Box2DXDebug.Assert(_world.IsLocked() == false);
            if (_world.IsLocked() == true)
            {
                return(null);
            }

            BroadPhase broadPhase = _world._contactManager._broadPhase;

            Fixture fixture = new Fixture();

            fixture.Create(broadPhase, this, _xf, def);

            fixture._next = _fixtureList;
            _fixtureList  = fixture;
            ++_fixtureCount;

            fixture.Body = this;

            bool needMassUpdate = fixture._massData.Mass > 0.0f || fixture._massData.I > 0.0f;

            // Adjust mass properties if needed.
            if (needMassUpdate)
            {
                ResetMass();
            }

            // Let the world know we have a new fixture. This will cause new contacts
            // to be created at the beginning of the next time step.
            _world._flags |= World.WorldFlags.NewFixture;

            return(fixture);
        }
        public VaryingRestitution()
        {
            {
                BodyDef bd = new BodyDef();
                Body ground = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0);
            }

            {
                CircleShape shape = new CircleShape();
                shape._radius = 1.0f;

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

                float[] restitution = { 0.0f, 0.1f, 0.3f, 0.5f, 0.75f, 0.9f, 1.0f };

                for (int i = 0; i < 7; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(-10.0f + 3.0f * i, 20.0f);

                    Body body = _world.CreateBody(bd);

                    fd.Restitution = restitution[i];
                    body.CreateFixture(fd);
                }
            }
        }
Beispiel #3
0
        /// Creates a fixture from a shape and attach it to this body.
        /// This is a convenience function. Use b2FixtureDef if you need to set parameters
        /// like friction, restitution, user data, or filtering.
        /// This function automatically updates the mass of the body.
        /// @param shape the shape to be cloned.
        /// @param density the shape density (set to zero for static bodies).
        /// @warning This function is locked during callbacks.
        public Fixture CreateFixture(Shape shape, float density)
        {
            FixtureDef def = new FixtureDef();

            def.Shape   = shape;
            def.Density = density;

            return(CreateFixture(def));
        }
Beispiel #4
0
        public Confined()
        {
            {
                BodyDef bd = new BodyDef();
                Body ground = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();

                // Floor
                shape.SetAsEdge(new Vec2(-10.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0);

                // Left wall
                shape.SetAsEdge(new Vec2(-10.0f, 0.0f), new Vec2(-10.0f, 20.0f));
                ground.CreateFixture(shape, 0);

                // Right wall
                shape.SetAsEdge(new Vec2(10.0f, 0.0f), new Vec2(10.0f, 20.0f));
                ground.CreateFixture(shape, 0);

                // Roof
                shape.SetAsEdge(new Vec2(-10.0f, 20.0f), new Vec2(10.0f, 20.0f));
                ground.CreateFixture(shape, 0);
            }

            {
                float radius = 0.5f;
                CircleShape shape = new CircleShape();
                shape._p.SetZero();
                shape._radius = radius;

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

                for (int j = 0; j < _columnCount; ++j)
                {
                    for (int i = 0; i < _rowCount; ++i)
                    {
                        BodyDef bd = new BodyDef();
                        bd.Position.Set(-10.0f + (2.1f * j + 1.0f + 0.01f * i) * radius, (2.0f * i + 1.0f) * radius);
                        Body body = _world.CreateBody(bd);

                        body.CreateFixture(fd);
                    }
                }
            }

            _world.Gravity = new Vec2(0, 0);
        }
Beispiel #5
0
        public SensorTest()
        {
            {
                BodyDef bd = new BodyDef();
                Body ground = _world.CreateBody(bd);

                {
                    PolygonShape shape = new PolygonShape();
                    shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                    ground.CreateFixture(shape, 0);
                }

            #if false
            {
                b2FixtureDef sd;
                sd.SetAsBox(10.0f, 2.0f, b2Vec2(0.0f, 20.0f), 0.0f);
                sd.isSensor = true;
                m_sensor = ground->CreateFixture(&sd);
            }
            #else
                {
                    CircleShape shape = new CircleShape();
                    shape._radius = 5.0f;
                    shape._p.Set(0.0f, 10.0f);

                    FixtureDef fd = new FixtureDef();
                    fd.Shape = shape;
                    fd.IsSensor = true;
                    _sensor = ground.CreateFixture(fd);
                }
            #endif
            }

            {
                CircleShape shape = new CircleShape();
                shape._radius = 1.0f;

                for (int i = 0; i < _count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(-10.0f + 3.0f * i, 20.0f);
                    bd.UserData = false;

                    _bodies[i] = _world.CreateBody(bd);

                    _bodies[i].CreateFixture(shape, 1.0f);
                }
            }
        }
Beispiel #6
0
        public Chain()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0);
            }

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

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

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.CollideConnected = false;

                const float y = 25.0f;
                Body prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(0.5f + i, y);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(fd);

                    Vec2 anchor = new Vec2(i, y);
                    jd.Initialize(prevBody, body, anchor);
                    _world.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
Beispiel #7
0
        // We need separation create/destroy functions from the constructor/destructor because
        // the destructor cannot access the allocator or broad-phase (no destructor arguments allowed by C++).
        public void Create(BroadPhase broadPhase, Body body, Transform xf, FixtureDef def)
        {
            UserData    = def.UserData;
            Friction    = def.Friction;
            Restitution = def.Restitution;

            Body  = body;
            _next = null;

            Filter = def.Filter;

            IsSensor = def.IsSensor;

            Shape = def.Shape.Clone();

            Shape.ComputeMass(out _massData, def.Density);

            // Create proxy in the broad-phase.
            Shape.ComputeAABB(out Aabb, ref xf);

            ProxyId = broadPhase.CreateProxy(Aabb, this);
        }
Beispiel #8
0
        /// <summary>
        /// Creates a fixture and attach it to this body.
        /// @warning This function is locked during callbacks.
        /// </summary>
        /// <param name="def">The fixture definition.</param>
        public Fixture CreateFixture(FixtureDef def)
        {
            Box2DXDebug.Assert(_world._lock == false);
            if (_world._lock == true)
            {
                return(null);
            }

            BroadPhase broadPhase = _world._broadPhase;

            Fixture fixture = new Fixture();

            fixture.Create(broadPhase, this, _xf, def);

            fixture._next = _fixtureList;
            _fixtureList  = fixture;
            ++_fixtureCount;

            fixture._body = this;

            return(fixture);
        }
Beispiel #9
0
        /// Creates a fixture from a shape and attach it to this body.
        /// This is a convenience function. Use b2FixtureDef if you need to set parameters
        /// like friction, restitution, user data, or filtering.
        /// This function automatically updates the mass of the body.
        /// @param shape the shape to be cloned.
        /// @param density the shape density (set to zero for static bodies).
        /// @warning This function is locked during callbacks.
        public Fixture CreateFixture(Shape shape, float density)
        {
            FixtureDef def = new FixtureDef();
            def.Shape = shape;
            def.Density = density;

            return CreateFixture(def);
        }
Beispiel #10
0
        /// Creates a fixture and attach it to this body. Use this function if you need
        /// to set some fixture parameters, like friction. Otherwise you can create the
        /// fixture directly from a shape.
        /// This function automatically updates the mass of the body.
        /// @param def the fixture definition.
        /// @warning This function is locked during callbacks.
        public Fixture CreateFixture(FixtureDef def)
        {
            Box2DXDebug.Assert(_world.IsLocked() == false);
            if (_world.IsLocked() == true)
            {
                return null;
            }

            BroadPhase broadPhase = _world._contactManager._broadPhase;

            Fixture fixture = new Fixture();
            fixture.Create(broadPhase, this, _xf, def);

            fixture._next = _fixtureList;
            _fixtureList = fixture;
            ++_fixtureCount;

            fixture.Body = this;

            bool needMassUpdate = fixture._massData.Mass > 0.0f || fixture._massData.I > 0.0f;

            // Adjust mass properties if needed.
            if (needMassUpdate)
            {
                ResetMass();
            }

            // Let the world know we have a new fixture. This will cause new contacts
            // to be created at the beginning of the next time step.
            _world._flags |= World.WorldFlags.NewFixture;

            return fixture;
        }
Beispiel #11
0
        public void Create(BroadPhase broadPhase, Body body, XForm xf, FixtureDef def)
        {
            UserData    = def.UserData;
            Friction    = def.Friction;
            Restitution = def.Restitution;
            Density     = def.Density;

            _body = body;
            _next = null;

            Filter = def.Filter;

            _isSensor = def.IsSensor;

            _type = def.Type;

            // Allocate and initialize the child shape.
            switch (_type)
            {
            case ShapeType.CircleShape:
            {
                CircleShape circle    = new CircleShape();
                CircleDef   circleDef = (CircleDef)def;
                circle._position = circleDef.LocalPosition;
                circle._radius   = circleDef.Radius;
                _shape           = circle;
            }
            break;

            case ShapeType.PolygonShape:
            {
                PolygonShape polygon    = new PolygonShape();
                PolygonDef   polygonDef = (PolygonDef)def;
                polygon.Set(polygonDef.Vertices, polygonDef.VertexCount);
                _shape = polygon;
            }
            break;

            case ShapeType.EdgeShape:
            {
                EdgeShape edge    = new EdgeShape();
                EdgeDef   edgeDef = (EdgeDef)def;
                edge.Set(edgeDef.Vertex1, edgeDef.Vertex2);
                _shape = edge;
            }
            break;

            default:
                Box2DXDebug.Assert(false);
                break;
            }

            // Create proxy in the broad-phase.
            AABB aabb;

            _shape.ComputeAABB(out aabb, xf);

            bool inRange = broadPhase.InRange(aabb);

            // You are creating a shape outside the world box.
            Box2DXDebug.Assert(inRange);

            if (inRange)
            {
                _proxyId = broadPhase.CreateProxy(aabb, this);
            }
            else
            {
                _proxyId = PairManager.NullProxy;
            }
        }
        public CollisionProcessing()
        {
            // Ground body
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(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 = _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.Position.Set(Math.Random(xLo, xHi), Math.Random(yLo, yHi));

            Body body1 = _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(Math.Random(xLo, xHi), Math.Random(yLo, yHi));

            Body body2 = _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.Position.Set(Math.Random(xLo, xHi), Math.Random(yLo, yHi));

            Body body3 = _world.CreateBody(boxBodyDef);
            body3.CreateFixture(boxShapeDef);

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

            Body body4 = _world.CreateBody(boxBodyDef);
            body4.CreateFixture(boxShapeDef);

            // Small circle
            CircleShape circle = new CircleShape();
            circle._radius = 1.0f;

            FixtureDef circleShapeDef = new FixtureDef();
            circleShapeDef.Shape = circle;
            circleShapeDef.Density = 1.0f;

            BodyDef circleBodyDef = new BodyDef();
            circleBodyDef.Position.Set(Math.Random(xLo, xHi), Math.Random(yLo, yHi));

            Body body5 = _world.CreateBody(circleBodyDef);
            body5.CreateFixture(circleShapeDef);

            // Large circle
            circle._radius *= 2.0f;
            circleBodyDef.Position.Set(Math.Random(xLo, xHi), Math.Random(yLo, yHi));

            Body body6 = _world.CreateBody(circleBodyDef);
            body6.CreateFixture(circleShapeDef);
        }
Beispiel #13
0
        // We need separation create/destroy functions from the constructor/destructor because
        // the destructor cannot access the allocator or broad-phase (no destructor arguments allowed by C++).
        public void Create(BroadPhase broadPhase, Body body, Transform xf, FixtureDef def)
        {
            UserData = def.UserData;
            Friction = def.Friction;
            Restitution = def.Restitution;

            Body = body;
            _next = null;

            Filter = def.Filter;

            IsSensor = def.IsSensor;

            Shape = def.Shape.Clone();

            Shape.ComputeMass(out _massData, def.Density);

            // Create proxy in the broad-phase.
            Shape.ComputeAABB(out Aabb, ref xf);

            ProxyId = broadPhase.CreateProxy(Aabb, this);
        }
Beispiel #14
0
        public ApplyForce()
        {
            _world.Gravity = new Vec2(0.0f, 0.0f);

            const float k_restitution = 0.4f;

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

                PolygonShape shape = new PolygonShape();

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

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

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

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

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

            {
                Transform xf1 = new Transform();
                xf1.R.Set(0.3524f * Box2DX.Common.Settings.PI);
                xf1.Position = Math.Mul(xf1.R, new Vec2(1.0f, 0.0f));

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

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

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

                Transform xf2 = new Transform();
                xf2.R.Set(-0.3524f * Box2DX.Common.Settings.PI);
                xf2.Position = Math.Mul(xf2.R, new Vec2(-1.0f, 0.0f));

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

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

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

                BodyDef bd = new BodyDef();
                bd.AngularDamping = 2.0f;
                bd.LinearDamping = 0.1f;

                bd.Position.Set(0.0f, 2.0f);
                bd.Angle = Box2DX.Common.Settings.PI;
                _body = _world.CreateBody(bd);
                _body.CreateFixture(sd1);
                _body.CreateFixture(sd2);
            }
        }
Beispiel #15
0
        public void Create(BroadPhase broadPhase, Body body, XForm xf, FixtureDef def)
        {
            UserData = def.UserData;
            Friction = def.Friction;
            Restitution = def.Restitution;
            Density = def.Density;

            _body = body;
            _next = null;

            Filter = def.Filter;

            _isSensor = def.IsSensor;

            _type = def.Type;

            // Allocate and initialize the child shape.
            switch (_type)
            {
                case ShapeType.CircleShape:
                    {
                        CircleShape circle = new CircleShape();
                        CircleDef circleDef = (CircleDef)def;
                        circle._position = circleDef.LocalPosition;
                        circle._radius = circleDef.Radius;
                        _shape = circle;
                    }
                    break;

                case ShapeType.PolygonShape:
                    {
                        PolygonShape polygon = new PolygonShape();
                        PolygonDef polygonDef = (PolygonDef)def;
                        polygon.Set(polygonDef.Vertices, polygonDef.VertexCount);
                        _shape = polygon;
                    }
                    break;

                case ShapeType.EdgeShape:
                    {
                        EdgeShape edge = new EdgeShape();
                        EdgeDef edgeDef = (EdgeDef)def;
                        edge.Set(edgeDef.Vertex1, edgeDef.Vertex2);
                        _shape = edge;
                    }
                    break;

                default:
                    Box2DXDebug.Assert(false);
                    break;
            }

            // Create proxy in the broad-phase.
            AABB aabb;
            _shape.ComputeAABB(out aabb, xf);

            bool inRange = broadPhase.InRange(aabb);

            // You are creating a shape outside the world box.
            Box2DXDebug.Assert(inRange);

            if (inRange)
            {
                _proxyId = broadPhase.CreateProxy(aabb, this);
            }
            else
            {
                _proxyId = PairManager.NullProxy;
            }
        }
Beispiel #16
0
        private void CreateLeg(float s, Vec2 wheelAnchor)
        {
            Vec2 p1 = new Vec2(5.4f * s, -6.1f);
            Vec2 p2 = new Vec2(7.2f * s, -1.2f);
            Vec2 p3 = new Vec2(4.3f * s, -1.9f);
            Vec2 p4 = new Vec2(3.1f * s, 0.8f);
            Vec2 p5 = new Vec2(6.0f * s, 1.5f);
            Vec2 p6 = new Vec2(2.5f * s, 3.7f);

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

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

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

                vertices[0] = p1;
                vertices[1] = p2;
                vertices[2] = p3;
                poly1.Set(vertices, 3);

                vertices[0] = Vec2.Zero;
                vertices[1] = p5 - p4;
                vertices[2] = p6 - p4;
                poly2.Set(vertices, 3);
            }
            else
            {
                Vec2[] vertices = new Vec2[3];

                vertices[0] = p1;
                vertices[1] = p3;
                vertices[2] = p2;
                poly1.Set(vertices, 3);

                vertices[0] = Vec2.Zero;
                vertices[1] = p6 - p4;
                vertices[2] = p5 - p4;
                poly2.Set(vertices, 3);
            }

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

            BodyDef bd1 = new BodyDef();
            BodyDef bd2 = new BodyDef();
            bd1.Position = _offset;
            bd2.Position = p4 + _offset;

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

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

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

            DistanceJointDef djd = new DistanceJointDef();

            // Using a soft distance constraint can reduce some jitter.
            // It also makes the structure seem a bit more fluid by
            // acting like a suspension system.
            djd.DampingRatio = 0.5f;
            djd.FrequencyHz = 10.0f;

            djd.Initialize(body1, body2, p2 + _offset, p5 + _offset);
            _world.CreateJoint(djd);

            djd.Initialize(body1, body2, p3 + _offset, p4 + _offset);
            _world.CreateJoint(djd);

            djd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset);
            _world.CreateJoint(djd);

            djd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset);
            _world.CreateJoint(djd);

            RevoluteJointDef rjd = new RevoluteJointDef();

            rjd.Initialize(body2, _chassis, p4 + _offset);
            _world.CreateJoint(rjd);
        }
        public VaryingFriction()
        {
            {
                BodyDef bd = new BodyDef();
                Body ground = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-4.0f, 22.0f);
                bd.Angle = -0.25f;

                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape, 0);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(10.5f, 19.0f);

                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape, 0);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(4.0f, 14.0f);
                bd.Angle = 0.25f;

                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape, 0);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-10.5f, 11.0f);

                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape, 0);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-4.0f, 6.0f);
                bd.Angle = -0.25f;

                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape, 0);
            }

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

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

                float[] friction = { 0.75f, 0.5f, 0.35f, 0.1f, 0.0f };

                for (int i = 0; i < 5; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(-15.0f + 4.0f * i, 28.0f);
                    Body body = _world.CreateBody(bd);

                    fd.Friction = friction[i];
                    body.CreateFixture(fd);
                }
            }
        }
Beispiel #18
0
        private void CreateCircle()
        {
            float radius = 0.5f;
            CircleShape shape = new CircleShape();
            shape._p.SetZero();
            shape._radius = radius;

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

            BodyDef bd = new BodyDef();
            bd.Position.Set(Math.Random(), (2.0f + Math.Random()) * radius);
            Body body = _world.CreateBody(bd);

            body.CreateFixture(fd);
        }
Beispiel #19
0
        public Dominos()
        {
            Body b1;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));

                BodyDef bd = new BodyDef();
                b1 = _world.CreateBody(bd);
                b1.CreateFixture(shape,0);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-1.5f, 10.0f);
                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape,0);
            }

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

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

                for (int i = 0; i < 10; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(-6.0f + 1.0f * i, 11.25f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(fd);
                }
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(7.0f, 0.25f, Vec2.Zero, 0.3f);

                BodyDef bd = new BodyDef();
                bd.Position.Set(1.0f, 6.0f);
                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape,0);
            }

            Body b2;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.25f, 1.5f);

                BodyDef bd = new BodyDef();
                bd.Position.Set(-7.0f, 4.0f);
                 b2 = _world.CreateBody(bd);
                b2.CreateFixture(shape,0);
            }

            Body b3;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(6.0f, 0.125f);

                BodyDef bd = new BodyDef();
                bd.Position.Set(-0.9f, 1.0f);
                bd.Angle = -0.15f;

                b3 = _world.CreateBody(bd);
                b3.CreateFixture(shape, 10.0f);
            }

            RevoluteJointDef jd = new RevoluteJointDef();
            Vec2 anchor = new Vec2();

            anchor.Set(-2.0f, 1.0f);
            jd.Initialize(b1, b3, anchor);
            jd.CollideConnected = true;
            _world.CreateJoint(jd);

            Body b4;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.25f, 0.25f);

                BodyDef bd  = new BodyDef();
                bd.Position.Set(-10.0f, 15.0f);
                b4 = _world.CreateBody(bd);
                b4.CreateFixture(shape, 10.0f);
            }

            anchor.Set(-7.0f, 15.0f);
            jd.Initialize(b2, b4, anchor);
            _world.CreateJoint(jd);

            Body b5;
            {
                BodyDef bd = new BodyDef();
                bd.Position.Set(6.5f, 3.0f);
                b5 = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                FixtureDef fd = new FixtureDef();

                fd.Shape = shape;
                fd.Density  = 10.0f;
                fd.Friction = 0.1f;

                shape.SetAsBox(1.0f, 0.1f,new Vec2(0.0f, -0.9f), 0.0f);
                b5.CreateFixture(fd);

                shape.SetAsBox(0.1f, 1.0f, new Vec2(-0.9f, 0.0f), 0.0f);
                b5.CreateFixture(fd);

                shape.SetAsBox(0.1f, 1.0f,new Vec2(0.9f, 0.0f), 0.0f);
                b5.CreateFixture(fd);

            }

            anchor.Set(6.0f, 2.0f);
            jd.Initialize(b1, b5, anchor);
            _world.CreateJoint(jd);

            Body b6;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(1.0f, 0.1f);

                BodyDef bd = new BodyDef();
                bd.Position.Set(6.5f, 4.1f);
                b6 = _world.CreateBody(bd);
                b6.CreateFixture(shape, 30.0f);
            }

            anchor.Set(7.5f, 4.0f);
            jd.Initialize(b5, b6, anchor);
            _world.CreateJoint(jd);

            Body b7;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.1f, 1.0f);

                BodyDef bd = new BodyDef();
                bd.Position.Set(7.4f, 1.0f);

                b7 = _world.CreateBody(bd);
                b7.CreateFixture(shape, 10.0f);
            }

            DistanceJointDef djd  =new DistanceJointDef();
            djd.Body1 = b3;
            djd.Body2 = b7;
            djd.LocalAnchor1.Set(6.0f, 0.0f);
            djd.LocalAnchor2.Set(0.0f, -1.0f);
            Vec2 d = djd.Body2.GetWorldPoint(djd.LocalAnchor2) - djd.Body1.GetWorldPoint(djd.LocalAnchor1);
            djd.Length = d.Length();
            _world.CreateJoint(djd);

            {
                float radius = 0.2f;

                CircleShape shape = new CircleShape();
                shape._radius = radius;

                for (int i = 0; i < 4; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(5.9f + 2.0f * radius * i, 2.4f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 10.0f);
                }
            }
        }
Beispiel #20
0
        public CCDTest()
        {
            #if true
            {
                //TODO: only one shape is created here instead of two. That is because
                //they are reference types and when shape is changed, it changes the original instead of a new copy.
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(10.0f, 0.2f);

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(0.0f, -0.2f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(fd);

                shape.SetAsBox(0.2f, 1.0f, new Vec2(0.5f, 1.2f), 0.0f);
                body.CreateFixture(fd);
            }

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

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

                _angularVelocity = Math.Random(-50.0f, 50.0f);
                _angularVelocity = -30.669577f;

                BodyDef bd = new BodyDef();
                bd.Position.Set(0.0f, 20.0f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(fd);
                body.SetLinearVelocity(new Vec2(0.0f, -100.0f));
                body.SetAngularVelocity(_angularVelocity);
            }
            #elif false
            {
            FixtureDef fd;
            fd.SetAsBox(10.0f, 0.1f);
            fd.density = 0.0f;

            BodyDef bd;
            bd.type = BodyDef::e_static;
            bd.position.Set(0.0f, -0.2f);
            Body* ground = m_world.CreateBody(&bd);
            ground.CreateFixture(&fd);
            }

            {
            FixtureDef fd;
            fd.SetAsBox(2.0f, 0.1f);
            fd.density = 1.0f;
            fd.restitution = 0.0f;

            BodyDef bd1;
            bd1.type = BodyDef::e_dynamic;
            bd1.isBullet = true;
            bd1.allowSleep = false;
            bd1.position.Set(0.0f, 20.0f);
            Body* b1 = m_world.Create(&bd1);
            b1.CreateFixture(&fd);
            b1.SetMassFromShapes();
            b1.SetLinearVelocity(Vec2(0.0f, -100.0f));

            fd.SetAsBox(1.0f, 0.1f);
            BodyDef bd2;
            bd2.type = BodyDef::e_dynamic;
            bd2.isBullet = true;
            bd2.allowSleep = false;
            bd2.position.Set(0.0f, 20.2f);
            Body*  = m_world.Create(&bd2);
            .CreateFixture(&fd);
            .SetMassFromShapes();
            .SetLinearVelocity(Vec2(0.0f, -100.0f));

            fd.SetAsBox(0.25f, 0.25f);
            fd.density = 10.0f;
            BodyDef bd3;
            bd3.type = BodyDef::e_dynamic;
            bd3.isBullet = true;
            bd3.allowSleep = false;
            bd3.position.Set(0.0f, 100.0f);
            Body* b3 = m_world.Create(&bd3);
            b3.CreateFixture(&fd);
            b3.SetMassFromShapes();
            b3.SetLinearVelocity(Vec2(0.0f, -150.0f));
            }
            #else
            const float32 k_restitution = 1.4f;

            {
            BodyDef bd;
            bd.position.Set(0.0f, 20.0f);
            Body* body = m_world.CreateBody(&bd);

            FixtureDef fd;
            fd.density = 0.0f;
            fd.restitution = k_restitution;

            fd.SetAsBox(0.1f, 10.0f, Vec2(-10.0f, 0.0f), 0.0f);
            body.CreateFixture(&fd);

            fd.SetAsBox(0.1f, 10.0f, Vec2(10.0f, 0.0f), 0.0f);
            body.CreateFixture(&fd);

            fd.SetAsBox(0.1f, 10.0f, Vec2(0.0f, -10.0f), 0.5f * _pi);
            body.CreateFixture(&fd);

            fd.SetAsBox(0.1f, 10.0f, Vec2(0.0f, 10.0f), -0.5f * _pi);
            body.CreateFixture(&fd);
            }

            #if false
            {
            FixtureDef sd_bottom;
            sd_bottom.SetAsBox(1.0f, 0.1f, Vec2(0.0f, -1.0f), 0.0f);
            sd_bottom.density = 4.0f;

            FixtureDef sd_top;
            sd_top.SetAsBox(1.0f, 0.1f, Vec2(0.0f,  1.0f), 0.0f);
            sd_top.density = 4.0f;

            FixtureDef sd_left;
            sd_left.SetAsBox(0.1f, 1.0f, Vec2(-1.0f, 0.0f), 0.0f);
            sd_left.density = 4.0f;

            FixtureDef sd_right;
            sd_right.SetAsBox(0.1f, 1.0f, Vec2(1.0f, 0.0f), 0.0f);
            sd_right.density = 4.0f;

            BodyDef bd;
            bd.type = BodyDef::e_dynamicBody;
            bd.position.Set(0.0f, 15.0f);
            Body* body = m_world.CreateBody(&bd);
            body.CreateFixture(&sd_bottom);
            body.CreateFixture(&sd_top);
            body.CreateFixture(&sd_left);
            body.CreateFixture(&sd_right);
            body.SetMassFromShapes();
            }
            #elif false
            {
            FixtureDef sd_bottom;
            sd_bottom.SetAsBox( 1.5f, 0.15f );
            sd_bottom.density = 4.0f;

            FixtureDef sd_left;
            sd_left.SetAsBox(0.15f, 2.7f, Vec2(-1.45f, 2.35f), 0.2f);
            sd_left.density = 4.0f;

            FixtureDef sd_right;
            sd_right.SetAsBox(0.15f, 2.7f, Vec2(1.45f, 2.35f), -0.2f);
            sd_right.density = 4.0f;

            BodyDef bd;
            bd.position.Set( 0.0f, 15.0f );
            Body* body = m_world.CreateBody(&bd);
            body.CreateFixture(&sd_bottom);
            body.CreateFixture(&sd_left);
            body.CreateFixture(&sd_right);
            body.SetMassFromShapes();
            }
            #else
            {
            BodyDef bd;
            bd.position.Set(-5.0f, 20.0f);
            bd.isBullet = true;
            Body* body = m_world.CreateBody(&bd);
            body.SetAngularVelocity(RandomFloat(-50.0f, 50.0f));

            FixtureDef fd;
            fd.SetAsBox(0.1f, 4.0f);
            fd.density = 1.0f;
            fd.restitution = 0.0f;
            body.CreateFixture(&fd);
            body.SetMassFromShapes();
            }
            #endif

            for (int32 i = 0; i < 0; ++i)
            {
            BodyDef bd;
            bd.position.Set(0.0f, 15.0f + i);
            bd.isBullet = true;
            Body* body = m_world.CreateBody(&bd);
            body.SetAngularVelocity(RandomFloat(-50.0f, 50.0f));

            FixtureDef fd;
            fd.radius = 0.25f;
            fd.density = 1.0f;
            fd.restitution = 0.0f;
            body.CreateFixture(&fd);
            body.SetMassFromShapes();
            }
            #endif
        }
Beispiel #21
0
        public void LaunchBomb(Vec2 position, Vec2 velocity)
        {
            if (_bomb != null)
            {
                _world.DestroyBody(_bomb);
                _bomb = null;
            }

            BodyDef bd = new BodyDef();
            bd.AllowSleep = true;
            bd.Position = position;

            bd.IsBullet = true;
            _bomb = _world.CreateBody(bd);
            _bomb.SetLinearVelocity(velocity);

            CircleShape circle = new CircleShape();
            circle._radius = 0.3f;

            FixtureDef fd = new FixtureDef();
            fd.Shape = circle;
            fd.Density = 20.0f;
            fd.Restitution = 0.1f;

            Vec2 minV = position - new Vec2(0.3f, 0.3f);
            Vec2 maxV = position + new Vec2(0.3f, 0.3f);

            AABB aabb = new AABB();
            aabb.LowerBound = minV;
            aabb.UpperBound = maxV;

            _bomb.CreateFixture(fd);
        }
Beispiel #22
0
        void Create(int index)
        {
            if (_bodies[_bodyIndex] != null)
            {
                _world.DestroyBody(_bodies[_bodyIndex]);
                _bodies[_bodyIndex] = null;
            }

            BodyDef bd = new BodyDef();

            float x = Math.Random(-10.0f, 10.0f);
            float y = Math.Random(0.0f, 20.0f);
            bd.Position.Set(x, y);
            bd.Angle = Math.Random(-Box2DX.Common.Settings.PI, Box2DX.Common.Settings.PI);

            if (index == 4)
            {
                bd.AngularDamping = 0.02f;
            }

            _bodies[_bodyIndex] = _world.CreateBody(bd);

            if (index < 4)
            {
                FixtureDef fd = new FixtureDef();
                fd.Shape = _polygons[index];
                fd.Friction = 0.3f;
                _bodies[_bodyIndex].CreateFixture(fd);
            }
            else
            {
                FixtureDef fd = new FixtureDef();
                fd.Shape = _circle;
                fd.Friction = 0.3f;

                _bodies[_bodyIndex].CreateFixture(fd);
            }

            _bodyIndex = (_bodyIndex + 1) % _maxBodies;
        }
Beispiel #23
0
        public TheoJansen()
        {
            _offset.Set(0.0f, 8.0f);
            _motorSpeed = 2.0f;
            _motorOn = true;
            Vec2 pivot = new Vec2(0.0f, 0.8f);

            // Ground
            {
                BodyDef bd = new BodyDef();
                Body ground = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-50.0f, 0.0f), new Vec2(50.0f, 0.0f));
                ground.CreateFixture(shape, 0);

                shape.SetAsEdge(new Vec2(-50.0f, 0.0f), new Vec2(-50.0f, 10.0f));
                ground.CreateFixture(shape, 0);

                shape.SetAsEdge(new Vec2(50.0f, 0.0f), new Vec2(50.0f, 10.0f));
                ground.CreateFixture(shape, 0);
            }

            // Balls
            for (int i = 0; i < 40; ++i)
            {
                CircleShape shape = new CircleShape();
                shape._radius = 0.25f;

                BodyDef bd = new BodyDef();
                bd.Position.Set(-40.0f + 2.0f * i, 0.5f);

                Body body = _world.CreateBody(bd);
                body.CreateFixture(shape, 1.0f);
            }

            // Chassis
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(2.5f, 1.0f);

                FixtureDef sd = new FixtureDef();
                sd.Density = 1.0f;
                sd.Shape = shape;
                sd.Filter.GroupIndex = -1;
                BodyDef bd = new BodyDef();
                bd.Position = pivot + _offset;
                _chassis = _world.CreateBody(bd);
                _chassis.CreateFixture(sd);
            }

            {
                CircleShape shape = new CircleShape();
                shape._radius = 1.6f;

                FixtureDef sd = new FixtureDef();
                sd.Density = 1.0f;
                sd.Shape = shape;
                sd.Filter.GroupIndex = -1;
                BodyDef bd = new BodyDef();
                bd.Position = pivot + _offset;
                _wheel = _world.CreateBody(bd);
                _wheel.CreateFixture(sd);
            }

            {
                RevoluteJointDef jd = new RevoluteJointDef();
                jd.Initialize(_wheel, _chassis, pivot + _offset);
                jd.CollideConnected = false;
                jd.MotorSpeed = _motorSpeed;
                jd.MaxMotorTorque = 400.0f;
                jd.EnableMotor = _motorOn;
                _motorJoint = (RevoluteJoint)_world.CreateJoint(jd);
            }

            Vec2 wheelAnchor;

            wheelAnchor = pivot + new Vec2(0.0f, -0.8f);

            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            _wheel.SetTransform(_wheel.GetPosition(), 120.0f * Box2DX.Common.Settings.PI / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            _wheel.SetTransform(_wheel.GetPosition(), -120.0f * Box2DX.Common.Settings.PI / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);
        }
        public CollisionFiltering()
        {
            // Ground body
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));

                FixtureDef sd = new FixtureDef();
                sd.Shape = shape;
                sd.Friction = 0.3f;

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

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

            triangleShapeDef.Filter.GroupIndex = k_smallGroup;
            triangleShapeDef.Filter.CategoryBits = k_triangleCategory;
            triangleShapeDef.Filter.MaskBits = k_triangleMask;

            BodyDef triangleBodyDef = new BodyDef();
            triangleBodyDef.Position.Set(-5.0f, 2.0f);

            Body body1 = _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);
            triangleShapeDef.Filter.GroupIndex = k_largeGroup;
            triangleBodyDef.Position.Set(-5.0f, 6.0f);
            triangleBodyDef.FixedRotation = true; // look at me!

            Body body2 = _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;
            boxShapeDef.Restitution = 0.1f;

            boxShapeDef.Filter.GroupIndex = k_smallGroup;
            boxShapeDef.Filter.CategoryBits = k_boxCategory;
            boxShapeDef.Filter.MaskBits = k_boxMask;

            BodyDef boxBodyDef = new BodyDef();
            boxBodyDef.Position.Set(0.0f, 2.0f);

            Body body3 = _world.CreateBody(boxBodyDef);
            body3.CreateFixture(boxShapeDef);

            // Large box (recycle definitions)
            polygon.SetAsBox(2.0f, 1.0f);
            boxShapeDef.Filter.GroupIndex = k_largeGroup;
            boxBodyDef.Position.Set(0.0f, 6.0f);

            Body body4 = _world.CreateBody(boxBodyDef);
            body4.CreateFixture(boxShapeDef);

            // Small circle
            CircleShape circle = new CircleShape();
            circle._radius = 1.0f;

            FixtureDef circleShapeDef = new FixtureDef();
            circleShapeDef.Shape = circle;
            circleShapeDef.Density = 1.0f;

            circleShapeDef.Filter.GroupIndex = k_smallGroup;
            circleShapeDef.Filter.CategoryBits = k_circleCategory;
            circleShapeDef.Filter.MaskBits = k_circleMask;

            BodyDef circleBodyDef = new BodyDef();
            circleBodyDef.Position.Set(5.0f, 2.0f);

            Body body5 = _world.CreateBody(circleBodyDef);
            body5.CreateFixture(circleShapeDef);

            // Large circle
            circle._radius *= 2.0f;
            circleShapeDef.Filter.GroupIndex = k_largeGroup;
            circleBodyDef.Position.Set(5.0f, 6.0f);

            Body body6 = _world.CreateBody(circleBodyDef);
            body6.CreateFixture(circleShapeDef);
        }
Beispiel #25
0
        /// <summary>
        /// Creates a fixture and attach it to this body.
        /// @warning This function is locked during callbacks.
        /// </summary>
        /// <param name="def">The fixture definition.</param>
        public Fixture CreateFixture(FixtureDef def)
        {
            Box2DXDebug.Assert(_world._lock == false);
            if (_world._lock == true)
            {
                return null;
            }

            BroadPhase broadPhase = _world._broadPhase;

            Fixture fixture = new Fixture();
            fixture.Create(broadPhase, this, _xf, def);

            fixture._next = _fixtureList;
            _fixtureList = fixture;
            ++_fixtureCount;

            fixture._body = this;

            return fixture;
        }
Beispiel #26
0
        public Bridge()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0);
            }

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

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

                RevoluteJointDef jd = new RevoluteJointDef();
                const int numPlanks = 30;

                Body prevBody = ground;
                for (int i = 0; i < _count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(-14.5f + 1.0f * i, 5.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(fd);

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

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

                    prevBody = body;
                }

                Vec2 anchor2 = new Vec2(-15.0f + 1.0f * _count, 5.0f);
                jd.Initialize(prevBody, ground, anchor2);
                _world.CreateJoint(jd);
            }

            for (int i = 0; i < 2; ++i)
            {
                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);

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

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-8.0f + 8.0f * i, 12.0f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(fd);
            }

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

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-6.0f + 6.0f * i, 10.0f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(fd);
            }
        }