Daniele Bartolini 11 лет назад
Родитель
Сommit
ce752d0b77

+ 11 - 15
engine/physics/actor.cpp

@@ -63,10 +63,9 @@ using physx::PxPlane;
 namespace crown
 {
 
-Actor::Actor(PhysicsWorld& pw, const PhysicsResource* res, uint32_t actor_idx, SceneGraph& sg, int32_t node, UnitId unit_id)
+Actor::Actor(PhysicsWorld& pw, const ActorResource* ar, SceneGraph& sg, int32_t node, UnitId unit_id)
 	: m_world(pw)
-	, m_resource(res)
-	, m_index(actor_idx)
+	, m_resource(ar)
 	, m_scene_graph(sg)
 	, m_node(node)
 	, m_unit(unit_id)
@@ -81,7 +80,7 @@ Actor::~Actor()
 
 void Actor::create_objects()
 {
-	const PhysicsActor* actor = physics_resource::actor(m_resource, m_index);
+	const ActorResource* actor = m_resource;
 
 	PxScene* scene = m_world.physx_scene();
 	PxPhysics* physics = m_world.physx_physics();
@@ -110,10 +109,9 @@ void Actor::create_objects()
 	}
 
 	// Create shapes
-	uint32_t shape_index = physics_resource::shape_index(m_resource, m_index);
-	for (uint32_t i = 0; i < actor->num_shapes; i++)
+	for (uint32_t i = 0; i < actor->num_shapes; ++i)
 	{
-		const PhysicsShape* shape = physics_resource::shape(m_resource, shape_index);
+		const ShapeResource* shape = actor_resource::shape(m_resource, i);
 		const PhysicsShape2* shape_class = physics_config_resource::shape(config, shape->shape_class);
 		const PhysicsMaterial* material = physics_config_resource::material(config, shape->material);
 
@@ -122,27 +120,27 @@ void Actor::create_objects()
 		PxShape* px_shape = NULL;
 		switch(shape->type)
 		{
-			case PhysicsShapeType::SPHERE:
+			case ShapeType::SPHERE:
 			{
 				px_shape = m_actor->createShape(PxSphereGeometry(shape->data_0), *mat);
 				break;
 			}
-			case PhysicsShapeType::CAPSULE:
+			case ShapeType::CAPSULE:
 			{
 				px_shape = m_actor->createShape(PxCapsuleGeometry(shape->data_0, shape->data_1), *mat);
 				break;
 			}
-			case PhysicsShapeType::BOX:
+			case ShapeType::BOX:
 			{
 				px_shape = m_actor->createShape(PxBoxGeometry(shape->data_0, shape->data_1, shape->data_2), *mat);
 				break;
 			}
-			case PhysicsShapeType::PLANE:
+			case ShapeType::PLANE:
 			{
 				px_shape = m_actor->createShape(PxPlaneGeometry(), *mat);
 				break;
 			}
-			case PhysicsShapeType::CONVEX_MESH:
+			case ShapeType::CONVEX_MESH:
 			{
 				// MeshResource* resource = (MeshResource*) device()->resource_manager()->get(MESH_TYPE, shape->resource.name);
 
@@ -172,7 +170,7 @@ void Actor::create_objects()
 		}
 
 		// Setup shape pose
-		if (shape->type == PhysicsShapeType::PLANE)
+		if (shape->type == ShapeType::PLANE)
 		{
 			px_shape->setLocalPose(PxTransformFromPlaneEquation(
 				PxPlane(shape->data_0, shape->data_1, shape->data_2, shape->data_3)));
@@ -195,8 +193,6 @@ void Actor::create_objects()
 			px_shape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
 			px_shape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, true);
 		}
-
-		shape_index++;
 	}
 
 	if (is_dynamic())

+ 3 - 4
engine/physics/actor.h

@@ -32,7 +32,7 @@ struct SceneGraph;
 /// @ingroup Physics
 struct Actor
 {
-	Actor(PhysicsWorld& pw, const PhysicsResource* res, uint32_t actor_idx, SceneGraph& sg, int32_t node, UnitId unit_id);
+	Actor(PhysicsWorld& pw, const ActorResource* ar, SceneGraph& sg, int32_t node, UnitId unit_id);
 	~Actor();
 
 	/// Returns the world position of the actor.
@@ -150,7 +150,7 @@ struct Actor
 	/// Returns the unit that owns the actor.
 	Unit* unit();
 
-	const PhysicsResource* resource() const { return m_resource; }
+	const ActorResource* resource() const { return m_resource; }
 
 private:
 
@@ -162,8 +162,7 @@ private:
 public:
 
 	PhysicsWorld& m_world;
-	const PhysicsResource* m_resource;
-	uint32_t m_index;
+	const ActorResource* m_resource;
 	PxRigidActor* m_actor;
 
 	SceneGraph& m_scene_graph;

+ 7 - 8
engine/physics/controller.cpp

@@ -24,24 +24,23 @@ using physx::PxVec3;
 namespace crown
 {
 
-Controller::Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node, PxPhysics* physics, PxControllerManager* manager)
-	: m_resource(pr)
+Controller::Controller(const ControllerResource* cr, SceneGraph& sg, int32_t node, PxPhysics* physics, PxControllerManager* manager)
+	: m_resource(cr)
 	, m_scene_graph(sg)
 	, m_node(node)
 	, m_manager(manager)
 	, m_controller(NULL)
 {
-	const PhysicsController* contr = physics_resource::controller(pr);
 	const Vector3 pos = sg.world_position(m_node);
 
 	PxCapsuleControllerDesc desc;
 	desc.climbingMode = PxCapsuleClimbingMode::eCONSTRAINED;
 	desc.nonWalkableMode = PxCCTNonWalkableMode::eFORCE_SLIDING;
-	desc.radius = contr->radius;
-	desc.height = contr->height;
-	desc.slopeLimit = cos(contr->slope_limit);
-	desc.stepOffset = contr->step_offset;
-	desc.contactOffset = contr->contact_offset;
+	desc.radius = cr->radius;
+	desc.height = cr->height;
+	desc.slopeLimit = cos(cr->slope_limit);
+	desc.stepOffset = cr->step_offset;
+	desc.contactOffset = cr->contact_offset;
 	desc.upDirection = PxVec3(0.0, 1.0, 0.0);
 	desc.material = physics->createMaterial(0.5f, 0.5f, 0.5f);
 	desc.position = PxExtendedVec3(pos.x, pos.y, pos.z);

+ 2 - 2
engine/physics/controller.h

@@ -26,7 +26,7 @@ struct SceneGraph;
 /// @ingroup Physics
 struct Controller
 {
-	Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node, PxPhysics* physics, PxControllerManager* manager);
+	Controller(const ControllerResource* cr, SceneGraph& sg, int32_t node, PxPhysics* physics, PxControllerManager* manager);
 	~Controller();
 
 	/// Moves the controller to @a pos.
@@ -51,7 +51,7 @@ struct Controller
 
 private:
 
-	const PhysicsResource* m_resource;
+	const ControllerResource* m_resource;
 
 	SceneGraph& m_scene_graph;
 	int32_t m_node;

+ 15 - 23
engine/physics/joint.cpp

@@ -65,16 +65,13 @@ using physx::PxJointAngularLimitPair;
 namespace crown
 {
 
-Joint::Joint(PxPhysics* physics, const PhysicsResource* pr, const uint32_t index, const Actor& actor_0, const Actor& actor_1)
-	: m_resource(pr)
-	, m_index(index)
+Joint::Joint(PxPhysics* physics, const JointResource* jr, const Actor& actor_0, const Actor& actor_1)
+	: m_resource(jr)
 {
-	const PhysicsJoint* joint = physics_resource::joint(m_resource, m_index);
+	PxVec3 anchor_0(jr->anchor_0.x, jr->anchor_0.y, jr->anchor_0.z);
+	PxVec3 anchor_1(jr->anchor_1.x, jr->anchor_1.y, jr->anchor_1.z);
 
-	PxVec3 anchor_0(joint->anchor_0.x, joint->anchor_0.y, joint->anchor_0.z);
-	PxVec3 anchor_1(joint->anchor_1.x, joint->anchor_1.y, joint->anchor_1.z);
-
-	switch(joint->type)
+	switch(jr->type)
 	{
 		case JointType::FIXED:
 		{
@@ -84,10 +81,10 @@ Joint::Joint(PxPhysics* physics, const PhysicsResource* pr, const uint32_t index
 		}
 		case JointType::SPHERICAL:
 		{
-			PxJointLimitCone limit_cone(joint->y_limit_angle, joint->z_limit_angle, joint->contact_dist);
-			limit_cone.restitution = joint->restitution;
-			limit_cone.damping = joint->damping;
-			limit_cone.contactDistance = joint->distance;
+			PxJointLimitCone limit_cone(jr->y_limit_angle, jr->z_limit_angle, jr->contact_dist);
+			limit_cone.restitution = jr->restitution;
+			limit_cone.damping = jr->damping;
+			limit_cone.contactDistance = jr->distance;
 
 			m_joint = PxSphericalJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
 			static_cast<PxSphericalJoint*>(m_joint)->setLimitCone(limit_cone);
@@ -97,8 +94,8 @@ Joint::Joint(PxPhysics* physics, const PhysicsResource* pr, const uint32_t index
 		}
 		case JointType::REVOLUTE:
 		{
-			PxJointAngularLimitPair limit_pair(joint->lower_limit, joint->upper_limit, joint->contact_dist);
-			limit_pair.damping = joint->damping;
+			PxJointAngularLimitPair limit_pair(jr->lower_limit, jr->upper_limit, jr->contact_dist);
+			limit_pair.damping = jr->damping;
 
 			m_joint = PxRevoluteJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
 			static_cast<PxRevoluteJoint*>(m_joint)->setLimit(limit_pair);
@@ -111,8 +108,8 @@ Joint::Joint(PxPhysics* physics, const PhysicsResource* pr, const uint32_t index
 		}
 		case JointType::PRISMATIC:
 		{
-			PxJointLinearLimitPair limit_pair(PxTolerancesScale(), joint->lower_limit, joint->upper_limit, joint->contact_dist);
-			limit_pair.damping = joint->damping;
+			PxJointLinearLimitPair limit_pair(PxTolerancesScale(), jr->lower_limit, jr->upper_limit, jr->contact_dist);
+			limit_pair.damping = jr->damping;
 
 			m_joint = PxPrismaticJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
 			static_cast<PxPrismaticJoint*>(m_joint)->setLimit(limit_pair);
@@ -123,7 +120,7 @@ Joint::Joint(PxPhysics* physics, const PhysicsResource* pr, const uint32_t index
 		case JointType::DISTANCE:
 		{
 			m_joint = PxDistanceJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
-			static_cast<PxDistanceJoint*>(m_joint)->setMaxDistance(joint->max_distance);
+			static_cast<PxDistanceJoint*>(m_joint)->setMaxDistance(jr->max_distance);
 			static_cast<PxDistanceJoint*>(m_joint)->setDistanceJointFlag(PxDistanceJointFlag::eMAX_DISTANCE_ENABLED, true);
 
 			break;
@@ -136,12 +133,7 @@ Joint::Joint(PxPhysics* physics, const PhysicsResource* pr, const uint32_t index
 		}
 	}
 
-	if (joint->breakable) m_joint->setBreakForce(joint->break_force, joint->break_torque);
-}
-
-Joint::~Joint()
-{
-
+	if (jr->breakable) m_joint->setBreakForce(jr->break_force, jr->break_torque);
 }
 
 } // namespace crown

+ 2 - 6
engine/physics/joint.h

@@ -9,13 +9,11 @@
 #include "resource_types.h"
 
 #include "PxPhysics.h"
-#include "PxScene.h"
 #include "PxJoint.h"
 #include "PxMaterial.h"
 
 using physx::PxPhysics;
 using physx::PxJoint;
-using physx::PxScene;
 using physx::PxMaterial;
 
 namespace crown
@@ -27,13 +25,11 @@ struct Actor;
 /// @ingroup Physics
 struct Joint
 {
-	Joint(PxPhysics* physics, const PhysicsResource* pr, const uint32_t index, const Actor& actor_0, const Actor& actor_1);
-	~Joint();
+	Joint(PxPhysics* physics, const JointResource* jr, const Actor& actor_0, const Actor& actor_1);
 
 public:
 
-	const PhysicsResource* 	m_resource;
-	const uint32_t 			m_index;
+	const JointResource* 	m_resource;
 
 	PxJoint* 			m_joint;
 	PxMaterial* 		m_mat;

+ 6 - 2
engine/physics/physics_types.h

@@ -39,7 +39,9 @@ struct ShapeType
 		CAPSULE,
 		BOX,
 		PLANE,
-		CONVEX_MESH
+		CONVEX_MESH,
+
+		COUNT
 	};
 };
 
@@ -52,7 +54,9 @@ struct JointType
 		REVOLUTE,
 		PRISMATIC,
 		DISTANCE,
-		D6
+		D6,
+
+		COUNT
 	};
 };
 

+ 6 - 6
engine/physics/physics_world.cpp

@@ -251,9 +251,9 @@ PhysicsWorld::~PhysicsWorld()
 	m_scene->release();
 }
 
-ActorId	PhysicsWorld::create_actor(const PhysicsResource* res, const uint32_t index, SceneGraph& sg, int32_t node, UnitId unit_id)
+ActorId	PhysicsWorld::create_actor(const ActorResource* ar, SceneGraph& sg, int32_t node, UnitId unit_id)
 {
-	Actor* actor = CE_NEW(m_actors_pool, Actor)(*this, res, index, sg, node, unit_id);
+	Actor* actor = CE_NEW(m_actors_pool, Actor)(*this, ar, sg, node, unit_id);
 	return id_array::create(m_actors, actor);
 }
 
@@ -263,9 +263,9 @@ void PhysicsWorld::destroy_actor(ActorId id)
 	id_array::destroy(m_actors, id);
 }
 
-ControllerId PhysicsWorld::create_controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node)
+ControllerId PhysicsWorld::create_controller(const ControllerResource* cr, SceneGraph& sg, int32_t node)
 {
-	Controller* controller = CE_NEW(m_controllers_pool, Controller)(pr, sg, node, physics_globals::s_physics, m_controller_manager);
+	Controller* controller = CE_NEW(m_controllers_pool, Controller)(cr, sg, node, physics_globals::s_physics, m_controller_manager);
 	return id_array::create(m_controllers, controller);
 }
 
@@ -275,9 +275,9 @@ void PhysicsWorld::destroy_controller(ControllerId id)
 	id_array::destroy(m_controllers, id);
 }
 
-JointId	PhysicsWorld::create_joint(const PhysicsResource* pr, const uint32_t index, const Actor& actor_0, const Actor& actor_1)
+JointId	PhysicsWorld::create_joint(const JointResource* jr, const Actor& actor_0, const Actor& actor_1)
 {
-	Joint* joint = CE_NEW(m_joints_pool, Joint)(physics_globals::s_physics, pr, index, actor_0, actor_1);
+	Joint* joint = CE_NEW(m_joints_pool, Joint)(physics_globals::s_physics, jr, actor_0, actor_1);
 	return id_array::create(m_joints, joint);
 }
 

+ 3 - 3
engine/physics/physics_world.h

@@ -48,13 +48,13 @@ public:
 	PhysicsWorld(World& world);
 	~PhysicsWorld();
 
-	ActorId create_actor(const PhysicsResource* res, const uint32_t index, SceneGraph& sg, int32_t node, UnitId unit_id);
+	ActorId create_actor(const ActorResource* ar, SceneGraph& sg, int32_t node, UnitId unit_id);
 	void destroy_actor(ActorId id);
 
-	ControllerId create_controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node);
+	ControllerId create_controller(const ControllerResource* cr, SceneGraph& sg, int32_t node);
 	void destroy_controller(ControllerId id);
 
-	JointId create_joint(const PhysicsResource* pr, const uint32_t index, const Actor& actor_0, const Actor& actor_1);
+	JointId create_joint(const JointResource* jr, const Actor& actor_0, const Actor& actor_1);
 	void destroy_joint(JointId id);
 
 	RaycastId create_raycast(CollisionMode::Enum mode, CollisionType::Enum filter);

+ 96 - 116
engine/resource/physics_resource.cpp

@@ -3,10 +3,10 @@
  * License: https://github.com/taylor001/crown/blob/master/LICENSE
  */
 
+#include "physics_resource.h"
+#include "physics_types.h"
 #include "filesystem.h"
-#include "string_utils.h"
 #include "json_parser.h"
-#include "physics_resource.h"
 #include "string_utils.h"
 #include "dynamic_string.h"
 #include "map.h"
@@ -21,36 +21,36 @@ namespace physics_resource
 	struct Shape
 	{
 		const char* name;
-		PhysicsShapeType::Enum type;
+		ShapeType::Enum type;
 	};
 
-	static const Shape s_shape[PhysicsShapeType::COUNT] =
+	static const Shape s_shape[ShapeType::COUNT] =
 	{
-		{ "sphere",  PhysicsShapeType::SPHERE  },
-		{ "capsule", PhysicsShapeType::CAPSULE },
-		{ "box",     PhysicsShapeType::BOX     },
-		{ "plane",   PhysicsShapeType::PLANE   }
+		{ "sphere",  ShapeType::SPHERE  },
+		{ "capsule", ShapeType::CAPSULE },
+		{ "box",     ShapeType::BOX     },
+		{ "plane",   ShapeType::PLANE   }
 	};
 
 	struct Joint
 	{
 		const char* name;
-		PhysicsJointType::Enum type;
+		JointType::Enum type;
 	};
 
-	static const Joint s_joint[PhysicsJointType::COUNT] =
+	static const Joint s_joint[JointType::COUNT] =
 	{
-		{ "fixed",     PhysicsJointType::FIXED     },
-		{ "spherical", PhysicsJointType::SPHERICAL },
-		{ "revolute",  PhysicsJointType::REVOLUTE  },
-		{ "prismatic", PhysicsJointType::PRISMATIC },
-		{ "distance",  PhysicsJointType::DISTANCE  },
-		{ "d6",        PhysicsJointType::D6        }
+		{ "fixed",     JointType::FIXED     },
+		{ "spherical", JointType::SPHERICAL },
+		{ "revolute",  JointType::REVOLUTE  },
+		{ "prismatic", JointType::PRISMATIC },
+		{ "distance",  JointType::DISTANCE  },
+		{ "d6",        JointType::D6        }
 	};
 
 	static uint32_t shape_type_to_enum(const char* type)
 	{
-		for (uint32_t i = 0; i < PhysicsShapeType::COUNT; i++)
+		for (uint32_t i = 0; i < ShapeType::COUNT; i++)
 		{
 			if (strcmp(type, s_shape[i].name) == 0)
 				return s_shape[i].type;
@@ -62,7 +62,7 @@ namespace physics_resource
 
 	static uint32_t joint_type_to_enum(const char* type)
 	{
-		for (uint32_t i = 0; i < PhysicsJointType::COUNT; i++)
+		for (uint32_t i = 0; i < JointType::COUNT; i++)
 		{
 			if (strcmp(type, s_joint[i].name) == 0)
 				return s_joint[i].type;
@@ -72,7 +72,7 @@ namespace physics_resource
 		return 0;
 	}
 
-	void parse_controller(JSONElement e, PhysicsController& controller)
+	void parse_controller(JSONElement e, ControllerResource& controller)
 	{
 		controller.name =             e.key("name").to_string_id();
 		controller.height =           e.key("height").to_float();
@@ -83,7 +83,7 @@ namespace physics_resource
 		controller.collision_filter = e.key("collision_filter").to_string_id();
 	}
 
-	void parse_shapes(JSONElement e, Array<PhysicsShape>& shapes)
+	void parse_shapes(JSONElement e, Array<ShapeResource>& shapes)
 	{
 		Vector<DynamicString> keys(default_allocator());
 		e.to_keys(keys);
@@ -92,55 +92,55 @@ namespace physics_resource
 		{
 			JSONElement shape 		= e.key(keys[k].c_str());
 
-			PhysicsShape ps;
-			ps.name =        keys[k].to_string_id();
-			ps.shape_class = shape.key("class").to_string_id();
-			ps.material =    shape.key("material").to_string_id();
-			ps.position =    shape.key("position").to_vector3();
-			ps.rotation =    shape.key("rotation").to_quaternion();
+			ShapeResource sr;
+			sr.name =        keys[k].to_string_id();
+			sr.shape_class = shape.key("class").to_string_id();
+			sr.material =    shape.key("material").to_string_id();
+			sr.position =    shape.key("position").to_vector3();
+			sr.rotation =    shape.key("rotation").to_quaternion();
 
 			DynamicString stype; shape.key("type").to_string(stype);
-			ps.type = shape_type_to_enum(stype.c_str());
+			sr.type = shape_type_to_enum(stype.c_str());
 
-			switch (ps.type)
+			switch (sr.type)
 			{
-				case PhysicsShapeType::SPHERE:
+				case ShapeType::SPHERE:
 				{
-					ps.data_0 = shape.key("radius").to_float();
+					sr.data_0 = shape.key("radius").to_float();
 					break;
 				}
-				case PhysicsShapeType::CAPSULE:
+				case ShapeType::CAPSULE:
 				{
-					ps.data_0 = shape.key("radius").to_float();
-					ps.data_1 = shape.key("half_height").to_float();
+					sr.data_0 = shape.key("radius").to_float();
+					sr.data_1 = shape.key("half_height").to_float();
 					break;
 				}
-				case PhysicsShapeType::BOX:
+				case ShapeType::BOX:
 				{
-					ps.data_0 = shape.key("half_x").to_float();
-					ps.data_1 = shape.key("half_y").to_float();
-					ps.data_2 = shape.key("half_z").to_float();
+					sr.data_0 = shape.key("half_x").to_float();
+					sr.data_1 = shape.key("half_y").to_float();
+					sr.data_2 = shape.key("half_z").to_float();
 					break;
 				}
-				case PhysicsShapeType::PLANE:
+				case ShapeType::PLANE:
 				{
-					ps.data_0 = shape.key("n_x").to_float();
-					ps.data_1 = shape.key("n_y").to_float();
-					ps.data_2 = shape.key("n_z").to_float();
-					ps.data_3 = shape.key("distance").to_float();
+					sr.data_0 = shape.key("n_x").to_float();
+					sr.data_1 = shape.key("n_y").to_float();
+					sr.data_2 = shape.key("n_z").to_float();
+					sr.data_3 = shape.key("distance").to_float();
 					break;
 				}
-				case PhysicsShapeType::CONVEX_MESH:
+				case ShapeType::CONVEX_MESH:
 				{
-					ps.resource = shape.key("mesh").to_resource_id("mesh");
+					sr.resource = shape.key("mesh").to_resource_id("mesh");
 					break;
 				}
 			}
-			array::push_back(shapes, ps);
+			array::push_back(shapes, sr);
 		}
 	}
 
-	void parse_actors(JSONElement e, Array<PhysicsActor>& actors, Array<PhysicsShape>& actor_shapes, Array<uint32_t>& shape_indices)
+	void parse_actors(JSONElement e, Array<ActorResource>& actors, Array<ShapeResource>& actor_shapes, Array<uint32_t>& shape_indices)
 	{
 		Vector<DynamicString> keys(default_allocator());
 		e.to_keys(keys);
@@ -149,7 +149,7 @@ namespace physics_resource
 		{
 			JSONElement actor 	= e.key(keys[k].c_str());
 
-			PhysicsActor pa;
+			ActorResource pa;
 			pa.name =        keys[k].to_string_id();
 			pa.node =        actor.key("node").to_string_id();
 			pa.actor_class = actor.key("class").to_string_id();
@@ -163,7 +163,7 @@ namespace physics_resource
 		}
 	}
 
-	void parse_joints(JSONElement e, Array<PhysicsJoint>& joints)
+	void parse_joints(JSONElement e, Array<JointResource>& joints)
 	{
 		Vector<DynamicString> keys(default_allocator());
 		e.to_keys(keys);
@@ -173,7 +173,7 @@ namespace physics_resource
 			JSONElement joint			= e.key(keys[k].c_str());
 			JSONElement type 			= joint.key("type");
 
-			PhysicsJoint pj;
+			JointResource pj;
 			pj.name = keys[k].to_string_id();
 			DynamicString jtype; type.to_string(jtype);
 			pj.type         = joint_type_to_enum(jtype.c_str());
@@ -191,31 +191,31 @@ namespace physics_resource
 
 			switch (pj.type)
 			{
-				case PhysicsJointType::FIXED:
+				case JointType::FIXED:
 				{
 					return;
 				}
-				case PhysicsJointType::SPHERICAL:
+				case JointType::SPHERICAL:
 				{
 					pj.y_limit_angle = joint.key_or_nil("y_limit_angle").to_float(HALF_PI);
 					pj.z_limit_angle = joint.key_or_nil("z_limit_angle").to_float(HALF_PI);
 					pj.contact_dist =  joint.key_or_nil("contact_dist").to_float(0.0f);
 					break;
 				}
-				case PhysicsJointType::REVOLUTE:
-				case PhysicsJointType::PRISMATIC:
+				case JointType::REVOLUTE:
+				case JointType::PRISMATIC:
 				{
 					pj.lower_limit =  joint.key_or_nil("lower_limit").to_float(0.0f);
 					pj.upper_limit =  joint.key_or_nil("upper_limit").to_float(0.0f);
 					pj.contact_dist = joint.key_or_nil("contact_dist").to_float(0.0f);
 					break;
 				}
-				case PhysicsJointType::DISTANCE:
+				case JointType::DISTANCE:
 				{
 					pj.max_distance = joint.key_or_nil("max_distance").to_float(0.0f);
 					break;
 				}
-				case PhysicsJointType::D6:
+				case JointType::D6:
 				{
 					CE_FATAL("Not implemented");
 					break;
@@ -235,7 +235,7 @@ namespace physics_resource
 		JSONElement root = json.root();
 
 		bool m_has_controller = false;
-		PhysicsController m_controller;
+		ControllerResource m_controller;
 
 		// Read controller
 		JSONElement controller = root.key_or_nil("controller");
@@ -249,10 +249,10 @@ namespace physics_resource
 			m_has_controller = true;
 		}
 
-		Array<PhysicsActor> m_actors(default_allocator());
+		Array<ActorResource> m_actors(default_allocator());
 		Array<uint32_t> m_shapes_indices(default_allocator());
-		Array<PhysicsShape> m_shapes(default_allocator());
-		Array<PhysicsJoint> m_joints(default_allocator());
+		Array<ShapeResource> m_shapes(default_allocator());
+		Array<JointResource> m_joints(default_allocator());
 
 		if (root.has_key("actors")) parse_actors(root.key("actors"), m_actors, m_shapes, m_shapes_indices);
 		if (root.has_key("joints")) parse_joints(root.key("joints"), m_joints);
@@ -261,15 +261,11 @@ namespace physics_resource
 		pr.version = VERSION;
 		pr.num_controllers = m_has_controller ? 1 : 0;
 		pr.num_actors = array::size(m_actors);
-		pr.num_shapes_indices = array::size(m_shapes_indices);
-		pr.num_shapes = array::size(m_shapes);
 		pr.num_joints = array::size(m_joints);
 
 		uint32_t offt = sizeof(PhysicsResource);
-		pr.controller_offset = offt; offt += sizeof(PhysicsController) * pr.num_controllers;
-		pr.actors_offset = offt; offt += sizeof(PhysicsActor) * pr.num_actors;
-		pr.shapes_indices_offset = offt; offt += sizeof(uint32_t) * pr.num_shapes_indices;
-		pr.shapes_offset = offt; offt += sizeof(PhysicsShape) * pr.num_shapes;
+		pr.controller_offset = offt; offt += sizeof(ControllerResource) * pr.num_controllers;
+		pr.actors_offset = offt; offt += sizeof(ActorResource) * pr.num_actors;
 		pr.joints_offset = offt;
 
 		// Write all
@@ -278,10 +274,6 @@ namespace physics_resource
 		opts.write(pr.controller_offset);
 		opts.write(pr.num_actors);
 		opts.write(pr.actors_offset);
-		opts.write(pr.num_shapes_indices);
-		opts.write(pr.shapes_indices_offset);
-		opts.write(pr.num_shapes);
-		opts.write(pr.shapes_offset);
 		opts.write(pr.num_joints);
 		opts.write(pr.joints_offset);
 
@@ -303,26 +295,23 @@ namespace physics_resource
 			opts.write(m_actors[i].actor_class);
 			opts.write(m_actors[i].mass);
 			opts.write(m_actors[i].num_shapes);
-		}
-
-		for (uint32_t i = 0; i < array::size(m_shapes_indices); i++)
-		{
-			opts.write(m_shapes_indices[i]);
-		}
 
-		for (uint32_t i = 0; i < array::size(m_shapes); i++)
-		{
-			opts.write(m_shapes[i].name);
-			opts.write(m_shapes[i].shape_class);
-			opts.write(m_shapes[i].type);
-			opts.write(m_shapes[i].material);
-			opts.write(m_shapes[i].resource);
-			opts.write(m_shapes[i].position);
-			opts.write(m_shapes[i].rotation);
-			opts.write(m_shapes[i].data_0);
-			opts.write(m_shapes[i].data_1);
-			opts.write(m_shapes[i].data_2);
-			opts.write(m_shapes[i].data_3);
+			for (uint32_t ss = 0; ss < m_actors[i].num_shapes; ++ss)
+			{
+				const uint32_t base = m_shapes_indices[i];
+
+				opts.write(m_shapes[base + ss].name);
+				opts.write(m_shapes[base + ss].shape_class);
+				opts.write(m_shapes[base + ss].type);
+				opts.write(m_shapes[base + ss].material);
+				opts.write(m_shapes[base + ss].resource);
+				opts.write(m_shapes[base + ss].position);
+				opts.write(m_shapes[base + ss].rotation);
+				opts.write(m_shapes[base + ss].data_0);
+				opts.write(m_shapes[base + ss].data_1);
+				opts.write(m_shapes[base + ss].data_2);
+				opts.write(m_shapes[base + ss].data_3);
+			}
 		}
 
 		for (uint32_t i = 0; i < array::size(m_joints); i++)
@@ -378,10 +367,10 @@ namespace physics_resource
 		return pr->num_controllers == 1;
 	}
 
-	const PhysicsController* controller(const PhysicsResource* pr)
+	const ControllerResource* controller(const PhysicsResource* pr)
 	{
 		CE_ASSERT(has_controller(pr), "Controller does not exist");
-		PhysicsController* controller = (PhysicsController*) ((char*)pr + pr->controller_offset);
+		ControllerResource* controller = (ControllerResource*) ((char*)pr + pr->controller_offset);
 		return controller;
 	}
 
@@ -390,49 +379,40 @@ namespace physics_resource
 		return pr->num_actors;
 	}
 
-	const PhysicsActor* actor(const PhysicsResource* pr, uint32_t i)
+	const ActorResource* actor(const PhysicsResource* pr, uint32_t i)
 	{
 		CE_ASSERT(i < num_actors(pr), "Index out of bounds");
-		PhysicsActor* actor = (PhysicsActor*) ((char*)pr + pr->actors_offset);
+		ActorResource* actor = (ActorResource*) ((char*)pr + pr->actors_offset);
 		return &actor[i];
 	}
 
-	uint32_t num_shapes_indices(const PhysicsResource* pr)
+	uint32_t num_joints(const PhysicsResource* pr)
 	{
-		return pr->num_shapes_indices;
+		return pr->num_joints;
 	}
 
-	uint32_t shape_index(const PhysicsResource* pr, uint32_t i)
+	const JointResource* joint(const PhysicsResource* pr, uint32_t i)
 	{
-		CE_ASSERT(i < num_shapes_indices(pr), "Index out of bounds");
-		uint32_t* index = (uint32_t*) ((char*)pr + pr->shapes_indices_offset);
-		return index[i];
+		CE_ASSERT(i < num_joints(pr), "Index out of bounds");
+		JointResource* joint = (JointResource*) ((char*)pr + pr->joints_offset);
+		return &joint[i];
 	}
+} // namespace physics_resource
 
-	uint32_t num_shapes(const PhysicsResource* pr)
+namespace actor_resource
+{
+	uint32_t num_shapes(const ActorResource* ar)
 	{
-		return pr->num_shapes;
+		return ar->num_shapes;
 	}
 
-	const PhysicsShape* shape(const PhysicsResource* pr, uint32_t i)
+	const ShapeResource* shape(const ActorResource* ar, uint32_t i)
 	{
-		CE_ASSERT(i < num_shapes(pr), "Index out of bounds");
-		PhysicsShape* shape = (PhysicsShape*) ((char*)pr + pr->shapes_offset);
+		CE_ASSERT(i < num_shapes(ar), "Index out of bounds");
+		ShapeResource* shape = (ShapeResource*) (ar + 1);
 		return &shape[i];
 	}
-
-	uint32_t num_joints(const PhysicsResource* pr)
-	{
-		return pr->num_joints;
-	}
-
-	const PhysicsJoint* joint(const PhysicsResource* pr, uint32_t i)
-	{
-		CE_ASSERT(i < num_joints(pr), "Index out of bounds");
-		PhysicsJoint* joint = (PhysicsJoint*) ((char*)pr + pr->joints_offset);
-		return &joint[i];
-	}
-} // namespace physics_resource
+} // namespace actor_resource
 
 namespace physics_config_resource
 {

+ 13 - 44
engine/resource/physics_resource.h

@@ -22,15 +22,11 @@ struct PhysicsResource
 	uint32_t controller_offset;
 	uint32_t num_actors;
 	uint32_t actors_offset;
-	uint32_t num_shapes_indices;
-	uint32_t shapes_indices_offset;
-	uint32_t num_shapes;
-	uint32_t shapes_offset;
 	uint32_t num_joints;
 	uint32_t joints_offset;
 };
 
-struct PhysicsController
+struct ControllerResource
 {
 	StringId32 name;
 	float height;				// Height of the capsule
@@ -41,7 +37,7 @@ struct PhysicsController
 	StringId32 collision_filter;// Collision filter from global.physics_config
 };
 
-struct PhysicsActor
+struct ActorResource
 {
 	StringId32 name;			// Name of the actor
 	StringId32 node;			// Node from .unit file
@@ -50,21 +46,7 @@ struct PhysicsActor
 	uint32_t num_shapes;		// Number of shapes
 };
 
-struct PhysicsShapeType
-{
-	enum Enum
-	{
-		SPHERE,
-		CAPSULE,
-		BOX,
-		PLANE,
-		CONVEX_MESH,
-
-		COUNT
-	};
-};
-
-struct PhysicsShape
+struct ShapeResource
 {
 	StringId32 name;			// Name of the shape
 	StringId32 shape_class;		// Shape class from global.physics_config
@@ -79,22 +61,7 @@ struct PhysicsShape
 	float data_3;
 };
 
-struct PhysicsJointType
-{
-	enum Enum
-	{
-		FIXED,
-		SPHERICAL,
-		REVOLUTE,
-		PRISMATIC,
-		DISTANCE,
-		D6,
-
-		COUNT
-	};
-};
-
-struct PhysicsJoint
+struct JointResource
 {
 	StringId32 name;
 	uint32_t type;
@@ -137,17 +104,19 @@ namespace physics_resource
 	void unload(Allocator& allocator, void* resource);
 
 	bool has_controller(const PhysicsResource* pr);
-	const PhysicsController* controller(const PhysicsResource* pr);
+	const ControllerResource* controller(const PhysicsResource* pr);
 	uint32_t num_actors(const PhysicsResource* pr);
-	const PhysicsActor* actor(const PhysicsResource* pr, uint32_t i);
-	uint32_t num_shapes_indices(const PhysicsResource* pr);
-	uint32_t shape_index(const PhysicsResource* pr, uint32_t i);
-	uint32_t num_shapes(const PhysicsResource* pr);
-	const PhysicsShape* shape(const PhysicsResource* pr, uint32_t i);
+	const ActorResource* actor(const PhysicsResource* pr, uint32_t i);
 	uint32_t num_joints(const PhysicsResource* pr);
-	const PhysicsJoint* joint(const PhysicsResource* pr, uint32_t i);
+	const JointResource* joint(const PhysicsResource* pr, uint32_t i);
 } // namespace physics_resource
 
+namespace actor_resource
+{
+	uint32_t num_shapes(const ActorResource* ar);
+	const ShapeResource* shape(const ActorResource* ar, uint32_t i);
+}
+
 struct PhysicsConfigResource
 {
 	uint32_t version;

+ 4 - 0
engine/resource/resource_types.h

@@ -68,4 +68,8 @@ namespace crown
 	struct SpriteResource;
 	struct TextureResource;
 	struct UnitResource;
+	struct ActorResource;
+	struct ShapeResource;
+	struct JointResource;
+	struct ControllerResource;
 } // namespace crown

+ 10 - 8
engine/world/unit.cpp

@@ -181,27 +181,29 @@ void Unit::create_physics_objects()
 		// Create controller if any
 		if (has_controller(pr))
 		{
-			set_controller(physics_resource::controller(pr)->name, m_world.physics_world()->create_controller(pr, m_scene_graph, 0));
+			const ControllerResource* cr = physics_resource::controller(pr);
+
+			set_controller(cr->name, m_world.physics_world()->create_controller(cr, m_scene_graph, 0));
 		}
 
 		// Create actors if any
 		for (uint32_t i = 0; i < num_actors(pr); i++)
 		{
-			const PhysicsActor* actor = physics_resource::actor(pr, i);
+			const ActorResource* ar = physics_resource::actor(pr, i);
 
-			ActorId id = m_world.physics_world()->create_actor(pr, i, m_scene_graph, m_scene_graph.node(actor->node), m_id);
-			add_actor(actor->name, id);
+			ActorId id = m_world.physics_world()->create_actor(ar, m_scene_graph, m_scene_graph.node(ar->node), m_id);
+			add_actor(ar->name, id);
 		}
 
 		// Create joints if any
 		for (uint32_t i = 0; i < num_joints(pr); i++)
 		{
-			const PhysicsJoint* joint = physics_resource::joint(pr, i);
+			const JointResource* jr = physics_resource::joint(pr, i);
 
-			Actor* a1 = actor_by_index(joint->actor_0);
-			Actor* a2 = actor_by_index(joint->actor_1);
+			Actor* a1 = actor_by_index(jr->actor_0);
+			Actor* a2 = actor_by_index(jr->actor_1);
 
-			m_world.physics_world()->create_joint(pr, i, *a1, *a2);
+			m_world.physics_world()->create_joint(jr, *a1, *a2);
 		}
 	}
 }