Esempio n. 1
0
string IESoRWorld::worldDrawList()
{
	//This will be written to json
	//fastwriter is not human readable --> compact
	//exactly what we want to sent to our html files
	Json::FastWriter* writer = new Json::FastWriter();

	//root is what we'll be sending back with all the shapes (stored in shapes!)
	Json::Value root;
	Json::Value shapes;

	//we'll loop through all required body information
	b2Body * B = this->world->GetBodyList();

	while(B != NULL)
	{
		if(B->GetUserData())
		{
			//fetch our body identifier
			PhysicsID* pid = static_cast<PhysicsID*>(B->GetUserData());//*((string*)B->GetUserData());
			std::string bodyID = pid->ID();
			
			//we must get all our shapes
			b2Fixture* F = B->GetFixtureList();

			//cycle through the shapes
			while(F != NULL)
			{
				//Hold our shape drawing information
				Json::Value singleShape;

				switch (F->GetType())
				{
				case b2Shape::e_circle:
					{
						b2CircleShape* circle = (b2CircleShape*) F->GetShape();
						/* Do stuff with a circle shape */
						Json::Value center = positionToJSONValue(circle->m_p);
						Json::Value radius = circle->m_radius;
						singleShape["type"] = "Circle";
						singleShape["bodyOffset"] = positionToJSONValue(B->GetPosition());
						singleShape["rotation"] = B->GetAngle();
						singleShape["center"] = center;
						singleShape["radius"] = circle->m_radius;
						singleShape["color"] = "#369";
					}
					break;
				case b2Shape::e_polygon:
					{
						b2PolygonShape* poly = (b2PolygonShape*) F->GetShape();
						/* Do stuff with a polygon shape */

						Json::Value points = listOfPoints(poly->m_vertices, poly->m_count);
						singleShape["type"] = "Polygon";
						singleShape["points"] = points;
						singleShape["bodyOffset"] = positionToJSONValue(B->GetPosition());
						singleShape["rotation"] = B->GetAngle();
						singleShape["color"] = "#38F";

					}
					break;
				}

				//Each shape is the unique combination of 
				pid = static_cast<PhysicsID*>(F->GetUserData());//*((string*)B->GetUserData());
				
				string shapeID = pid->ID();// *((string*)F->GetUserData());

				string fullID = bodyID + "_" + shapeID;
				shapes[fullID] = singleShape;

				F = F->GetNext();
			}
		}

		B = B->GetNext();
	}
	//we set our shapes using the body loops
	root["shapes"] = shapes;

	//we now need to process all of our joints as well
	b2Joint * J = this->world->GetJointList();

	Json::Value joints;

	while(J != NULL)
	{
		if(J->GetUserData())
		{
			//fetch our body identifier
			Bone* jid = static_cast<Bone*>(J->GetUserData());

			//we grab the joint identifier
			std::string bodyID = jid->ID();
		
			//Hold our joint drawing information
			Json::Value singleJoint;

			//we should use the body identifiers
			//but they both need to exist for this to be valid
			if(J->GetBodyA()->GetUserData() && J->GetBodyB()->GetUserData())
			{
				//we need to know what bodies are connected
				PhysicsID* pid = static_cast<PhysicsID*>(J->GetBodyA()->GetUserData());
				singleJoint["sourceID"] =  pid->ID();

				pid = static_cast<PhysicsID*>( J->GetBodyB()->GetUserData());
				singleJoint["targetID"] =  pid->ID();

				//now we need more drawing informtion regarding offsets on the body
				
				//get the anchor relative to body A
				//set that in our json object
				singleJoint["sourceOffset"] = positionToJSONValue(J->GetAnchorA());
				//set the same for our second object
				singleJoint["targetOffset"] = positionToJSONValue(J->GetAnchorB());
			}
			
			//set our joint object using the id, and json values
			joints[bodyID] = singleJoint;
		}

		//loop to the next object
		J = J->GetNext();
	}

	root["joints"] = joints;

	return writer->write(root);
}