PhysicsResource.cpp 20 KB

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