Exemplo n.º 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.
        /// If the Density is non-zero, this function automatically updates the mass of the body.
        /// Contacts are not created until the next time step.
        /// @param def the fixture definition.
        /// @warning This function is locked during callbacks.
        public Fixture CreateFixture(FixtureDef def)
        {
            Utilities.Assert(m_world.IsLocked() == false);
            if (m_world.IsLocked() == true)
            {
                return(null);
            }

            Fixture fixture = new Fixture();

            fixture.Create(this, def);

            if (m_flags.HasFlag(BodyFlags.e_activeFlag))
            {
                BroadPhase broadPhase = m_world.m_contactManager.m_broadPhase;
                fixture.CreateProxies(broadPhase, m_xf);
            }

            m_fixtureList.Add(fixture);

            fixture.m_body = this;

            // Adjust mass properties if needed.
            if (fixture.m_Density > 0.0f)
            {
                ResetMassData();
            }

            // 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.
            m_world.m_flags |= WorldFlags.e_newFixture;

            return(fixture);
        }
Exemplo n.º 2
0
		public override void Keyboard()
		{
			if (KeyboardManager.IsPressed(Key.Comma)) {
				if (m_bullet != null)
				{
					m_world.DestroyBody(m_bullet);
					m_bullet = null;
				}

				{
					CircleShape shape = new CircleShape();
					shape.m_radius = 0.25f;

					FixtureDef fd = new FixtureDef();
					fd.shape = shape;
					fd.Density = 20.0f;
					fd.restitution = 0.05f;

					BodyDef bd = new BodyDef();
					bd.type = BodyType._dynamicBody;
					bd.bullet = true;
					bd.Position.Set(-31.0f, 5.0f);

					m_bullet = m_world.CreateBody(bd);
					m_bullet.CreateFixture(fd);

					m_bullet.SetLinearVelocity(new Vec2(400.0f, 0.0f));
				}
			}
		}
Exemplo n.º 3
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.
		/// If the Density is non-zero, this function automatically updates the mass of the body.
		/// Contacts are not created until the next time step.
		/// @param def the fixture definition.
		/// @warning This function is locked during callbacks.
		public Fixture CreateFixture(FixtureDef def){
			Utilities.Assert(m_world.IsLocked() == false);
			if (m_world.IsLocked() == true)
			{
			    return null;
			}

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

			if (m_flags.HasFlag(BodyFlags.e_activeFlag))
			{
			    BroadPhase broadPhase = m_world.m_contactManager.m_broadPhase;
			    fixture.CreateProxies(broadPhase, m_xf);
			}

			m_fixtureList.Add(fixture);

			fixture.m_body = this;

			// Adjust mass properties if needed.
			if (fixture.m_Density > 0.0f)
			{
			    ResetMassData();
			}

			// 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.
			m_world.m_flags |= WorldFlags.e_newFixture;

			return fixture;
		}
Exemplo n.º 4
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++).
        internal void Create(Body body, 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();

            // Reserve proxy space
            int childCount = _shape.GetChildCount();

            _proxies = new FixtureProxy[childCount];
            for (int i = 0; i < childCount; ++i)
            {
                _proxies[i]         = new FixtureProxy();
                _proxies[i].fixture = null;
                _proxies[i].proxyId = BroadPhase.NullProxy;
            }
            _proxyCount = 0;

            _density = def.density;
        }
Exemplo n.º 5
0
		/// Creates a fixture from a shape and attach it to this body.
		/// This is a convenience function. Use FixtureDef if you need to set parameters
		/// like friction, restitution, user data, or filtering.
		/// If the Density is non-zero, 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){
			FixtureDef def = new FixtureDef();
			def.shape = shape.Clone();
			def.Density = shape.Density;
			def.Filter = shape.Filter;

			return CreateFixture(def);
		}
Exemplo n.º 6
0
        /// Creates a fixture from a shape and attach it to this body.
        /// This is a convenience function. Use FixtureDef if you need to set parameters
        /// like friction, restitution, user data, or filtering.
        /// If the density is non-zero, 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));
        }
Exemplo n.º 7
0
        /// Creates a fixture from a shape and attach it to this body.
        /// This is a convenience function. Use FixtureDef if you need to set parameters
        /// like friction, restitution, user data, or filtering.
        /// If the Density is non-zero, 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)
        {
            FixtureDef def = new FixtureDef();

            def.shape   = shape.Clone();
            def.Density = shape.Density;
            def.Filter  = shape.Filter;

            return(CreateFixture(def));
        }
Exemplo n.º 8
0
		public SensorTest()
		{
			{
				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);
				}

	#if ZERO
				{
					FixtureDef sd;
					sd.SetAsBox(10.0f, 2.0f, new Vec2(0.0f, 20.0f), 0.0f);
					sd.IsSensor = true;
					m_sensor = ground.CreateFixture(sd);
				}
	#else
				{
					CircleShape shape = new CircleShape();
					shape.m_radius = 5.0f;
					shape.m_p.Set(0.0f, 10.0f);

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

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

				for (int i = 0; i < e_count; ++i)
				{
					BodyDef bd = new BodyDef();
					bd.type = BodyType._dynamicBody;
					bd.Position.Set(-10.0f + 3.0f * i, 20.0f);
					bd.UserData = m_touching[i];

					m_touching[i] = false;
					m_bodies[i] = m_world.CreateBody(bd);

					m_bodies[i].CreateFixture(shape);
				}
			}
		}
Exemplo n.º 9
0
			//e_columnCount = 1,
			//e_rowCount = 1

		public VerticalStack()
		{
			{
				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);

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

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

			for (int j = 0; j < e_columnCount; ++j)
			{
				PolygonShape shape = new PolygonShape();
				shape.SetAsBox(0.5f, 0.5f);

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

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

					int n = j * e_rowCount + i;
					Utilities.Assert(n < e_rowCount * e_columnCount);
					m_indices[n] = n;
					bd.UserData = m_indices[n];

					float x = 0.0f;
					//float x = RandomFloat(-0.02f, 0.02f);
					//float x = i % 2 == 0 ? -0.025f : 0.025f;
					bd.Position.Set(xs[j] + x, 0.752f + 1.54f * i);
					Body body = m_world.CreateBody(bd);

					m_bodies[n] = body;

					body.CreateFixture(fd);
				}
			}

			m_bullet = null;
		}
Exemplo n.º 10
0
		public Confined()
		{
			{
				BodyDef bd = new BodyDef();
				Body ground = m_world.CreateBody(bd);

				EdgeShape shape2 = new EdgeShape();

				// Floor
				shape2.Set(new Vec2(-10.0f, 0.0f), new Vec2(10.0f, 0.0f));
				shape2.Density = 0;
				ground.CreateFixture(shape2);

				// Left wall
				shape2.Set(new Vec2(-10.0f, 0.0f), new Vec2(-10.0f, 20.0f));
				ground.CreateFixture(shape2);

				// Right wall
				shape2.Set(new Vec2(10.0f, 0.0f), new Vec2(10.0f, 20.0f));
				ground.CreateFixture(shape2);

				// Roof
				shape2.Set(new Vec2(-10.0f, 20.0f), new Vec2(10.0f, 20.0f));
				ground.CreateFixture(shape2);
			}

			float radius = 0.5f;
			CircleShape shape = new CircleShape();
			shape.m_p.SetZero();
			shape.m_radius = radius;

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

			for (int j = 0; j < e_columnCount; ++j)
			{
				for (int i = 0; i < e_rowCount; ++i)
				{
					BodyDef bd = new BodyDef();
					bd.type = BodyType._dynamicBody;
					bd.Position.Set(-10.0f + (2.1f * j + 1.0f + 0.01f * i) * radius, (2.0f * i + 1.0f) * radius);
					Body body = m_world.CreateBody(bd);

					body.CreateFixture(fd);
				}
			}

			m_world.SetGravity(new Vec2(0.0f, 0.0f));
		}
Exemplo n.º 11
0
		public GravityTest() {
			// Define the gravity vector.
			Vec2 gravity = new Vec2(0.0f, 10.0f);

			// Construct a world object, which will hold and simulate the rigid bodies.
			m_world.SetGravity(gravity);

			// Define the ground body.
			BodyDef groundBodyDef = new BodyDef();
			groundBodyDef.Position.Set(0.0f, 20.0f);

			// Call the body factory which allocates memory for the ground body
			// from a pool and creates the ground box shape (also from a pool).
			// The body is also added to the world.
			Body groundBody = m_world.CreateBody(groundBodyDef);

			// Define the ground box shape.
			PolygonShape groundBox = new PolygonShape();

			// The extents are the half-widths of the box.
			groundBox.SetAsBox(50.0f, 10.0f);
			groundBox.Density = 0;

			// Add the ground fixture to the ground body.
			groundBody.CreateFixture(groundBox);

			// Define the dynamic body. We set its position and call the body factory.
			BodyDef bodyDef = new BodyDef();
			bodyDef.type = BodyType._dynamicBody;
			bodyDef.Position = new Vec2(0.0f, 4.0f);
			Body body = m_world.CreateBody(bodyDef);

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

			// Define the dynamic body fixture.
			FixtureDef fixtureDef = new FixtureDef();
			fixtureDef.shape = dynamicBox;

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

			// Override the default friction.
			fixtureDef.friction = 0.3f;

			// Add the shape to the body.
			body.CreateFixture(fixtureDef);
		}
Exemplo n.º 12
0
		public Chain()
		{
			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);
			}

			{
				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.type = BodyType._dynamicBody;
					bd.Position.Set(0.5f + i, y);
					Body body = m_world.CreateBody(bd);
					body.CreateFixture(fd);

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

					prevBody = body;
				}
			}
		}
Exemplo n.º 13
0
		public MotorJointTest()
		{
			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				ground = m_world.CreateBody(bd);

				EdgeShape shape = new EdgeShape();
				shape.Set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));

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

				ground.CreateFixture(fd);
			}

			// Define motorized body
			{
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(0.0f, 8.0f);
				Body body = m_world.CreateBody(bd);

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

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.friction = 0.6f;
				fd.Density = 2.0f;
				body.CreateFixture(fd);

				MotorJointDef mjd = new MotorJointDef();
				mjd.Initialize(ground, body);
				mjd.maxForce = 1000.0f;
				mjd.maxTorque = 1000.0f;
				m_joint = (MotorJoint)m_world.CreateJoint(mjd);
			}

			m_go = false;
			m_time = 0.0f;
		}
Exemplo n.º 14
0
		public ConveyorBelt()
		{
			// Ground
			{
				BodyDef bd = new BodyDef();
				Body ground = m_world.CreateBody(bd);

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

			// Platform
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(-5.0f, 5.0f);
				Body body = m_world.CreateBody(bd);

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

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.friction = 0.8f;
				m_platform = body.CreateFixture(fd);
			}

			// Boxes
			for (int i = 0; i < 5; ++i)
			{
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(-10.0f + 2.0f * i, 7.0f);
				Body body = m_world.CreateBody(bd);

				PolygonShape shape = new PolygonShape();
				shape.SetAsBox(0.5f, 0.5f);
				shape.Density = 20;
				body.CreateFixture(shape);
			}
		}
Exemplo n.º 15
0
		void CreateCircle()
		{
			float radius = 2.0f;
			CircleShape shape = new CircleShape();
			shape.m_p.SetZero();
			shape.m_radius = radius;

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

			Vec2 p = new Vec2(RandomFloat(), 3.0f + RandomFloat());
			BodyDef bd = new BodyDef();
			bd.type = BodyType._dynamicBody;
			bd.Position = p;
			//bd.allowSleep = false;
			Body body = m_world.CreateBody(bd);

			body.CreateFixture(fd);
		}
Exemplo n.º 16
0
        // We need separation create/destroy functions from the constructor/destructor because
        // the destructor cannot access the allocator (no destructor arguments allowed by C++).
        internal void Create(Body body, FixtureDef def)
        {
            m_userData    = def.UserData;
            m_friction    = def.friction;
            m_restitution = def.restitution;

            m_body = body;
            m_next = null;

            m_filter = def.Filter;

            m_isSensor = def.IsSensor;

            m_shape = def.shape.Clone();

            // Reserve proxy space
            int childCount = m_shape.GetChildCount();

            m_proxies = new List <FixtureProxy>();

            m_Density = def.Density;
        }
Exemplo n.º 17
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.
        /// If the density is non-zero, this function automatically updates the mass of the body.
        /// Contacts are not created until the next time step.
        /// @param def the fixture definition.
        /// @warning This function is locked during callbacks.
        public Fixture CreateFixture(FixtureDef def)
        {
            //Debug.Assert(_world.IsLocked == false);
            if (_world.IsLocked == true)
            {
                return(null);
            }

            Fixture fixture = new Fixture();

            fixture.Create(this, def);

            if ((_flags & BodyFlags.Active) == BodyFlags.Active)
            {
                BroadPhase broadPhase = _world._contactManager._broadPhase;
                fixture.CreateProxies(broadPhase, ref _xf);
            }

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

            fixture._body = this;


            // Adjust mass properties if needed.
            if (fixture._density > 0.0f)
            {
                ResetMassData();
            }

            // 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 |= WorldFlags.NewFixture;

            return(fixture);
        }
Exemplo n.º 18
0
		public VaryingRestitution()
		{
			{
				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);
				
			}

			{
				CircleShape shape = new CircleShape();
				shape.m_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.type = BodyType._dynamicBody;
					bd.Position.Set(-10.0f + 3.0f * i, 20.0f);

					Body body = m_world.CreateBody(bd);

					fd.restitution = restitution[i];
					body.CreateFixture(fd);
				}
			}
		}
Exemplo n.º 19
0
		public BodyTypes()
		{
			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				ground = m_world.CreateBody(bd);

				EdgeShape shape = new EdgeShape();
				shape.Set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));

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

				ground.CreateFixture(fd);
			}

			// Define attachment
			{
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(0.0f, 3.0f);
				m_attachment = m_world.CreateBody(bd);

				PolygonShape shape = new PolygonShape();
				shape.SetAsBox(0.5f, 2.0f);
				shape.Density = 2;
				m_attachment.CreateFixture(shape);
			}

			// Define platform
			{
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(-4.0f, 5.0f);
				m_platform = m_world.CreateBody(bd);

				PolygonShape shape = new PolygonShape();
				shape.SetAsBox(0.5f, 4.0f, new Vec2(4.0f, 0.0f), 0.5f * (float)Math.PI);

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.friction = 0.6f;
				fd.Density = 2.0f;
				m_platform.CreateFixture(fd);

				RevoluteJointDef rjd = new RevoluteJointDef();
				rjd.Initialize(m_attachment, m_platform, new Vec2(0.0f, 5.0f));
				rjd.maxMotorTorque = 50.0f;
				rjd.enableMotor = true;
				m_world.CreateJoint(rjd);

				PrismaticJointDef pjd = new PrismaticJointDef();
				pjd.Initialize(ground, m_platform, new Vec2(0.0f, 5.0f), new Vec2(1.0f, 0.0f));

				pjd.maxMotorForce = 1000.0f;
				pjd.enableMotor = true;
				pjd.lowerTranslation = -10.0f;
				pjd.upperTranslation = 10.0f;
				pjd.enableLimit = true;

				m_world.CreateJoint(pjd);

				m_speed = 3.0f;
			}

			// Create a payload
			{
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(0.0f, 8.0f);
				Body body = m_world.CreateBody(bd);

				PolygonShape shape = new PolygonShape();
				shape.SetAsBox(0.75f, 0.75f);

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.friction = 0.6f;
				fd.Density = 2.0f;

				body.CreateFixture(fd);
			}
		}
Exemplo n.º 20
0
		// This is a simple example of building and running a simulation
		// using Box2D. Here we create a large ground box and a small dynamic
		// box.
		// There are no graphics for this example. Box2D is meant to be used
		// with your rendering engine in your game engine.
		public static int Main(string[] args)
		{
			// Define the gravity vector.
			Vec2 gravity = new Vec2(0.0f, -10.0f);

			// Construct a world object, which will hold and simulate the rigid bodies.
			World world = new World(gravity);

			// Define the ground body.
			BodyDef groundBodyDef = new BodyDef();
			groundBodyDef.Position.Set(0.0f, -10.0f);

			// Call the body factory which allocates memory for the ground body
			// from a pool and creates the ground box shape (also from a pool).
			// The body is also added to the world.
			Body groundBody = world.CreateBody(groundBodyDef);

			// Define the ground box shape.
			PolygonShape groundBox = new PolygonShape();

			// The extents are the half-widths of the box.
			groundBox.SetAsBox(50.0f, 10.0f);
			groundBox.Density = 0;

			// Add the ground fixture to the ground body.
			groundBody.CreateFixture(groundBox);

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

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

			// Define the dynamic body fixture.
			FixtureDef fixtureDef = new FixtureDef();
			fixtureDef.shape = dynamicBox;

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

			// Override the default friction.
			fixtureDef.friction = 0.3f;

			// Add the shape to the body.
			body.CreateFixture(fixtureDef);

			// 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.
			float timeStep = 1.0f / 60.0f;
			int velocityIterations = 6;
			int positionIterations = 2;

			// This is our little game loop.
			for (int i = 0; i < 60; ++i)
			{
				// Instruct the world to perform a single step of simulation.
				// It is generally best to keep the time step and iterations fixed.
				world.Step(timeStep, velocityIterations, positionIterations);

				// Now print the position and angle of the body.
				Vec2 position = body.GetPosition();
				float angle = body.GetAngle();

				Console.WriteLine("{0} {1} {2}", position.X.ToString("0000.00"), position.Y.ToString("0000.00"), angle.ToString("0000.00"));
			}

			// When the world destructor is called, all bodies and joints are freed. This can
			// create orphaned pointers, so be careful about your world management.
			Console.WriteLine("Done!");
			Console.ReadLine();
			return 0;
		}
Exemplo n.º 21
0
		public Pinball()
		{
			// Ground body
			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				ground = m_world.CreateBody(bd);

				Vec2[] vs = new Vec2[5];
				vs[0].Set(0.0f, -2.0f);
				vs[1].Set(8.0f, 6.0f);
				vs[2].Set(8.0f, 20.0f);
				vs[3].Set(-8.0f, 20.0f);
				vs[4].Set(-8.0f, 6.0f);

				ChainShape loop = new ChainShape();
				loop.CreateLoop(vs, 5);
				FixtureDef fd = new FixtureDef();
				fd.shape = loop;
				fd.Density = 0.0f;
				ground.CreateFixture(fd);
			}

			// Flippers
			{
				Vec2 p1 = new Vec2(-2.0f, 0.0f);
				Vec2 p2 = new Vec2(2.0f, 0.0f);

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;

				bd.Position = p1;
				Body leftFlipper = m_world.CreateBody(bd);

				bd.Position = p2;
				Body rightFlipper = m_world.CreateBody(bd);

				PolygonShape box = new PolygonShape();
				box.SetAsBox(1.75f, 0.1f);

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

				leftFlipper.CreateFixture(fd);
				rightFlipper.CreateFixture(fd);

				RevoluteJointDef jd = new RevoluteJointDef();
				jd.bodyA = ground;
				jd.localAnchorB.SetZero();
				jd.enableMotor = true;
				jd.maxMotorTorque = 1000.0f;
				jd.enableLimit = true;

				jd.motorSpeed = 0.0f;
				jd.localAnchorA = p1;
				jd.bodyB = leftFlipper;
				jd.lowerAngle = -30.0f * (float)Math.PI / 180.0f;
				jd.upperAngle = 5.0f * (float)Math.PI / 180.0f;
				m_leftJoint = (RevoluteJoint)m_world.CreateJoint(jd);

				jd.motorSpeed = 0.0f;
				jd.localAnchorA = p2;
				jd.bodyB = rightFlipper;
				jd.lowerAngle = -5.0f * (float)Math.PI / 180.0f;
				jd.upperAngle = 30.0f * (float)Math.PI / 180.0f;
				m_rightJoint = (RevoluteJoint)m_world.CreateJoint(jd);
			}

			// Circle character
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(1.0f, 15.0f);
				bd.type = BodyType._dynamicBody;
				bd.bullet = true;

				m_ball = m_world.CreateBody(bd);

				CircleShape shape = new CircleShape();
				shape.m_radius = 0.2f;

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.Density = 1.0f;
				m_ball.CreateFixture(fd);
			}

			m_button = false;
		}
Exemplo n.º 22
0
		Bridge()
		{
			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);
			}

			{
				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();

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

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

					if (i == (e_count >> 1))
					{
						m_middle = body;
					}
					prevBody = body;
				}

				Vec2 anchor2 = new Vec2(-15.0f + 1.0f * e_count, 5.0f);
				jd.Initialize(prevBody, ground, anchor2);
				m_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.type = BodyType._dynamicBody;
				bd.Position.Set(-8.0f + 8.0f * i, 12.0f);
				Body body = m_world.CreateBody(bd);
				body.CreateFixture(fd);
			}

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

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

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(-6.0f + 6.0f * i, 10.0f);
				Body body = m_world.CreateBody(bd);
				body.CreateFixture(fd);
			}
		}
Exemplo n.º 23
0
		public TheoJansen()
		{
			m_offset.Set(0.0f, 8.0f);
			m_motorSpeed = 2.0f;
			m_motorOn = true;
			Vec2 pivot = new Vec2(0.0f, 0.8f);

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

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

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

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

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

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

				Body body = m_world.CreateBody(bd);
				body.CreateFixture(shape);
			}

			// 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.type = BodyType._dynamicBody;
				bd.Position = pivot + m_offset;
				m_chassis = m_world.CreateBody(bd);
				m_chassis.CreateFixture(sd);
			}

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

				FixtureDef sd = new FixtureDef();
				sd.Density = 1.0f;
				sd.shape = shape;
				sd.Filter.GroupIndex = -1;
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position = pivot + m_offset;
				m_wheel = m_world.CreateBody(bd);
				m_wheel.CreateFixture(sd);
			}

			{
				RevoluteJointDef jd = new RevoluteJointDef();
				jd.Initialize(m_wheel, m_chassis, pivot + m_offset);
				jd.collideConnected = false;
				jd.motorSpeed = m_motorSpeed;
				jd.maxMotorTorque = 400.0f;
				jd.enableMotor = m_motorOn;
				m_motorJoint = (RevoluteJoint)m_world.CreateJoint(jd);
			}

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

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

			m_wheel.SetTransform(m_wheel.GetPosition(), 120.0f * (float)Math.PI / 180.0f);
			CreateLeg(-1.0f, wheelAnchor);
			CreateLeg(1.0f, wheelAnchor);

			m_wheel.SetTransform(m_wheel.GetPosition(), -120.0f * (float)Math.PI / 180.0f);
			CreateLeg(-1.0f, wheelAnchor);
			CreateLeg(1.0f, wheelAnchor);
		}
Exemplo n.º 24
0
		public void LaunchBomb(Vec2 position, Vec2 velocity){
			if (m_bomb != null)
			{
				m_world.DestroyBody(m_bomb);
				m_bomb = null;
			}

			BodyDef bd = new BodyDef();
			bd.type = BodyType._dynamicBody;
			bd.Position = position;
			bd.bullet = true;
			m_bomb = m_world.CreateBody(bd);
			m_bomb.SetLinearVelocity(velocity);
	
			CircleShape circle = new CircleShape();
			circle.m_radius = 0.3f;

			FixtureDef fd = new FixtureDef();
			fd.shape = circle;
			fd.Density = 20.0f;
			fd.restitution = 0.0f;
	
			Vec2 minV = position - new Vec2(0.3f,0.3f);
			Vec2 maxV = position + new Vec2(0.3f, 0.3f);
	
			AABB aabb;
			aabb.lowerBound = minV;
			aabb.upperBound = maxV;

			m_bomb.CreateFixture(fd);
		}
Exemplo n.º 25
0
		DumpShell() {
			Vec2 g = new Vec2(0, 0);
			m_world.SetGravity(g);
			List<Body> bodies = new List<Body>();
			List<Joint> joints = new List<Joint>();

			{
				BodyDef bd = new BodyDef();
				bd.type = (BodyType)2;
				bd.Position.Set(3.000000000000000e+001f, 6.909857940673828e+001f);
				bd.angle = 0.000000000000000e+000f;
				bd.linearVelocity.Set(0.000000000000000e+000f, -8.618643951416016e+001f);
				bd.angularVelocity = 0.000000000000000e+000f;
				bd.linearDamping = 0.000000000000000e+000f;
				bd.angularDamping = 0.000000000000000e+000f;
				bd.allowSleep = true;
				bd.awake = true;
				bd.fixedRotation = true;
				bd.bullet = false;
				bd.active = true;
				bd.gravityScale = 1.000000000000000e+000f;
				bodies.Add(m_world.CreateBody(bd));
				{
					FixtureDef fd = new FixtureDef();
					fd.friction = 6.000000238418579e-001f;
					fd.restitution = 0.000000000000000e+000f;
					fd.Density = 5.000000000000000e-001f;
					fd.IsSensor = false;
					fd.Filter.CategoryBits = (ushort)(1);
					fd.Filter.MaskBits = (ushort)(2);
					fd.Filter.GroupIndex = (short)(0);
					PolygonShape shape = new PolygonShape();
					Vec2[] vs = new Vec2[8];
					vs[0].Set(-1.950000047683716e+000f, -4.750000000000000e+000f);
					vs[1].Set(1.950000047683716e+000f, -4.750000000000000e+000f);
					vs[2].Set(1.950000047683716e+000f, 4.750000000000000e+000f);
					vs[3].Set(-1.950000047683716e+000f, 4.750000000000000e+000f);
					shape.Set(vs, 4);
		
					fd.shape = shape;
		
					bodies[0].CreateFixture(fd);
				}
			}
		
			{
				BodyDef bd = new BodyDef();
				bd.type = (BodyType)(0);
				bd.Position.Set(5.120000457763672e+001f, 7.580000305175781e+001f);
				bd.angle = 0.000000000000000e+000f;
				bd.linearVelocity.Set(0.000000000000000e+000f, 0.000000000000000e+000f);
				bd.angularVelocity = 0.000000000000000e+000f;
				bd.linearDamping = 0.000000000000000e+000f;
				bd.angularDamping = 0.000000000000000e+000f;
				bd.allowSleep = true;
				bd.awake = true;
				bd.fixedRotation = false;
				bd.bullet = false;
				bd.active = true;
				bd.gravityScale = 1.000000000000000e+000f;
				bodies.Add(m_world.CreateBody(bd));
				{
					FixtureDef fd = new FixtureDef();
					fd.friction = 0.000000000000000e+000f;
					fd.restitution = 0.000000000000000e+000f;
					fd.Density = 1.000000000000000e+000f;
					fd.IsSensor = false;
					fd.Filter.CategoryBits = (ushort)(2);
					fd.Filter.MaskBits = (ushort)(65535);
					fd.Filter.GroupIndex = (short)(0);
					PolygonShape shape = new PolygonShape();
					Vec2[] vs = new Vec2[8];
					vs[0].Set(-5.120000076293945e+001f, -5.000000000000000e-001f);
					vs[1].Set(5.120000076293945e+001f, -5.000000000000000e-001f);
					vs[2].Set(5.120000076293945e+001f, 5.000000000000000e-001f);
					vs[3].Set(-5.120000076293945e+001f, 5.000000000000000e-001f);
					shape.Set(vs, 4);

					fd.shape = shape;

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

			joints = null;
			bodies = null;
		}
Exemplo n.º 26
0
		public 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] = new Vec2(0, 0);
				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] = new Vec2(0, 0);
				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.type = BodyType._dynamicBody;
			bd2.type = BodyType._dynamicBody;
			bd1.Position = m_offset;
			bd2.Position = p4 + m_offset;

			bd1.angularDamping = 10.0f;
			bd2.angularDamping = 10.0f;

			Body body1 = m_world.CreateBody(bd1);
			Body body2 = m_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 + m_offset, p5 + m_offset);
			m_world.CreateJoint(djd);

			djd.Initialize(body1, body2, p3 + m_offset, p4 + m_offset);
			m_world.CreateJoint(djd);

			djd.Initialize(body1, m_wheel, p3 + m_offset, wheelAnchor + m_offset);
			m_world.CreateJoint(djd);

			djd.Initialize(body2, m_wheel, p6 + m_offset, wheelAnchor + m_offset);
			m_world.CreateJoint(djd);

			RevoluteJointDef rjd = new RevoluteJointDef();

			rjd.Initialize(body2, m_chassis, p4 + m_offset);
			m_world.CreateJoint(rjd);
		}
Exemplo n.º 27
0
		public VaryingFriction()
		{
			{
				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);
			}

			{
				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 = m_world.CreateBody(bd);
				shape.Density = 0;
				ground.CreateFixture(shape);
			}

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

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

				Body ground = m_world.CreateBody(bd);
				ground.CreateFixture(shape);
			}

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

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

				Body ground = m_world.CreateBody(bd);
				ground.CreateFixture(shape);
			}

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

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

				Body ground = m_world.CreateBody(bd);
				ground.CreateFixture(shape);
			}

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

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

				Body ground = m_world.CreateBody(bd);
				ground.CreateFixture(shape);
			}

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

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

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

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

					fd.friction = friction[i];
					body.CreateFixture(fd);
				}
			}
		}
Exemplo n.º 28
0
		public void Create(int index)
		{
			if (m_bodies[m_bodyIndex] != null)
			{
				m_world.DestroyBody(m_bodies[m_bodyIndex]);
				m_bodies[m_bodyIndex] = null;
			}

			BodyDef bd = new BodyDef();

			float x = RandomFloat(-10.0f, 10.0f);
			float y = RandomFloat(10.0f, 20.0f);
			bd.Position.Set(x, y);
			bd.angle = RandomFloat(-(float)Math.PI, (float)Math.PI);
			bd.type = BodyType._dynamicBody;

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

			m_bodies[m_bodyIndex] = m_world.CreateBody(bd);

			if (index < 4)
			{
				FixtureDef fd = new FixtureDef();
				fd.shape = m_polygons[index];
				fd.friction = 0.3f;
				fd.Density = 20.0f;
				m_bodies[m_bodyIndex].CreateFixture(fd);
			}
			else
			{
				FixtureDef fd = new FixtureDef();
				fd.shape = m_circle;
				fd.friction = 0.3f;
				fd.Density = 20.0f;
				m_bodies[m_bodyIndex].CreateFixture(fd);
			}

			m_bodyIndex = (m_bodyIndex + 1) % e_maxBodies;
		}
Exemplo n.º 29
0
		public Revolute()
		{
			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));

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				//fd.Filter.CategoryBits = 2;

				ground.CreateFixture(fd);
			}

			{
				CircleShape shape = new CircleShape();
				shape.m_radius = 0.5f;
				shape.Density = 5;

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;

				RevoluteJointDef rjd = new RevoluteJointDef();

				bd.Position.Set(-10.0f, 20.0f);
				Body body = m_world.CreateBody(bd);
				body.CreateFixture(shape);

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

				rjd.Initialize(ground, body, new Vec2(-10.0f, 12.0f));
				rjd.motorSpeed = 1.0f * (float)Math.PI;
				rjd.maxMotorTorque = 10000.0f;
				rjd.enableMotor = false;
				rjd.lowerAngle = -0.25f * (float)Math.PI;
				rjd.upperAngle = 0.5f * (float)Math.PI;
				rjd.enableLimit = true;
				rjd.collideConnected = true;

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

			{
				CircleShape circle_shape = new CircleShape();
				circle_shape.m_radius = 3.0f;

				BodyDef circle_bd = new BodyDef();
				circle_bd.type = BodyType._dynamicBody;
				circle_bd.Position.Set(5.0f, 30.0f);

				FixtureDef fd = new FixtureDef();
				fd.Density = 5.0f;
				fd.Filter.MaskBits = 1;
				fd.shape = circle_shape;

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

				PolygonShape polygon_shape = new PolygonShape();
				polygon_shape.SetAsBox(10.0f, 0.2f, new Vec2(-10.0f, 0.0f), 0.0f);
				polygon_shape.Density = 2;

				BodyDef polygon_bd = new BodyDef();
				polygon_bd.Position.Set(20.0f, 10.0f);
				polygon_bd.type = BodyType._dynamicBody;
				polygon_bd.bullet = true;
				Body polygon_body = m_world.CreateBody(polygon_bd);
				
				polygon_body.CreateFixture(polygon_shape);

				RevoluteJointDef rjd = new RevoluteJointDef();
				rjd.Initialize(ground, polygon_body, new Vec2(20.0f, 10.0f));
				rjd.lowerAngle = -0.25f * (float)Math.PI;
				rjd.upperAngle = 0.0f * (float)Math.PI;
				rjd.enableLimit = true;
				m_world.CreateJoint(rjd);
			}

			// Tests mass computation of a small object far from the origin
			{
				BodyDef bodyDef = new BodyDef();
				bodyDef.type = BodyType._dynamicBody;
				Body body = m_world.CreateBody(bodyDef);
		
				PolygonShape polyShape = new PolygonShape();
				Vec2[] verts = new Vec2[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, 3);
		
				FixtureDef polyFixtureDef = new FixtureDef();
				polyFixtureDef.shape = polyShape;
				polyFixtureDef.Density = 1;

				body.CreateFixture(polyFixtureDef);	//assertion hits inside here
			}

		}
Exemplo n.º 30
0
		public Car()
		{		
			m_hz = 4.0f;
			m_zeta = 0.7f;
			m_speed = 50.0f;

			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				ground = m_world.CreateBody(bd);

				EdgeShape shape = new EdgeShape();

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

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

				float[] hs = new float[]{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 (int i = 0; i < 10; ++i)
				{
					float y2 = hs[i];
					shape.Set(new Vec2(x, y1), new Vec2(x + dx, y2));
					ground.CreateFixture(fd);
					y1 = y2;
					x += dx;
				}

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

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

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

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

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

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

			// Teeter
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(140.0f, 1.0f);
				bd.type = BodyType._dynamicBody;
				Body body = m_world.CreateBody(bd);

				PolygonShape box = new PolygonShape();
				box.SetAsBox(10.0f, 0.25f);
				box.Density = 1;
				body.CreateFixture(box);

				RevoluteJointDef jd = new RevoluteJointDef();
				jd.Initialize(ground, body, body.GetPosition());
				jd.lowerAngle = -8.0f * (float)Math.PI / 180.0f;
				jd.upperAngle = 8.0f * (float)Math.PI / 180.0f;
				jd.enableLimit = true;
				m_world.CreateJoint(jd);

				body.ApplyAngularImpulse(100.0f, true);
			}

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

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

				RevoluteJointDef jd = new RevoluteJointDef();

				Body prevBody = ground;
				for (int i = 0; i < N; ++i)
				{
					BodyDef bd = new BodyDef();
					bd.type = BodyType._dynamicBody;
					bd.Position.Set(161.0f + 2.0f * i, -0.125f);
					Body body = m_world.CreateBody(bd);
					body.CreateFixture(fd);

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

					prevBody = body;
				}

				Vec2 anchor2 = new Vec2(160.0f + 2.0f * N, -0.125f);
				jd.Initialize(prevBody, ground, anchor2);
				m_world.CreateJoint(jd);
			}

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

				Body body = null;
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;

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

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

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

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

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

			// Car
			{
				PolygonShape chassis = new PolygonShape();
				Vec2[] vertices = new Vec2[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, 6);

				CircleShape circle = new CircleShape();
				circle.m_radius = 0.4f;

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(0.0f, 1.0f);
				m_car = m_world.CreateBody(bd);
				m_car.CreateFixture(chassis);

				FixtureDef fd = new FixtureDef();
				fd.shape = circle;
				fd.Density = 1.0f;
				fd.friction = 0.9f;

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

				bd.Position.Set(1.0f, 0.4f);
				m_wheel2 = m_world.CreateBody(bd);
				m_wheel2.CreateFixture(fd);

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

				jd.Initialize(m_car, m_wheel1, m_wheel1.GetPosition(), axis);
				jd.motorSpeed = 0.0f;
				jd.maxMotorTorque = 20.0f;
				jd.enableMotor = true;
				jd.frequencyHz = m_hz;
				jd.dampingRatio = m_zeta;
				m_spring1 = (WheelJoint)m_world.CreateJoint(jd);

				jd.Initialize(m_car, m_wheel2, m_wheel2.GetPosition(), axis);
				jd.motorSpeed = 0.0f;
				jd.maxMotorTorque = 10.0f;
				jd.enableMotor = false;
				jd.frequencyHz = m_hz;
				jd.dampingRatio = m_zeta;
				m_spring2 = (WheelJoint)m_world.CreateJoint(jd);
			}
		}
Exemplo n.º 31
0
		public CharacterCollision()
		{
			// Ground body
			{
				BodyDef bd = new BodyDef();
				Body ground = m_world.CreateBody(bd);

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

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

				EdgeShape shape = new EdgeShape();
				shape.Density = 0;
				shape.Set(new Vec2(-8.0f, 1.0f), new Vec2(-6.0f, 1.0f));
				ground.CreateFixture(shape);
				shape.Set(new Vec2(-6.0f, 1.0f), new Vec2(-4.0f, 1.0f));
				ground.CreateFixture(shape);
				shape.Set(new Vec2(-4.0f, 1.0f), new Vec2(-2.0f, 1.0f));
				ground.CreateFixture(shape);
			}

			// Chain shape
			{
				BodyDef bd = new BodyDef();
				bd.angle = 0.25f * (float)Math.PI;
				Body ground = m_world.CreateBody(bd);

				Vec2[] vs = new Vec2[4];
				vs[0].Set(5.0f, 7.0f);
				vs[1].Set(6.0f, 8.0f);
				vs[2].Set(7.0f, 8.0f);
				vs[3].Set(8.0f, 7.0f);
				ChainShape shape = new ChainShape();
				shape.CreateChain(vs, 4);
				shape.Density = 0;
				ground.CreateFixture(shape);
			}

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

				PolygonShape shape = new PolygonShape();
				shape.Density = 0;

				shape.SetAsBox(1.0f, 1.0f, new Vec2(4.0f, 3.0f), 0.0f);
				ground.CreateFixture(shape);
				shape.SetAsBox(1.0f, 1.0f, new Vec2(6.0f, 3.0f), 0.0f);
				ground.CreateFixture(shape);
				shape.SetAsBox(1.0f, 1.0f, new Vec2(8.0f, 3.0f), 0.0f);
				ground.CreateFixture(shape);
			}

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

				Vec2[] vs = new Vec2[4];
				vs[0].Set(-1.0f, 3.0f);
				vs[1].Set(1.0f, 3.0f);
				vs[2].Set(1.0f, 5.0f);
				vs[3].Set(-1.0f, 5.0f);
				ChainShape shape = new ChainShape();
				shape.CreateLoop(vs, 4);
				shape.Density = 0;
				ground.CreateFixture(shape);
			}

			// Edge loop. Collision should be smooth.
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(-10.0f, 4.0f);
				Body ground = m_world.CreateBody(bd);

				Vec2[] vs = new Vec2[10];
				vs[0].Set(0.0f, 0.0f);
				vs[1].Set(6.0f, 0.0f);
				vs[2].Set(6.0f, 2.0f);
				vs[3].Set(4.0f, 1.0f);
				vs[4].Set(2.0f, 2.0f);
				vs[5].Set(0.0f, 2.0f);
				vs[6].Set(-2.0f, 2.0f);
				vs[7].Set(-4.0f, 3.0f);
				vs[8].Set(-6.0f, 2.0f);
				vs[9].Set(-6.0f, 0.0f);
				ChainShape shape = new ChainShape();
				shape.CreateLoop(vs, 10);
				shape.Density = 0;
				ground.CreateFixture(shape);
			}

			// Square character 1
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(-3.0f, 8.0f);
				bd.type = BodyType._dynamicBody;
				bd.fixedRotation = true;
				bd.allowSleep = false;

				Body body = m_world.CreateBody(bd);

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

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.Density = 20.0f;
				body.CreateFixture(fd);
			}

			// Square character 2
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(-5.0f, 5.0f);
				bd.type = BodyType._dynamicBody;
				bd.fixedRotation = true;
				bd.allowSleep = false;

				Body body = m_world.CreateBody(bd);

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

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.Density = 20.0f;
				body.CreateFixture(fd);
			}

			// Hexagon character
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(-5.0f, 8.0f);
				bd.type = BodyType._dynamicBody;
				bd.fixedRotation = true;
				bd.allowSleep = false;

				Body body = m_world.CreateBody(bd);

				float angle = 0.0f;
				float delta = (float)Math.PI / 3.0f;
				Vec2[] vertices = new Vec2[6];
				for (int i = 0; i < 6; ++i)
				{
					vertices[i].Set(0.5f * (float)Math.Cos(angle), 0.5f * (float)Math.Sin(angle));
					angle += delta;
				}

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

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.Density = 20.0f;
				body.CreateFixture(fd);
			}

			// Circle character
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(3.0f, 5.0f);
				bd.type = BodyType._dynamicBody;
				bd.fixedRotation = true;
				bd.allowSleep = false;

				Body body = m_world.CreateBody(bd);

				CircleShape shape = new CircleShape();
				shape.m_radius = 0.5f;

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.Density = 20.0f;
				body.CreateFixture(fd);
			}

			// Circle character
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(-7.0f, 6.0f);
				bd.type = BodyType._dynamicBody;
				bd.allowSleep = false;

				m_character = m_world.CreateBody(bd);

				CircleShape shape = new CircleShape();
				shape.m_radius = 0.25f;

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.Density = 20.0f;
				fd.friction = 1.0f;
				m_character.CreateFixture(fd);
			}
		}
Exemplo n.º 32
0
		public Dominos()
		{
			Body b1;
			{
				EdgeShape shape = new EdgeShape();
				shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
				shape.Density = 0;

				BodyDef bd = new BodyDef();
				b1 = m_world.CreateBody(bd);
				b1.CreateFixture(shape);
			}

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

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

			{
				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.type = BodyType._dynamicBody;
					bd.Position.Set(-6.0f + 1.0f * i, 11.25f);
					Body body = m_world.CreateBody(bd);
					body.CreateFixture(fd);
				}
			}

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

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

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

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

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

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(-0.9f, 1.0f);
				bd.angle = -0.15f;

				b3 = m_world.CreateBody(bd);
				b3.CreateFixture(shape);
			}

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

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

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

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(-10.0f, 15.0f);
				b4 = m_world.CreateBody(bd);
				shape.Density = 10;
				shape.Density = 10;
				b4.CreateFixture(shape);
			}

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

			Body b5;
			{
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(6.5f, 3.0f);
				b5 = m_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);
			m_world.CreateJoint(jd);

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

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(6.5f, 4.1f);
				b6 = m_world.CreateBody(bd);
				shape.Density = 30;
				b6.CreateFixture(shape);
			}

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

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

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

				b7 = m_world.CreateBody(bd);
				shape.Density = 10;
				b7.CreateFixture(shape);
			}

			DistanceJointDef djd = new DistanceJointDef();
			djd.bodyA = b3;
			djd.bodyB = b7;
			djd.localAnchorA.Set(6.0f, 0.0f);
			djd.localAnchorB.Set(0.0f, -1.0f);
			Vec2 d = djd.bodyB.GetWorldPoint(djd.localAnchorB) - djd.bodyA.GetWorldPoint(djd.localAnchorA);
			djd.length = d.Length();
			m_world.CreateJoint(djd);

			{
				float radius = 0.2f;

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

				for (int i = 0; i < 4; ++i)
				{
					BodyDef bd = new BodyDef();
					bd.type = BodyType._dynamicBody;
					bd.Position.Set(5.9f + 2.0f * radius * i, 2.4f);
					Body body = m_world.CreateBody(bd);
					shape.Density = 10;
					body.CreateFixture(shape);
				}
			}
		}
Exemplo n.º 33
0
		// We need separation create/destroy functions from the constructor/destructor because
		// the destructor cannot access the allocator (no destructor arguments allowed by C++).
		internal void Create(Body body, FixtureDef def){
			m_userData = def.UserData;
			m_friction = def.friction;
			m_restitution = def.restitution;

			m_body = body;
			m_next = null;

			m_filter = def.Filter;

			m_isSensor = def.IsSensor;

			m_shape = def.shape.Clone();

			// Reserve proxy space
			int childCount = m_shape.GetChildCount();
			m_proxies = new List<FixtureProxy>();

			m_Density = def.Density;
		}
Exemplo n.º 34
0
		public CollisionFiltering()
		{
			// Ground body
			{
				EdgeShape shape = new EdgeShape();
				shape.Set(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 = m_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.type = BodyType._dynamicBody;
			triangleBodyDef.Position.Set(-5.0f, 2.0f);

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

			Body body2 = m_world.CreateBody(triangleBodyDef);
			body2.CreateFixture(triangleShapeDef);

			{
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(-5.0f, 10.0f);
				Body body = m_world.CreateBody(bd);

				PolygonShape p = new PolygonShape();
				p.SetAsBox(0.5f, 1.0f);
				body.CreateFixture(p);

				PrismaticJointDef jd = new PrismaticJointDef();
				jd.bodyA = body2;
				jd.bodyB = body;
				jd.enableLimit = true;
				jd.localAnchorA.Set(0.0f, 4.0f);
				jd.localAnchorB.SetZero();
				jd.localAxisA.Set(0.0f, 1.0f);
				jd.lowerTranslation = -1.0f;
				jd.upperTranslation = 1.0f;

				m_world.CreateJoint(jd);
			}

			// 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.type = BodyType._dynamicBody;
			boxBodyDef.Position.Set(0.0f, 2.0f);

			Body body3 = m_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 = 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;

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

			BodyDef circleBodyDef = new BodyDef();
			circleBodyDef.type = BodyType._dynamicBody;
			circleBodyDef.Position.Set(5.0f, 2.0f);
		
			Body body5 = m_world.CreateBody(circleBodyDef);
			body5.CreateFixture(circleShapeDef);

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

			Body body6 = m_world.CreateBody(circleBodyDef);
			body6.CreateFixture(circleShapeDef);
		}
Exemplo n.º 35
0
		public ApplyForce() {
			m_world.SetGravity(new Vec2(0.0f, 0.0f));

			const float k_restitution = 0.4f;

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

				EdgeShape shape = new EdgeShape();

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

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

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

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

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

			{
				Transform xf1 = new Transform();
				xf1.q.Set(0.3524f * (float)Math.PI);
				xf1.p = xf1.q.GetXAxis();

				Vec2[] vertices = new Vec2[3];
				vertices[0] = Utilities.Mul(xf1, new Vec2(-1.0f, 0.0f));
				vertices[1] = Utilities.Mul(xf1, new Vec2(1.0f, 0.0f));
				vertices[2] = Utilities.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 = 4.0f;

				Transform xf2 = new Transform();
				xf2.q.Set(-0.3524f * (float)Math.PI);
				xf2.p = -xf2.q.GetXAxis();

				vertices[0] = Utilities.Mul(xf2, new Vec2(-1.0f, 0.0f));
				vertices[1] = Utilities.Mul(xf2, new Vec2(1.0f, 0.0f));
				vertices[2] = Utilities.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.type = BodyType._dynamicBody;
				bd.angularDamping = 5.0f;
				bd.linearDamping = 0.1f;

				bd.Position.Set(0.0f, 2.0f);
				bd.angle = (float)Math.PI;
				bd.allowSleep = false;
				m_body = m_world.CreateBody(bd);
				m_body.CreateFixture(sd1);
				m_body.CreateFixture(sd2);
			}

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

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

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

					bd.Position.Set(0.0f, 5.0f + 1.54f * i);
					Body body = m_world.CreateBody(bd);

					body.CreateFixture(fd);

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

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

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

					m_world.CreateJoint(jd);
				}
			}
		}
Exemplo n.º 36
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);
		}