physics_resource.cpp 20 KB

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