PhysicsResource.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <algorithm>
  24. #include "Allocator.h"
  25. #include "Filesystem.h"
  26. #include "StringUtils.h"
  27. #include "JSONParser.h"
  28. #include "PhysicsResource.h"
  29. #include "StringUtils.h"
  30. #include "DynamicString.h"
  31. #include "Map.h"
  32. namespace crown
  33. {
  34. namespace physics_resource
  35. {
  36. //-----------------------------------------------------------------------------
  37. static uint32_t shape_type_to_enum(const char* type)
  38. {
  39. if (string::strcmp("sphere", type) == 0) return PhysicsShapeType::SPHERE;
  40. else if (string::strcmp("capsule", type) == 0) return PhysicsShapeType::CAPSULE;
  41. else if (string::strcmp("box", type) == 0) return PhysicsShapeType::BOX;
  42. else if (string::strcmp("plane", type) == 0) return PhysicsShapeType::PLANE;
  43. CE_FATAL("Bad shape type");
  44. return 0;
  45. }
  46. //-----------------------------------------------------------------------------
  47. static uint32_t joint_type_to_enum(const char* type)
  48. {
  49. if (string::strcmp("fixed", type) == 0) return PhysicsJointType::FIXED;
  50. else if (string::strcmp("spherical", type) == 0) return PhysicsJointType::SPHERICAL;
  51. else if (string::strcmp("revolute", type) == 0) return PhysicsJointType::REVOLUTE;
  52. else if (string::strcmp("prismatic", type) == 0) return PhysicsJointType::PRISMATIC;
  53. else if (string::strcmp("distance", type) == 0) return PhysicsJointType::DISTANCE;
  54. else if (string::strcmp("d6", type) == 0) return PhysicsJointType::D6;
  55. CE_FATAL("Bad joint type");
  56. return 0;
  57. }
  58. //-----------------------------------------------------------------------------
  59. void parse_controller(JSONElement e, PhysicsController& controller)
  60. {
  61. JSONElement name = e.key("name");
  62. JSONElement height = e.key("height");
  63. JSONElement radius = e.key("radius");
  64. JSONElement slope_limit = e.key("slope_limit");
  65. JSONElement step_offset = e.key("step_offset");
  66. JSONElement contact_offset = e.key("contact_offset");
  67. JSONElement collision_filter = e.key("collision_filter");
  68. controller.name = name.to_string_id();
  69. controller.height = height.to_float();
  70. controller.radius = radius.to_float();
  71. controller.slope_limit = slope_limit.to_float();
  72. controller.step_offset = step_offset.to_float();
  73. controller.contact_offset = contact_offset.to_float();
  74. controller.collision_filter = collision_filter.to_string_id();
  75. }
  76. //-----------------------------------------------------------------------------
  77. void parse_shapes(JSONElement e, Array<PhysicsShape>& shapes)
  78. {
  79. Vector<DynamicString> keys(default_allocator());
  80. e.to_keys(keys);
  81. for (uint32_t k = 0; k < vector::size(keys); k++)
  82. {
  83. JSONElement shape = e.key(keys[k].c_str());
  84. JSONElement clasz = shape.key("class");
  85. JSONElement type = shape.key("type");
  86. JSONElement material = shape.key("material");
  87. PhysicsShape ps;
  88. ps.name = keys[k].to_string_id();
  89. ps.shape_class = clasz.to_string_id();
  90. ps.material = material.to_string_id();
  91. DynamicString stype; type.to_string(stype);
  92. ps.type = shape_type_to_enum(stype.c_str());
  93. switch (ps.type)
  94. {
  95. case PhysicsShapeType::SPHERE:
  96. {
  97. JSONElement radius = shape.key("radius");
  98. ps.data_0 = radius.to_float();
  99. break;
  100. }
  101. case PhysicsShapeType::CAPSULE:
  102. {
  103. JSONElement radius = shape.key("radius");
  104. JSONElement half_height = shape.key("half_height");
  105. ps.data_0 = radius.to_float();
  106. ps.data_1 = half_height.to_float();
  107. break;
  108. }
  109. case PhysicsShapeType::BOX:
  110. {
  111. JSONElement half_x = shape.key("half_x");
  112. JSONElement half_y = shape.key("half_y");
  113. JSONElement half_z = shape.key("half_z");
  114. ps.data_0 = half_x.to_float();
  115. ps.data_1 = half_y.to_float();
  116. ps.data_2 = half_z.to_float();
  117. break;
  118. }
  119. case PhysicsShapeType::PLANE:
  120. {
  121. JSONElement n_x = shape.key("n_x");
  122. JSONElement n_y = shape.key("n_y");
  123. JSONElement n_z = shape.key("n_z");
  124. JSONElement distance = shape.key("distance");
  125. ps.data_0 = n_x.to_float();
  126. ps.data_1 = n_y.to_float();
  127. ps.data_2 = n_z.to_float();
  128. ps.data_3 = distance.to_float();
  129. break;
  130. }
  131. case PhysicsShapeType::CONVEX_MESH:
  132. {
  133. JSONElement resource = shape.key("mesh");
  134. DynamicString mesh_name;
  135. resource.to_string(mesh_name);
  136. mesh_name += ".mesh";
  137. StringId64 mesh_id = string::murmur2_64(mesh_name.c_str(), string::strlen(mesh_name.c_str()), 0);
  138. ps.resource.id = mesh_id;
  139. break;
  140. }
  141. }
  142. array::push_back(shapes, ps);
  143. }
  144. }
  145. //-----------------------------------------------------------------------------
  146. void parse_actors(JSONElement e, Array<PhysicsActor>& actors, Array<PhysicsShape>& actor_shapes, Array<uint32_t>& shape_indices)
  147. {
  148. Vector<DynamicString> keys(default_allocator());
  149. e.to_keys(keys);
  150. for (uint32_t k = 0; k < vector::size(keys); k++)
  151. {
  152. JSONElement actor = e.key(keys[k].c_str());
  153. JSONElement node = actor.key("node");
  154. JSONElement clasz = actor.key("class");
  155. JSONElement shapes = actor.key("shapes");
  156. JSONElement mass = actor.key("mass");
  157. PhysicsActor pa;
  158. pa.name = keys[k].to_string_id();
  159. pa.node = node.to_string_id();
  160. pa.actor_class = clasz.to_string_id();
  161. pa.mass = mass.to_float();
  162. pa.num_shapes = shapes.size();
  163. array::push_back(actors, pa);
  164. array::push_back(shape_indices, array::size(shape_indices));
  165. parse_shapes(shapes, actor_shapes);
  166. }
  167. }
  168. //-----------------------------------------------------------------------------
  169. void parse_joints(JSONElement e, Array<PhysicsJoint>& joints)
  170. {
  171. Vector<DynamicString> keys(default_allocator());
  172. e.to_keys(keys);
  173. for (uint32_t k = 0; k < vector::size(keys); k++)
  174. {
  175. JSONElement joint = e.key(keys[k].c_str());
  176. JSONElement type = joint.key("type");
  177. JSONElement actor_0 = joint.key("actor_0");
  178. JSONElement actor_1 = joint.key("actor_1");
  179. JSONElement anchor_0 = joint.key("anchor_0");
  180. JSONElement anchor_1 = joint.key("anchor_1");
  181. JSONElement restitution = joint.key_or_nil("restitution");
  182. JSONElement spring = joint.key_or_nil("spring");
  183. JSONElement damping = joint.key_or_nil("damping");
  184. JSONElement distance = joint.key_or_nil("distance");
  185. JSONElement breakable = joint.key_or_nil("breakable");
  186. JSONElement break_force = joint.key_or_nil("break_force");
  187. JSONElement break_torque = joint.key_or_nil("break_torque");
  188. PhysicsJoint pj;
  189. pj.name = keys[k].to_string_id();
  190. DynamicString jtype; type.to_string(jtype);
  191. pj.type = joint_type_to_enum(jtype.c_str());
  192. pj.actor_0 = actor_0.to_string_id();
  193. pj.actor_1 = actor_1.to_string_id();
  194. pj.anchor_0 = Vector3(anchor_0[0].to_float(), anchor_0[1].to_float(), anchor_0[2].to_float());
  195. pj.anchor_1 = Vector3(anchor_1[0].to_float(), anchor_1[1].to_float(), anchor_1[2].to_float());
  196. pj.restitution = restitution.is_nil() ? 0.5 : restitution.to_float();
  197. pj.spring = spring.is_nil() ? 100.0 : spring.to_float();
  198. pj.damping = damping.is_nil() ? 0.0 : damping.to_float();
  199. pj.distance = distance.is_nil() ? 1.0 : distance.to_float();
  200. pj.breakable = breakable.is_nil() ? false : breakable.to_bool();
  201. pj.break_force = break_force.is_nil() ? 3000.0 : break_force.to_float();
  202. pj.break_torque = break_torque.is_nil() ? 1000.0 : break_torque.to_float();
  203. switch (pj.type)
  204. {
  205. case PhysicsJointType::FIXED:
  206. {
  207. return;
  208. }
  209. case PhysicsJointType::SPHERICAL:
  210. {
  211. JSONElement y_limit_angle = joint.key_or_nil("y_limit_angle");
  212. JSONElement z_limit_angle = joint.key_or_nil("z_limit_angle");
  213. JSONElement contact_dist = joint.key_or_nil("contact_dist");
  214. pj.y_limit_angle = y_limit_angle.is_nil() ? math::HALF_PI : y_limit_angle.to_float();
  215. pj.z_limit_angle = z_limit_angle.is_nil() ? math::HALF_PI : z_limit_angle.to_float();
  216. pj.contact_dist = contact_dist.is_nil() ? 0.0 : contact_dist.to_float();
  217. break;
  218. }
  219. case PhysicsJointType::REVOLUTE:
  220. case PhysicsJointType::PRISMATIC:
  221. {
  222. JSONElement lower_limit = joint.key_or_nil("lower_limit");
  223. JSONElement upper_limit = joint.key_or_nil("upper_limit");
  224. JSONElement contact_dist = joint.key_or_nil("contact_dist");
  225. pj.lower_limit = lower_limit.is_nil() ? 0.0 : lower_limit.to_float();
  226. pj.upper_limit = upper_limit.is_nil() ? 0.0 : upper_limit.to_float();
  227. pj.contact_dist = contact_dist.is_nil() ? 0.0 : contact_dist.to_float();
  228. break;
  229. }
  230. case PhysicsJointType::DISTANCE:
  231. {
  232. JSONElement max_distance = joint.key_or_nil("max_distance");
  233. pj.max_distance = max_distance.is_nil() ? 0.0 : max_distance.to_float();
  234. break;
  235. }
  236. case PhysicsJointType::D6:
  237. {
  238. // Must be implemented
  239. break;
  240. }
  241. }
  242. array::push_back(joints, pj);
  243. }
  244. }
  245. //-----------------------------------------------------------------------------
  246. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  247. {
  248. File* file = fs.open(resource_path, FOM_READ);
  249. char* buf = (char*)default_allocator().allocate(file->size());
  250. file->read(buf, file->size());
  251. JSONParser json(buf);
  252. JSONElement root = json.root();
  253. bool m_has_controller = false;
  254. PhysicsController m_controller;
  255. // Read controller
  256. JSONElement controller = root.key_or_nil("controller");
  257. if (controller.is_nil())
  258. {
  259. m_has_controller = false;
  260. }
  261. else
  262. {
  263. parse_controller(controller, m_controller);
  264. m_has_controller = true;
  265. }
  266. Array<PhysicsActor> m_actors(default_allocator());
  267. Array<uint32_t> m_shapes_indices(default_allocator());
  268. Array<PhysicsShape> m_shapes(default_allocator());
  269. Array<PhysicsJoint> m_joints(default_allocator());
  270. if (root.has_key("actors")) parse_actors(root.key("actors"), m_actors, m_shapes, m_shapes_indices);
  271. if (root.has_key("joints")) parse_joints(root.key("joints"), m_joints);
  272. fs.close(file);
  273. default_allocator().deallocate(buf);
  274. PhysicsHeader h;
  275. h.version = 1;
  276. h.num_controllers = m_has_controller ? 1 : 0;
  277. h.num_actors = array::size(m_actors);
  278. h.num_shapes_indices = array::size(m_shapes_indices);
  279. h.num_shapes = array::size(m_shapes);
  280. h.num_joints = array::size(m_joints);
  281. uint32_t offt = sizeof(PhysicsHeader);
  282. h.controller_offset = offt; offt += sizeof(PhysicsController) * h.num_controllers;
  283. h.actors_offset = offt; offt += sizeof(PhysicsActor) * h.num_actors;
  284. h.shapes_indices_offset = offt; offt += sizeof(uint32_t) * h.num_shapes_indices;
  285. h.shapes_offset = offt; offt += sizeof(PhysicsShape) * h.num_shapes;
  286. h.joints_offset = offt;
  287. out_file->write((char*) &h, sizeof(PhysicsHeader));
  288. if (m_has_controller)
  289. {
  290. out_file->write((char*) &m_controller, sizeof(PhysicsController));
  291. }
  292. if (array::size(m_actors))
  293. {
  294. out_file->write((char*) array::begin(m_actors), sizeof(PhysicsActor) * array::size(m_actors));
  295. }
  296. if (array::size(m_shapes_indices))
  297. {
  298. out_file->write((char*) array::begin(m_shapes_indices), sizeof(uint32_t) * array::size(m_shapes_indices));
  299. }
  300. if (array::size(m_shapes))
  301. {
  302. out_file->write((char*) array::begin(m_shapes), sizeof(PhysicsShape) * array::size(m_shapes));
  303. }
  304. if (array::size(m_joints))
  305. {
  306. out_file->write((char*) array::begin(m_joints), sizeof(PhysicsJoint) * array::size(m_joints));
  307. }
  308. }
  309. } // namespace physics_resource
  310. namespace physics_config_resource
  311. {
  312. static Map<DynamicString, uint32_t>* s_ftm = NULL;
  313. struct ObjectName
  314. {
  315. StringId32 name;
  316. uint32_t index;
  317. bool operator()(const ObjectName& a, const ObjectName& b)
  318. {
  319. return a.name < b.name;
  320. }
  321. };
  322. void parse_materials(JSONElement e, Array<ObjectName>& names, Array<PhysicsMaterial>& objects)
  323. {
  324. Vector<DynamicString> keys(default_allocator());
  325. e.to_keys(keys);
  326. for (uint32_t i = 0; i < vector::size(keys); i++)
  327. {
  328. JSONElement material = e.key(keys[i].c_str());
  329. JSONElement static_friction = material.key("static_friction");
  330. JSONElement dynamic_friction = material.key("dynamic_friction");
  331. JSONElement restitution = material.key("restitution");
  332. // Read material name
  333. ObjectName mat_name;
  334. mat_name.name = keys[i].to_string_id();
  335. mat_name.index = i;
  336. // Read material object
  337. PhysicsMaterial mat;
  338. mat.static_friction = static_friction.to_float();
  339. mat.dynamic_friction = dynamic_friction.to_float();
  340. mat.restitution = restitution.to_float();
  341. array::push_back(names, mat_name);
  342. array::push_back(objects, mat);
  343. }
  344. }
  345. void parse_shapes(JSONElement e, Array<ObjectName>& names, Array<PhysicsShape2>& objects)
  346. {
  347. Vector<DynamicString> keys(default_allocator());
  348. e.to_keys(keys);
  349. for (uint32_t i = 0; i < vector::size(keys); i++)
  350. {
  351. JSONElement shape = e.key(keys[i].c_str());
  352. JSONElement collision_filter = shape.key("collision_filter");
  353. JSONElement trigger = shape.key("trigger");
  354. // Read shape name
  355. ObjectName shape_name;
  356. shape_name.name = keys[i].to_string_id();
  357. shape_name.index = i;
  358. // Read shape object
  359. PhysicsShape2 ps2;
  360. ps2.trigger = trigger.to_bool();
  361. ps2.collision_filter = collision_filter.to_string_id();
  362. array::push_back(names, shape_name);
  363. array::push_back(objects, ps2);
  364. }
  365. }
  366. void parse_actors(JSONElement e, Array<ObjectName>& names, Array<PhysicsActor2>& objects)
  367. {
  368. Vector<DynamicString> keys(default_allocator());
  369. e.to_keys(keys);
  370. for (uint32_t i = 0; i < vector::size(keys); i++)
  371. {
  372. JSONElement actor = e.key(keys[i].c_str());
  373. JSONElement linear_damping = actor.key_or_nil("linear_damping");
  374. JSONElement angular_damping = actor.key_or_nil("angular_damping");
  375. JSONElement dynamic = actor.key_or_nil("dynamic");
  376. JSONElement kinematic = actor.key_or_nil("kinematic");
  377. JSONElement disable_gravity = actor.key_or_nil("disable_gravity");
  378. // Read actor name
  379. ObjectName actor_name;
  380. actor_name.name = keys[i].to_string_id();
  381. actor_name.index = i;
  382. // Read actor object
  383. PhysicsActor2 pa2;
  384. pa2.linear_damping = linear_damping.is_nil() ? 0.0 : linear_damping.to_float();
  385. pa2.angular_damping = angular_damping.is_nil() ? 0.05 : angular_damping.to_float();
  386. pa2.flags = 0;
  387. if (!dynamic.is_nil())
  388. {
  389. pa2.flags |= dynamic.to_bool() ? 1 : 0;
  390. }
  391. if (!kinematic.is_nil())
  392. {
  393. pa2.flags |= kinematic.to_bool() ? PhysicsActor2::KINEMATIC : 0;
  394. }
  395. if (!disable_gravity.is_nil())
  396. {
  397. pa2.flags |= disable_gravity.to_bool() ? PhysicsActor2::DISABLE_GRAVITY : 0;
  398. }
  399. array::push_back(names, actor_name);
  400. array::push_back(objects, pa2);
  401. }
  402. }
  403. uint32_t new_filter_mask()
  404. {
  405. static uint32_t mask = 1;
  406. CE_ASSERT(mask != 0x80000000u, "Too many collision filters");
  407. uint32_t tmp = mask;
  408. mask = mask << 1;
  409. return tmp;
  410. }
  411. uint32_t filter_to_mask(const char* f)
  412. {
  413. if (map::has(*s_ftm, DynamicString(f)))
  414. return map::get(*s_ftm, DynamicString(f), 0u);
  415. uint32_t new_filter = new_filter_mask();
  416. map::set(*s_ftm, DynamicString(f), new_filter);
  417. return new_filter;
  418. }
  419. uint32_t collides_with_to_mask(const Vector<DynamicString>& coll_with)
  420. {
  421. uint32_t mask = 0;
  422. for (uint32_t i = 0; i < vector::size(coll_with); i++)
  423. {
  424. mask |= filter_to_mask(coll_with[i].c_str());
  425. }
  426. return mask;
  427. }
  428. void parse_collision_filters(JSONElement e, Array<ObjectName>& names, Array<PhysicsCollisionFilter>& objects)
  429. {
  430. Vector<DynamicString> keys(default_allocator());
  431. e.to_keys(keys);
  432. for (uint32_t i = 0; i < vector::size(keys); i++)
  433. {
  434. JSONElement filter = e.key(keys[i].c_str());
  435. JSONElement collides_with = filter.key("collides_with");
  436. // Read filter name
  437. ObjectName filter_name;
  438. filter_name.name = keys[i].to_string_id();
  439. filter_name.index = i;
  440. // Build mask
  441. Vector<DynamicString> collides_with_vector(default_allocator());
  442. collides_with.to_array(collides_with_vector);
  443. PhysicsCollisionFilter pcf;
  444. pcf.me = filter_to_mask(keys[i].c_str());
  445. pcf.mask = collides_with_to_mask(collides_with_vector);
  446. printf("FILTER: %s (me = %X, mask = %X\n", keys[i].c_str(), pcf.me, pcf.mask);
  447. array::push_back(names, filter_name);
  448. array::push_back(objects, pcf);
  449. }
  450. }
  451. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  452. {
  453. File* file = fs.open(resource_path, FOM_READ);
  454. char* buf = (char*)default_allocator().allocate(file->size());
  455. file->read(buf, file->size());
  456. JSONParser json(buf);
  457. JSONElement root = json.root();
  458. typedef Map<DynamicString, uint32_t> FilterMap;
  459. s_ftm = CE_NEW(default_allocator(), FilterMap)(default_allocator());
  460. Array<ObjectName> material_names(default_allocator());
  461. Array<PhysicsMaterial> material_objects(default_allocator());
  462. Array<ObjectName> shape_names(default_allocator());
  463. Array<PhysicsShape2> shape_objects(default_allocator());
  464. Array<ObjectName> actor_names(default_allocator());
  465. Array<PhysicsActor2> actor_objects(default_allocator());
  466. Array<ObjectName> filter_names(default_allocator());
  467. Array<PhysicsCollisionFilter> filter_objects(default_allocator());
  468. // Parse materials
  469. if (root.has_key("collision_filters")) parse_collision_filters(root.key("collision_filters"), filter_names, filter_objects);
  470. if (root.has_key("materials")) parse_materials(root.key("materials"), material_names, material_objects);
  471. if (root.has_key("shapes")) parse_shapes(root.key("shapes"), shape_names, shape_objects);
  472. if (root.has_key("actors")) parse_actors(root.key("actors"), actor_names, actor_objects);
  473. default_allocator().deallocate(buf);
  474. fs.close(file);
  475. // Sort objects by name
  476. std::sort(array::begin(material_names), array::end(material_names), ObjectName());
  477. std::sort(array::begin(shape_names), array::end(shape_names), ObjectName());
  478. std::sort(array::begin(actor_names), array::end(actor_names), ObjectName());
  479. std::sort(array::begin(filter_names), array::end(filter_names), ObjectName());
  480. // Setup struct for writing
  481. PhysicsConfigHeader header;
  482. header.num_materials = array::size(material_names);
  483. header.num_shapes = array::size(shape_names);
  484. header.num_actors = array::size(actor_names);
  485. header.num_filters = array::size(filter_names);
  486. uint32_t offt = sizeof(PhysicsConfigHeader);
  487. header.materials_offset = offt;
  488. offt += sizeof(StringId32) * header.num_materials;
  489. offt += sizeof(PhysicsMaterial) * header.num_materials;
  490. header.shapes_offset = offt;
  491. offt += sizeof(StringId32) * header.num_shapes;
  492. offt += sizeof(PhysicsShape2) * header.num_shapes;
  493. header.actors_offset = offt;
  494. offt += sizeof(StringId32) * header.num_actors;
  495. offt += sizeof(PhysicsActor2) * header.num_actors;
  496. header.filters_offset = offt;
  497. offt += sizeof(StringId32) * header.num_filters;
  498. offt += sizeof(PhysicsCollisionFilter) * header.num_filters;
  499. // Write all
  500. out_file->write((char*) &header, sizeof(PhysicsConfigHeader));
  501. if (header.num_materials)
  502. {
  503. // Write material names
  504. for (uint32_t i = 0; i < array::size(material_names); i++)
  505. {
  506. out_file->write((char*) &material_names[i].name, sizeof(StringId32));
  507. }
  508. // Write material objects
  509. for (uint32_t i = 0; i < array::size(material_names); i++)
  510. {
  511. out_file->write((char*) &material_objects[material_names[i].index], sizeof(PhysicsMaterial));
  512. }
  513. }
  514. if (header.num_shapes)
  515. {
  516. // Write shape names
  517. for (uint32_t i = 0; i < array::size(shape_names); i++)
  518. {
  519. out_file->write((char*) &shape_names[i].name, sizeof(StringId32));
  520. }
  521. // Write material objects
  522. for (uint32_t i = 0; i < array::size(shape_names); i++)
  523. {
  524. out_file->write((char*) &shape_objects[shape_names[i].index], sizeof(PhysicsShape2));
  525. }
  526. }
  527. if (header.num_actors)
  528. {
  529. // Write actor names
  530. for (uint32_t i = 0; i < array::size(actor_names); i++)
  531. {
  532. out_file->write((char*) &actor_names[i].name, sizeof(StringId32));
  533. }
  534. // Write actor objects
  535. for (uint32_t i = 0; i < array::size(actor_names); i++)
  536. {
  537. out_file->write((char*) &actor_objects[actor_names[i].index], sizeof(PhysicsActor2));
  538. }
  539. }
  540. if (header.num_filters)
  541. {
  542. // Write filter names
  543. for (uint32_t i = 0; i < array::size(filter_names); i++)
  544. {
  545. out_file->write((char*) &filter_names[i].name, sizeof(StringId32));
  546. }
  547. // Write filter objects
  548. for (uint32_t i = 0; i < array::size(filter_names); i++)
  549. {
  550. out_file->write((char*) &filter_objects[filter_names[i].index], sizeof(PhysicsCollisionFilter));
  551. }
  552. }
  553. CE_DELETE(default_allocator(), s_ftm);
  554. }
  555. } // namespace physics_config_resource
  556. } // namespace crown