Ejemplo n.º 1
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;
		}