Exemplo n.º 1
0
        private PinballTest()
        {
            // Ground body
            Body ground;
            {
                ground = BodyFactory.CreateBody(World);

                Vertices vertices = new Vertices(5);
                vertices.Add(new Vector2(0.0f, -2.0f));
                vertices.Add(new Vector2(8.0f, 6.0f));
                vertices.Add(new Vector2(8.0f, 20.0f));
                vertices.Add(new Vector2(-8.0f, 20.0f));
                vertices.Add(new Vector2(-8.0f, 6.0f));

                ChainShape chain = new ChainShape(vertices);
                chain.CreateLoop(vertices);
                ground.CreateFixture(chain);
            }

            // Flippers
            {
                Vector2 p1 = new Vector2(-2.0f, 0f);
                Vector2 p2 = new Vector2(2.0f, 0f);

                Body leftFlipper = BodyFactory.CreateBody(World, p1);
                leftFlipper.BodyType = BodyType.Dynamic;
                Body rightFlipper = BodyFactory.CreateBody(World, p2);
                rightFlipper.BodyType = BodyType.Dynamic;

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

                leftFlipper.CreateFixture(box);
                rightFlipper.CreateFixture(box);

                _leftJoint = new RevoluteJoint(ground, leftFlipper, p1, Vector2.Zero);
                _leftJoint.MaxMotorTorque = 1000.0f;
                _leftJoint.LimitEnabled = true;
                _leftJoint.MotorEnabled = true;
                _leftJoint.MotorSpeed = 0.0f;
                _leftJoint.LowerLimit = -30.0f * Settings.Pi / 180.0f;
                _leftJoint.UpperLimit = 5.0f * Settings.Pi / 180.0f;
                World.AddJoint(_leftJoint);

                _rightJoint = new RevoluteJoint(ground, rightFlipper, p2, Vector2.Zero);
                _rightJoint.MaxMotorTorque = 1000.0f;
                _rightJoint.LimitEnabled = true;
                _rightJoint.MotorEnabled = true;
                _rightJoint.MotorSpeed = 0.0f;
                _rightJoint.LowerLimit = -5.0f * Settings.Pi / 180.0f;
                _rightJoint.UpperLimit = 30.0f * Settings.Pi / 180.0f;
                World.AddJoint(_rightJoint);
            }

            // Circle character
            {
                _ball = BodyFactory.CreateBody(World, new Vector2(1.0f, 15.0f));
                _ball.BodyType = BodyType.Dynamic;
                _ball.IsBullet = true;
                _ball.CreateFixture(new CircleShape(0.2f, 1.0f));
            }
        }
Exemplo n.º 2
0
		Fixture j2b2Fixture(Body body, JObject fixtureValue)
		{
			
			
			if (null == fixtureValue)
				return null;
			
			
			//Fixture fixtureDef = new Fixture();
			var restitution = jsonToFloat("restitution", fixtureValue);
			var friction = jsonToFloat("friction", fixtureValue);
			var density = jsonToFloat("density", fixtureValue);
			var isSensor = fixtureValue["sensor"] == null ? false : (bool)fixtureValue["sensor"];
			
			
			var categoryBits = fixtureValue["filter-categoryBits"] == null ? 0x0001 : (int)fixtureValue["filter-categoryBits"];
			var maskBits = fixtureValue["filter-maskBits"] == null ? 0xffff : (int)fixtureValue["filter-maskBits"];
			var groupIndex = fixtureValue["filter-groupIndex"] == null ? (short)0 : (short)fixtureValue["filter-groupIndex"];
			
			
			Fixture fixture = null;
			
			
			
			if (null != fixtureValue["circle"])
			{
				JObject circleValue = (JObject)fixtureValue["circle"];
				var radius = jsonToFloat("radius", circleValue);
				var position = jsonToVec("center", circleValue);
				fixture = FixtureFactory.AttachCircle(radius, density, body, position);
			}
			else if (null != fixtureValue["edge"])
			{
				JObject edgeValue = (JObject)fixtureValue["edge"];
				var m_vertex1 = (jsonToVec("vertex1", edgeValue));
				var m_vertex2 = (jsonToVec("vertex2", edgeValue));
				fixture = FixtureFactory.AttachEdge(m_vertex1, m_vertex2, body);
				((EdgeShape)fixture.Shape).HasVertex0 = edgeValue["hasVertex0"] == null ? false : (bool)edgeValue["hasVertex0"];
				((EdgeShape)fixture.Shape).HasVertex3 = edgeValue["hasVertex3"] == null ? false : (bool)edgeValue["hasVertex3"];
				
				if (((EdgeShape)fixture.Shape).HasVertex0)
					((EdgeShape)fixture.Shape).Vertex0 = (jsonToVec("vertex0", edgeValue));
				if (((EdgeShape)fixture.Shape).HasVertex3)
					((EdgeShape)fixture.Shape).Vertex3 = (jsonToVec("vertex3", edgeValue));
				
			}
			else if (null != fixtureValue["loop"])
			{// support old
				// format (r197)
				JObject chainValue = (JObject)fixtureValue["loop"];
				int numVertices = ((JArray)chainValue["x"]).Count;
				Vertices vertices = new Vertices();
				for (int i = 0; i < numVertices; i++)
					vertices.Add(jsonToVec("vertices", chainValue, i));
				fixture = FixtureFactory.AttachChainShape(vertices, body);
				
			}
			else if (null != fixtureValue["chain"])
			{
				JObject chainValue = (JObject)fixtureValue["chain"];
				ChainShape chainShape = new ChainShape();
				int numVertices = ((JArray)chainValue["vertices"]["x"]).Count;
				
				Vertices vertices = new Vertices();
				
				
				for (int i = 0; i < numVertices; i++)
					vertices.Add(jsonToVec("vertices", chainValue, i));
				
				// FPE. See http://www.box2d.org/forum/viewtopic.php?f=4&t=7973&p=35363
				if (vertices[0] == vertices[vertices.Count - 1])
				{
					var vertices2 = new Vertices(numVertices - 1);
					vertices2.AddRange(vertices.GetRange(0, numVertices - 1));
					chainShape.CreateLoop(vertices2);
					fixture = body.CreateFixture(chainShape);
				}
				else
					fixture = FixtureFactory.AttachChainShape(vertices, body);
				
				var fixtureChain = fixture.Shape as ChainShape;
				
				var hasPrevVertex = chainValue["hasPrevVertex"] == null ? false : (bool)chainValue["hasPrevVertex"];
				var hasNextVertex = chainValue["hasNextVertex"] == null ? false : (bool)chainValue["hasNextVertex"];
				if (hasPrevVertex)
					fixtureChain.PrevVertex = (jsonToVec("prevVertex", chainValue));
				if (hasNextVertex)
					fixtureChain.NextVertex = (jsonToVec("nextVertex", chainValue));
				
			}
			else if (null != fixtureValue["polygon"])
			{
				JObject polygonValue = (JObject)fixtureValue["polygon"];
				Vertices vertices = new Vertices();
				int numVertices = ((JArray)polygonValue["vertices"]["x"]).Count;
				if (numVertices > Settings.MaxPolygonVertices)
				{
					Console.WriteLine("Ignoring polygon fixture with too many vertices.");
				}
				else if (numVertices < 2)
				{
					Console.WriteLine("Ignoring polygon fixture less than two vertices.");
				}
				else if (numVertices == 2)
				{
					Console.WriteLine("Creating edge shape instead of polygon with two vertices.");
					var m_vertex1 = (jsonToVec("vertices", polygonValue, 0));
					var m_vertex2 = (jsonToVec("vertices", polygonValue, 1));
					fixture = FixtureFactory.AttachEdge(m_vertex1, m_vertex2, body);
					
				}
				else
				{
					for (int i = 0; i < numVertices; i++)
						vertices.Add(jsonToVec("vertices", polygonValue, i));
					fixture = FixtureFactory.AttachPolygon(vertices, density, body);
				}
			}
			
			String fixtureName = fixtureValue["name"] == null ? "" : fixtureValue["name"].ToString();
			if (fixtureName != "")
			{
				SetFixtureName(fixture, fixtureName);
			}
			
			if (fixture != null)
			{
				fixture.Restitution = restitution;
				fixture.Friction = friction;
				fixture.Shape.Density = density;
				fixture.IsSensor = isSensor;
				fixture.CollisionCategories = (Category)categoryBits;
				fixture.CollidesWith = (Category)maskBits;
				fixture.CollisionGroup = groupIndex;
			}
			
			
			return fixture;
		}
Exemplo n.º 3
0
 public static Fixture AttachLoopShape(Vertices vertices, Body body, object userData = null)
 {
     ChainShape shape = new ChainShape();
     shape.CreateLoop(vertices);
     return body.CreateFixture(shape, userData);
 }