A body definition holds all the data needed to construct a rigid body. You can safely re-use body definitions.
Example #1
0
		public ShapeEditing()
		{
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(50.0f, 10.0f);

				BodyDef bd = new BodyDef();
				bd.Position.Set(0.0f, -10.0f);

				Body ground = _world.CreateBody(bd);
				ground.CreateShape(sd);
			}

			BodyDef bodydef = new BodyDef();
			bodydef.Position.Set(0.0f, 10.0f);
			_body = _world.CreateBody(bodydef);

			PolygonDef sd_ = new PolygonDef();
			sd_.SetAsBox(4.0f, 4.0f, new Vec2(0.0f, 0.0f), 0.0f);
			sd_.Density = 10.0f;
			_shape1 = _body.CreateShape(sd_);
			_body.SetMassFromShapes();

			_shape2 = null;
		}
Example #2
0
        public Projectile(Player creator, float x, float y, float width, float height)
            : base(creator, 0, 0)
        {
            /* Create New Projectile Body */
            BodyDef def = new BodyDef();
            def.IsBullet = true;
            def.Position = creator.body.GetPosition() + new Vec2(x, y);
            projectile = creator.body.GetWorld().CreateBody(def);

            /* Create a fixture for the projectile */
            PolygonDef fixdef = new PolygonDef();
            fixdef.Density = 1.0f;
            fixdef.SetAsBox(width / 2, height / 2);
            fixdef.Filter.GroupIndex = creator.ID;
            fixture = projectile.CreateFixture(fixdef);
            fixture.Filter.CategoryBits = 0x0004;
            fixture.Filter.MaskBits = 0xFFFF;

            /* Made a 2nd fixture, one to observe all collisions */
            fixdef.IsSensor = true;
            fix2 = projectile.CreateFixture(fixdef);
            fix2.UserData = this;

            /* Finally, give this projectile some mass */
            projectile.SetMassFromShapes();

            /* Also, make sure we destroy the projectile when it is time */
            this.OnDestroy += Cleanup;
        }
Example #3
0
		public TimeOfImpact()
		{
			{
				PolygonDef sd = new PolygonDef();
				sd.Density = 0.0f;

				sd.SetAsBox(0.1f, 10.0f, new Vec2(10.0f, 0.0f), 0.0f);

				BodyDef bd = new BodyDef();
				bd.Position.Set(0.0f, 20.0f);
				bd.Angle = 0.0f;
				_body1 = _world.CreateBody(bd);
				_shape1 = _body1.CreateShape(sd);
			}

			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(0.25f, 0.25f);
				sd.Density = 1.0f;

				BodyDef bd = new BodyDef();
				bd.Position.Set(9.6363468f, 28.050615f);
				bd.Angle = 1.6408679f;
				_body2 = _world.CreateBody(bd);
				_shape2 = (PolygonShape)_body2.CreateShape(sd);
				_body2.SetMassFromShapes();
			}
		}
Example #4
0
		public CCDTest()
		{
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(10.0f, 0.2f);
				sd.Density = 0.0f;

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

				sd.SetAsBox(0.2f, 1.0f, new Vec2(0.5f, 1.2f), 0.0f);
				body.CreateFixture(sd);
			}
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(2.0f, 0.1f);
				sd.Density = 1.0f;
				sd.Restitution = 0;

				BodyDef bd = new BodyDef();
				bd.Position.Set(0.0f, 20.0f);
				Body body = _world.CreateBody(bd);
				body.CreateFixture(sd);
				body.SetMassFromShapes();
				body.SetLinearVelocity(new Vec2(0.0f, -100.0f));
				body.SetAngularVelocity(Box2DX.Common.Math.Random(-50.0f, 50.0f));
			}
		}
Example #5
0
 public World(AABB worldAABB, Vec2 gravity, bool doSleep)
 {
     this._destructionListener = null;
     this._boundaryListener = null;
     this._contactFilter = WorldCallback.DefaultFilter;
     this._contactListener = null;
     this._debugDraw = null;
     this._bodyList = null;
     this._contactList = null;
     this._jointList = null;
     this._bodyCount = 0;
     this._contactCount = 0;
     this._jointCount = 0;
     this._warmStarting = true;
     this._continuousPhysics = true;
     this._allowSleep = doSleep;
     this._gravity = gravity;
     this._lock = false;
     this._inv_dt0 = 0f;
     this._contactManager = new ContactManager();
     this._contactManager._world = this;
     this._broadPhase = new BroadPhase(worldAABB, this._contactManager);
     BodyDef def = new BodyDef();
     this._groundBody = this.CreateBody(def);
 }
Example #6
0
        public SphereStack()
        {
            {
                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;

                for (int i = 0; i < _count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(0.0f, 4.0f + 3.0f * i);

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

                    _bodies[i].CreateFixture(shape, 1.0f);

                    //m_bodies[i]->SetLinearVelocity(b2Vec2(0.0f, -100.0f));
                }
            }
        }
Example #7
0
		public VaryingRestitution()
		{
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(50.0f, 10.0f);

				BodyDef bd = new BodyDef();
				bd.Position.Set(0.0f, -10.0f);

				Body ground = _world.CreateBody(bd);
				ground.CreateShape(sd);
			}

			{
				CircleDef sd = new CircleDef();
				sd.Radius = 1.0f;
				sd.Density = 1.0f;

				float[] restitution = new float[7] { 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);

					sd.Restitution = restitution[i];
					body.CreateShape(sd);
					body.SetMassFromShapes();
				}
			}
		}
Example #8
0
		public DistanceTest()
		{
			PolygonDef sd = new PolygonDef();
			sd.SetAsBox(1.0f, 1.0f);
			sd.Density = 0.0f;

			BodyDef bd = new BodyDef();
			bd.Position.Set(0.0f, 10.0f);
			_body1 = _world.CreateBody(bd);
			_shape1 = _body1.CreateShape(sd);

			PolygonDef sd2 = new PolygonDef();
			sd2.VertexCount = 3;
			sd2.Vertices[0].Set(-1.0f, 0.0f);
			sd2.Vertices[1].Set(1.0f, 0.0f);
			sd2.Vertices[2].Set(0.0f, 15.0f);
			sd2.Density = 1.0f;

			BodyDef bd2 = new BodyDef();

			bd2.Position.Set(0.0f, 10.0f);

			_body2 = _world.CreateBody(bd2);
			_shape2 = _body2.CreateShape(sd2);
			_body2.SetMassFromShapes();

			_world.Gravity = new Vec2(0.0f, 0.0f);
		}
Example #9
0
        public Breakable()
        {
            // Ground body
            {
                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);
            }

            // Breakable dynamic body
            {
                BodyDef bd = new BodyDef();
                bd.Position.Set(0.0f, 40.0f);
                bd.Angle = 0.25f * Box2DX.Common.Settings.PI;
                _body1 = _world.CreateBody(bd);

                _shape1.SetAsBox(0.5f, 0.5f, new Vec2(-0.5f, 0.0f), 0.0f);
                _piece1 = _body1.CreateFixture(_shape1, 1.0f);

                _shape2.SetAsBox(0.5f, 0.5f, new Vec2(0.5f, 0.0f), 0.0f);
                _piece2 = _body1.CreateFixture(_shape2, 1.0f);
            }

            _break = false;
            _broke = false;
        }
Example #10
0
		public PolyCollision()
		{
			_localPoints[0].state = ContactState.ContactRemoved;
			_localPoints[1].state = ContactState.ContactRemoved;

			{
				PolygonDef sd = new PolygonDef();
				sd.Vertices[0].Set(-9.0f, -1.1f);
				sd.Vertices[1].Set(7.0f, -1.1f);
				sd.Vertices[2].Set(5.0f, -0.9f);
				sd.Vertices[3].Set(-11.0f, -0.9f);
				sd.VertexCount = 4;
				sd.Density = 0.0f;

				BodyDef bd = new BodyDef();
				bd.Position.Set(0.0f, 10.0f);
				_body1 = _world.CreateBody(bd);
				_body1.CreateShape(sd);
			}

			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(0.5f, 0.5f);
				sd.Density = 1.0f;

				BodyDef bd = new BodyDef();
				bd.Position.Set(0.0f, 10.0f);
				_body2 = _world.CreateBody(bd);
				_body2.CreateShape(sd);
				_body2.SetMassFromShapes();
			}

			_world.Gravity = Vec2.Zero;
		}
        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);
                }
            }
        }
Example #12
0
        private Transform _xf; // the body origin transform

        #endregion Fields

        #region Constructors

        internal Body(BodyDef bd, World world)
        {
            _flags = 0;

            if (bd.IsBullet)
            {
                _flags |= BodyFlags.Bullet;
            }
            if (bd.FixedRotation)
            {
                _flags |= BodyFlags.FixedRotation;
            }
            if (bd.AllowSleep)
            {
                _flags |= BodyFlags.AllowSleep;
            }
            if (bd.IsSleeping)
            {
                _flags |= BodyFlags.Sleep;
            }

            _world = world;

            _xf.Position = bd.Position;
            _xf.R.Set(bd.Angle);

            _sweep.LocalCenter.SetZero();
            _sweep.T0 = 1.0f;
            _sweep.A0 = _sweep.A = bd.Angle;
            _sweep.C0 = _sweep.C = Math.Mul(_xf, _sweep.LocalCenter);

            _jointList = null;
            _contactList = null;
            _prev = null;
            _next = null;

            _linearVelocity = bd.LinearVelocity;
            _angularVelocity = bd.AngularVelocity;

            _linearDamping = bd.LinearDamping;
            _angularDamping = bd.AngularDamping;

            _force.Set(0.0f, 0.0f);
            _torque = 0.0f;

            _sleepTime = 0.0f;

            _mass = 0;
            _invMass = 0.0f;
            _I = 0.0f;
            _invI = 0.0f;

            _type = BodyType.Static;

            _userData = bd.UserData;

            _fixtureList = null;
            _fixtureCount = 0;
        }
Example #13
0
        public PhysicsActor(World _world, Vector2 _position, float _angle = 0.0f, bool _isStatic = false)
        {
            BodyDef bodydef = new BodyDef();
            bodydef.Position = _position;
            bodydef.Angle = 0.0f;

            body = _world.CreateBody(bodydef);
        }
Example #14
0
 public Test()
 {
     AABB aabb = new AABB();
     aabb.UpperBound = new UnityEngine.Vector2(100,100);
     aabb.LowerBound = new UnityEngine.Vector2(-100,-100);
     World world = new World(aabb, new Vector2(0, -1), false);
     BodyDef groundBodyDef = new BodyDef();
     groundBodyDef.Position.Set(0, 0);
     PolygonShape groundBox = new PolygonShape();
     groundBox.SetAsBox(10, 1);
     Body body = new Body(groundBodyDef, world);
 }
Example #15
0
        public InGameState()
        {
            //position the score display centered at the top
            ScoreDisplay.CharacterSize = 90;
            ScoreDisplay.DisplayedString = InGameState.WinCount0.ToString();
            float leftSize = ScoreDisplay.GetLocalBounds().Width;
            ScoreDisplay.DisplayedString += " : ";
            float midSize = ScoreDisplay.GetLocalBounds().Width - leftSize;
            ScoreDisplay.DisplayedString += InGameState.WinCount1.ToString();
            ScoreDisplay.Origin = new Vector2(leftSize + 0.5f * midSize, 0f);
            ScoreDisplay.Position = new Vector2(Constants.windowSizeX / 2 , Constants.windowSizeY / 14);
            ScoreDisplay.Color = new SFML.Graphics.Color(200, 255, 200);

            AABB aabb = new AABB();
            aabb.LowerBound.Set(0.0f, 0.0f);
            aabb.UpperBound.Set(800, 600/*Constants.worldSizeX * Constants.screenRatio*/);

            physicsWorld = new World(aabb, new Vec2(0.0f, -9.81f), false);

            contactManager = Physics.ContactManager.g_contactManager;
            physicsWorld.SetContactListener(contactManager);

             // Set new Players and appending dekoHands
            ResetPlayers();
            setDekoFlags();
            //0xF0A58A4
            groundPolygonAct = new Actors.PolygonActor(physicsWorld, new Vec2(0.0f, 15.0f), 0xFBA58A4, Actors.FunctionType.GradientNoise, 4);

            BackgroundBackSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.InGameBackGroundBack));
            BackgroundBackSprite.Scale = new Vector2(Constants.windowSizeX / (float)BackgroundBackSprite.TextureRect.Width, Constants.windowSizeY / (float)BackgroundBackSprite.TextureRect.Height);//0.5F * Vector2.One;
            BackgroundFrontSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.InGameBackGroundFront));
            BackgroundFrontSprite.Scale = BackgroundBackSprite.Scale;

            //left and right borders of the map
            BodyDef bodydef = new BodyDef();
            bodydef.Position = new Vector2(0,0);
            bodydef.Angle = 0.0f;
            PolygonDef box = new PolygonDef();
            box.SetAsBox(1f, Constants.worldSizeY);

            Body leftEdge = physicsWorld.CreateBody(bodydef);
            contactManager.addNonLethalShape(leftEdge.CreateShape(box));

            bodydef.Position = new Vector2(Constants.worldSizeX-1, 0);
            Body rightEdge = physicsWorld.CreateBody(bodydef);
            contactManager.addNonLethalShape(rightEdge.CreateShape(box));

            bodydef.Position = new Vector2(0, Constants.worldSizeY);
            box.SetAsBox(Constants.worldSizeX, 1f);
            Body topEdge = physicsWorld.CreateBody(bodydef);
            contactManager.addNonLethalShape(topEdge.CreateShape(box));
        }
Example #16
0
 protected override void CreatePhysics(World world, float positionX, float positionY, float friction)
 {
     var bodyDef = new BodyDef();
     bodyDef.Position.Set(positionX, positionY);
     this.body = world.CreateBody(bodyDef);
     var shapeDef = new PolygonDef();
     shapeDef.SetAsBox(this.Width, this.Height);
     shapeDef.Density = this.Density;
     shapeDef.Friction = friction;
     shapeDef.Restitution = 0.3f;
     this.body.CreateShape(shapeDef);
     this.body.SetMassFromShapes();
 }
Example #17
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);
        }
Example #18
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);
                }
            }
        }
Example #19
0
 protected override void CreatePhysics(World world, float positionX, float positionY, float friction)
 {
     var bodyDef = new BodyDef();
     bodyDef.Position.Set(positionX, positionY);
     bodyDef.LinearDamping = 0.3f;
     bodyDef.AngularDamping = 0.6f;
     var circleDef = new CircleDef();
     circleDef.Density = this.Density;
     circleDef.Radius = this.Radius * 0.5f;
     circleDef.Friction = friction;
     circleDef.Restitution = 0.3f;
     this.body = world.CreateBody(bodyDef);
     this.body.CreateShape(circleDef);
     this.body.SetMassFromShapes();
 }
Example #20
0
        public PhysicManager()
        {
            // Define the ground body.
            BodyDef groundBodyDef = new BodyDef();
            groundBodyDef.Position.Set(0.0f, -10.0f);

            // Call the body factory which  creates the ground box shape.
            // The body is also added to the world.
            Body groundBody = world.CreateBody(groundBodyDef);

            // Define the ground box shape.
            PolygonDef groundShapeDef = new PolygonDef();

            // The extents are the half-widths of the box.
            groundShapeDef.SetAsBox(50.0f, 10.0f);

            // Add the ground shape to the ground body.
            groundBody.CreateShape(groundShapeDef);

            // Define the dynamic body. We set its position and call the body factory.
            BodyDef bodyDef = new BodyDef();
            bodyDef.Position.Set(0.0f, 4.0f);
            Body body = world.CreateBody(bodyDef);

            // Define another box shape for our dynamic body.
            PolygonDef shapeDef = new PolygonDef();
            shapeDef.SetAsBox(1.0f, 1.0f);

            // Set the box density to be non-zero, so it will be dynamic.
            shapeDef.Density = 1.0f;

            // Override the default friction.
            shapeDef.Friction = 0.3f;

            // Add the shape to the body.
            body.CreateShape(shapeDef);

            // Now tell the dynamic body to compute it's mass properties base
            // on its shape.
            body.SetMassFromShapes();

            // Prepare for simulation. Typically we use a time step of 1/60 of a
            // second (60Hz) and 10 iterations. This provides a high quality simulation
            // in most game scenarios.

            world.SetDebugDraw(new OpenGLDebugDraw());
        }
Example #21
0
		public Pulleys()
		{
			Body ground = null;
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(50.0f, 10.0f);

				BodyDef bd = new BodyDef();
				bd.Position.Set(0.0f, -10.0f);
				ground = _world.CreateBody(bd);
				ground.CreateShape(sd);
			}

			{
				float a = 2.0f;
				float b = 4.0f;
				float y = 16.0f;
				float L = 12.0f;

				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(a, b);
				sd.Density = 5.0f;

				BodyDef bd = new BodyDef();

				bd.Position.Set(-10.0f, y);
				Body body1 = _world.CreateBody(bd);
				body1.CreateShape(sd);
				body1.SetMassFromShapes();

				bd.Position.Set(10.0f, y);
				Body body2 = _world.CreateBody(bd);
				body2.CreateShape(sd);
				body2.SetMassFromShapes();

				PulleyJointDef pulleyDef = new PulleyJointDef();
				Vec2 anchor1 = new Vec2(-10.0f, y + b);
				Vec2 anchor2 = new Vec2(10.0f, y + b);
				Vec2 groundAnchor1 = new Vec2(-10.0f, y + b + L);
				Vec2 groundAnchor2 = new Vec2(10.0f, y + b + L);
				pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 2.0f);

				_joint1 = (PulleyJoint)_world.CreateJoint(pulleyDef);
			}
		}
Example #22
0
		BipedTest()
		{
			const float k_restitution = 1.4f;

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

				PolygonDef sd = new PolygonDef();
				sd.Density = 0.0f;
				sd.Restitution = k_restitution;

				sd.SetAsBox(0.1f, 10.0f, new Vec2(-10.0f, 0.0f), 0.0f);
				body.CreateShape(sd);

				sd.SetAsBox(0.1f, 10.0f, new Vec2(10.0f, 0.0f), 0.0f);
				body.CreateShape(sd);

				sd.SetAsBox(0.1f, 10.0f, new Vec2(0.0f, -10.0f), 0.5f * Box2DX.Common.Settings.Pi);
				body.CreateShape(sd);

				sd.SetAsBox(0.1f, 10.0f, new Vec2(0.0f, 10.0f), -0.5f * Box2DX.Common.Settings.Pi);
				body.CreateShape(sd);
			}

			_biped = new Biped(_world, new Vec2(0.0f, 20.0f));

			for (int i = 0; i < 8; ++i)
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(5.0f, 20.0f + i);
				bd.IsBullet = true;
				Body body = _world.CreateBody(bd);
				body.SetLinearVelocity(new Vec2(0.0f, -100.0f));
				body.SetAngularVelocity(Box2DX.Common.Math.Random(-50.0f, 50.0f));

				CircleDef sd = new CircleDef();
				sd.Radius = 0.25f;
				sd.Density = 15.0f;
				sd.Restitution = k_restitution;
				body.CreateShape(sd);
				body.SetMassFromShapes();
			}
		}
Example #23
0
		public Buoyancy()
		{
			BuoyancyController bc = _bc;
			_world.AddController(bc);

			bc.offset = 15;
			bc.normal.Set(0, 1);
			bc.density = 2;
			bc.linearDrag = 2;
			bc.angularDrag = 1;

			for (int i = 0; i < 2; ++i)
			{
				PolygonDef sd = new PolygonDef();
				sd.VertexCount = 3;
				sd.Vertices[0].Set(-0.5f, 0.0f);
				sd.Vertices[1].Set(0.5f, 0.0f);
				sd.Vertices[2].Set(0.0f, 1.5f);
				sd.Density = 1.0f;

				BodyDef bd = new BodyDef();
				bd.Position.Set(-8.0f + 8.0f * i, 12.0f);
				Body body = _world.CreateBody(bd);
				body.CreateShape(sd);
				body.SetMassFromShapes();

				bc.AddBody(body);
			}

			for (int i = 0; i < 3; ++i)
			{
				CircleDef sd = new CircleDef();
				sd.Radius = 0.5f;
				sd.Density = 1.0f;

				BodyDef bd = new BodyDef();
				bd.Position.Set(-6.0f + 6.0f * i, 10.0f);
				Body body = _world.CreateBody(bd);
				body.CreateShape(sd);
				body.SetMassFromShapes();

				bc.AddBody(body);
			}
		}
Example #24
0
		public SimpleTest()
		{
			// Define the ground body.
			BodyDef groundBodyDef = new BodyDef();
			groundBodyDef.Position.Set(0.0f, -10.0f);

			// Call the body factory which creates the ground box shape.
			// The body is also added to the world.
			Body groundBody = _world.CreateBody(groundBodyDef);

			// Define the ground box shape.
			PolygonDef groundShapeDef = new PolygonDef();

			// The extents are the half-widths of the box.
			groundShapeDef.SetAsBox(50.0f, 10.0f);

			// Add the ground shape to the ground body.
			groundBody.CreateShape(groundShapeDef);

			for (int i = 0; i < 1; i++)
			{
				// Define the dynamic body. We set its position and call the body factory.
				BodyDef bodyDef = new BodyDef();
				bodyDef.Position.Set(0.0f, 4.0f *(i+1));
				Body body = _world.CreateBody(bodyDef);

				// Define another box shape for our dynamic body.
				PolygonDef shapeDef = new PolygonDef();
				shapeDef.SetAsBox(1.0f, 1.0f);
				
				// Set the box density to be non-zero, so it will be dynamic.
				shapeDef.Density = 1.0f;

				// Override the default friction.
				shapeDef.Friction = 0.3f;

				// Add the shape to the body.
				body.CreateShape(shapeDef);

				// Now tell the dynamic body to compute it's mass properties base
				// on its shape.
				body.SetMassFromShapes();
			}
		}
Example #25
0
		public Prismatic()
		{
			Body ground = null;
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(50.0f, 10.0f);

				BodyDef bd = new BodyDef();
				bd.Position.Set(0.0f, -10.0f);
				ground = _world.CreateBody(bd);
				ground.CreateShape(sd);
			}

			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(2.0f, 0.5f);
				sd.Density = 5.0f;
				sd.Friction = 0.05f;

				BodyDef bd = new BodyDef();
				bd.Position.Set(-10.0f, 10.0f);
				bd.Angle = 0.5f * Box2DX.Common.Settings.Pi;
				Body body = _world.CreateBody(bd);
				body.CreateShape(sd);
				body.SetMassFromShapes();

				PrismaticJointDef pjd = new PrismaticJointDef();

				// Bouncy limit
				pjd.Initialize(ground, body, new Vec2(0.0f, 0.0f), new Vec2(1.0f, 0.0f));

				// Non-bouncy limit
				//pjd.Initialize(ground, body, Vec2(-10.0f, 10.0f), Vec2(1.0f, 0.0f));

				pjd.MotorSpeed = 10.0f;
				pjd.MaxMotorForce = 1000.0f;
				pjd.EnableMotor = true;
				pjd.LowerTranslation = 0.0f;
				pjd.UpperTranslation = 20.0f;
				pjd.EnableLimit = true;

				_joint = (PrismaticJoint)_world.CreateJoint(pjd);
			}
		}
Example #26
0
		public Revolute()
		{
			Body ground = null;
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(50.0f, 10.0f);

				BodyDef bd = new BodyDef();
				bd.Position.Set(0.0f, -10.0f);
				ground = _world.CreateBody(bd);
				ground.CreateShape(sd);
			}

			{
				CircleDef sd = new CircleDef();
				sd.Radius = 0.5f;
				sd.Density = 5.0f;

				BodyDef bd = new BodyDef();

				RevoluteJointDef rjd = new RevoluteJointDef();

				bd.Position.Set(0.0f, 20.0f);
				Body body = _world.CreateBody(bd);
				body.CreateShape(sd);
				body.SetMassFromShapes();

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

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

				_joint = (RevoluteJoint)_world.CreateJoint(rjd);
			}
		}
Example #27
0
		public Bridge()
		{
			Body ground = null;
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(50.0f, 10.0f);

				BodyDef bd = new BodyDef();
				bd.Position.Set(0.0f, -10.0f);
				ground = _world.CreateBody(bd);
				ground.CreateShape(sd);
			}

			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(0.5f, 0.125f);
				sd.Density = 20.0f;
				sd.Friction = 0.2f;

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

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

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

					prevBody = body;
				}

				Vec2 anchor_ = new Vec2(-15.0f + 1.0f * numPlanks, 5.0f);
				jd.Initialize(prevBody, ground, anchor_);
				_world.CreateJoint(jd);
			}
		}
Example #28
0
		public Pyramid()
		{
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(50.0f, 10.0f);

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

			{
				PolygonDef sd = new PolygonDef();
				float a = 0.5f;
				sd.SetAsBox(a, a);
				sd.Density = 5.0f;

				Vec2 x = new Vec2(-10.0f, 0.75f);
				Vec2 y;
				Vec2 deltaX = new Vec2(0.5625f, 2.0f);
				Vec2 deltaY = new Vec2(1.125f, 0.0f);

				for (int i = 0; i < 25; ++i)
				{
					y = x;

					for (int j = i; j < 25; ++j)
					{
						BodyDef bd = new BodyDef();
						bd.Position = y;
						Body body = _world.CreateBody(bd);
						body.CreateFixture(sd);
						body.SetMassFromShapes();

						y += deltaY;
					}

					x += deltaX;
				}
			}
		}
Example #29
0
		public VerticalStack()
		{
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(50.0f, 10.0f, new Vec2(0.0f, -10.0f), 0.0f);

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

				sd.SetAsBox(0.1f, 10.0f, new Vec2(20.0f, 10.0f), 0.0f);
				ground.CreateShape(sd);
			}

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

			for (int j = 0; j < 5; ++j)
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(0.5f, 0.5f);
				sd.Density = 1.0f;
				sd.Friction = 0.3f;

				for (int i = 0; i < 12; ++i)
				{
					BodyDef bd = new BodyDef();

					// For this test we are using continuous physics for all boxes.
					// This is a stress test, you normally wouldn't do this for
					// performance reasons.
					bd.AllowSleep = true;
					bd.Position.Set(xs[j], 0.752f + 1.54f * i);
					Body body = _world.CreateBody(bd);

					body.CreateShape(sd);
					body.SetMassFromShapes();
				}
			}

			_bullet = null;
		}
Example #30
0
        public FrictionTest()
        {
            // Define the ground body.
            BodyDef groundBodyDef = new BodyDef();
            groundBodyDef.Position.Set(0.0f, -10.0f);

            // Call the body factory which creates the ground box shape.
            // The body is also added to the world.
            Body groundBody = _world.CreateBody(groundBodyDef);

            // Define the ground box shape.
            PolygonDef groundShapeDef = new PolygonDef();

            // The extents are the half-widths of the box.
            groundShapeDef.SetAsBox(50.0f, 10.0f);

            // Add the ground shape to the ground body.
            groundBody.CreateFixture(groundShapeDef);

            // Define the dynamic body. We set its position and call the body factory.
            BodyDef bodyDef = new BodyDef();
            bodyDef.Position.Set(1.0f, 2.0f);
            Body bodyfast = _world.CreateBody(bodyDef);

            // Define another box shape for our dynamic body.
            PolygonDef shapeDef = new PolygonDef();
            shapeDef.SetAsBox(1.0f, 1.0f);
            shapeDef.Density = 1.0f;
            shapeDef.Friction = 0.3f;
            bodyfast.CreateFixture(shapeDef);
            bodyfast.SetMassFromShapes();

            bodyDef.Position.Set(-1.0f, 2.0f);
            Body bodyslow = _world.CreateBody(bodyDef);
            shapeDef.Friction = 0.8f;
            bodyslow.CreateFixture(shapeDef);
            bodyslow.SetMassFromShapes();

            bodyslow.SetLinearVelocity(new Vec2(-3, 0));
            bodyfast.SetLinearVelocity(new Vec2(3, 0));
        }
Example #31
0
        internal Body(BodyDef bd, World world)
        {
            Box2DXDebug.Assert(world._lock == false);

            _flags = 0;

            if (bd.IsBullet)
            {
                _flags |= BodyFlags.Bullet;
            }
            if (bd.FixedRotation)
            {
                _flags |= BodyFlags.FixedRotation;
            }
            if (bd.AllowSleep)
            {
                _flags |= BodyFlags.AllowSleep;
            }
            if (bd.IsSleeping)
            {
                _flags |= BodyFlags.Sleep;
            }

            _world = world;

            _xf.position = bd.Position;
            _xf.rotation = Box2DX.Common.Math.AngleToRotation(bd.Angle);
            //_xf.R = new Mat22(bd.Angle);

            _sweep.LocalCenter = bd.MassData.Center;
            _sweep.T0          = 1.0f;
            _sweep.A0          = _sweep.A = bd.Angle;
            _sweep.C0          = _sweep.C = _xf.TransformPoint(_sweep.LocalCenter);

            //_jointList = null;
            //_contactList = null;
            //_controllerList = null;
            //_prev = null;
            //_next = null;

            _linearVelocity  = bd.LinearVelocity;
            _angularVelocity = bd.AngularVelocity;

            _linearDamping  = bd.LinearDamping;
            _angularDamping = bd.AngularDamping;

            //_force.Set(0.0f, 0.0f);
            //_torque = 0.0f;

            //_linearVelocity.SetZero();
            //_angularVelocity = 0.0f;

            //_sleepTime = 0.0f;

            //_invMass = 0.0f;
            //_I = 0.0f;
            //_invI = 0.0f;

            _mass = bd.MassData.Mass;

            if (_mass > 0.0f)
            {
                _invMass = 1.0f / _mass;
            }

            _I = bd.MassData.I;

            if (_I > 0.0f && (_flags & BodyFlags.FixedRotation) == 0)
            {
                _invI = 1.0f / _I;
            }

            if (_invMass == 0.0f && _invI == 0.0f)
            {
                _type = BodyType.Static;
            }
            else
            {
                _type = BodyType.Dynamic;
            }

            _userData = bd.UserData;

            //_fixtureList = null;
            //_fixtureCount = 0;

            bodyID = bodyCount++;
        }
Example #32
0
 internal bool _useGravity; // STEVE Added
 internal Body(BodyDef bd, World world)
 {
     Box2DXDebug.Assert(!world._lock);
     this._flags = (Body.BodyFlags) 0;
     if (bd.IsBullet)
     {
         this._flags |= Body.BodyFlags.Bullet;
     }
     if (bd.FixedRotation)
     {
         this._flags |= Body.BodyFlags.FixedRotation;
     }
     if (bd.AllowSleep)
     {
         this._flags |= Body.BodyFlags.AllowSleep;
     }
     if (bd.IsSleeping)
     {
         this._flags |= Body.BodyFlags.Sleep;
     }
     this._world       = world;
     this._xf.Position = bd.Position;
     this._xf.R.Set(bd.Angle);
     this._sweep.LocalCenter = bd.MassData.Center;
     this._sweep.T0          = 1f;
     this._sweep.A0          = (this._sweep.A = bd.Angle);
     this._sweep.C0          = (this._sweep.C = Box2DX.Common.Math.Mul(this._xf, this._sweep.LocalCenter));
     this._jointList         = null;
     this._contactList       = null;
     this._prev           = null;
     this._next           = null;
     this._linearDamping  = bd.LinearDamping;
     this._angularDamping = bd.AngularDamping;
     this._force.Set(0f, 0f);
     this._torque = 0f;
     this._linearVelocity.SetZero();
     this._angularVelocity = 0f;
     this._sleepTime       = 0f;
     this._invMass         = 0f;
     this._I    = 0f;
     this._invI = 0f;
     this._mass = bd.MassData.Mass;
     if (this._mass > 0f)
     {
         this._invMass = 1f / this._mass;
     }
     if ((this._flags & Body.BodyFlags.FixedRotation) == (Body.BodyFlags) 0)
     {
         this._I = bd.MassData.I;
     }
     if (this._I > 0f)
     {
         this._invI = 1f / this._I;
     }
     if (this._invMass == 0f && this._invI == 0f)
     {
         this._type = Body.BodyType.Static;
     }
     else
     {
         this._type = Body.BodyType.Dynamic;
     }
     this._userData   = bd.UserData;
     this._shapeList  = null;
     this._shapeCount = 0;
     this._gravity    = _world.Gravity; // STEVE Added
     this._useGravity = false;          // STEVE Added
 }