PhysicsResource.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. #pragma once
  24. #include <algorithm>
  25. #include "Types.h"
  26. #include "Allocator.h"
  27. #include "File.h"
  28. #include "Bundle.h"
  29. #include "ResourceManager.h"
  30. #include "Vector3.h"
  31. namespace crown
  32. {
  33. //-----------------------------------------------------------------------------
  34. struct PhysicsHeader
  35. {
  36. uint32_t version;
  37. uint32_t num_controllers; // 0 or 1, ATM
  38. uint32_t controller_offset;
  39. uint32_t num_actors;
  40. uint32_t actors_offset;
  41. uint32_t num_shapes_indices;
  42. uint32_t shapes_indices_offset;
  43. uint32_t num_shapes;
  44. uint32_t shapes_offset;
  45. uint32_t num_joints;
  46. uint32_t joints_offset;
  47. };
  48. //-----------------------------------------------------------------------------
  49. struct PhysicsController
  50. {
  51. StringId32 name;
  52. float height; // Height of the capsule
  53. float radius; // Radius of the capsule
  54. float slope_limit; // The maximum slope which the character can walk up in radians.
  55. float step_offset; // Maximum height of an obstacle which the character can climb.
  56. float contact_offset; // Skin around the object within which contacts will be generated. Use it to avoid numerical precision issues.
  57. StringId32 collision_filter;// Collision filter from global.physics_config
  58. };
  59. //-----------------------------------------------------------------------------
  60. struct PhysicsActor
  61. {
  62. StringId32 name; // Name of the actor
  63. StringId32 node; // Node from .unit file
  64. StringId32 actor_class; // Actor from global.physics
  65. float mass; // Mass of the actor
  66. uint32_t num_shapes; // Number of shapes
  67. };
  68. //-----------------------------------------------------------------------------
  69. struct PhysicsShapeType
  70. {
  71. enum Enum
  72. {
  73. SPHERE,
  74. CAPSULE,
  75. BOX,
  76. PLANE,
  77. CONVEX_MESH
  78. };
  79. };
  80. //-----------------------------------------------------------------------------
  81. struct PhysicsShape
  82. {
  83. StringId32 name; // Name of the shape
  84. StringId32 shape_class; // Shape class from global.physics_config
  85. StringId32 type; // Type of the shape
  86. StringId32 material; // Material from global.physics_config
  87. ResourceId resource; // Resource such as .mesh or .heightmap
  88. float data_0;
  89. float data_1;
  90. float data_2;
  91. float data_3;
  92. };
  93. //-----------------------------------------------------------------------------
  94. struct PhysicsJointType
  95. {
  96. enum Enum
  97. {
  98. FIXED,
  99. SPHERICAL,
  100. REVOLUTE,
  101. PRISMATIC,
  102. DISTANCE,
  103. D6
  104. };
  105. };
  106. //-----------------------------------------------------------------------------
  107. struct PhysicsJoint
  108. {
  109. StringId32 name;
  110. uint32_t type;
  111. StringId32 actor_0;
  112. StringId32 actor_1;
  113. Vector3 anchor_0;
  114. Vector3 anchor_1;
  115. bool breakable;
  116. float break_force;
  117. float break_torque;
  118. // Revolute/Prismatic Joint Limits
  119. float lower_limit;
  120. float upper_limit;
  121. // Spherical Joint Limits
  122. float y_limit_angle;
  123. float z_limit_angle;
  124. // Distance Joint Limits
  125. float max_distance;
  126. // JointLimitPair/cone param
  127. float contact_dist;
  128. float restitution;
  129. float spring;
  130. float damping;
  131. float distance;
  132. };
  133. //-----------------------------------------------------------------------------
  134. struct PhysicsResource
  135. {
  136. //-----------------------------------------------------------------------------
  137. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  138. {
  139. File* file = bundle.open(id);
  140. const size_t file_size = file->size();
  141. void* res = allocator.allocate(file_size);
  142. file->read(res, file_size);
  143. bundle.close(file);
  144. return res;
  145. }
  146. //-----------------------------------------------------------------------------
  147. static void online(void* resource)
  148. {
  149. }
  150. //-----------------------------------------------------------------------------
  151. static void unload(Allocator& allocator, void* resource)
  152. {
  153. CE_ASSERT_NOT_NULL(resource);
  154. allocator.deallocate(resource);
  155. }
  156. //-----------------------------------------------------------------------------
  157. static void offline(void* resource)
  158. {
  159. }
  160. //-----------------------------------------------------------------------------
  161. bool has_controller() const
  162. {
  163. return ((PhysicsHeader*) this)->num_controllers == 1;
  164. }
  165. //-----------------------------------------------------------------------------
  166. PhysicsController controller() const
  167. {
  168. CE_ASSERT(has_controller(), "Controller does not exist");
  169. const PhysicsHeader* ph = (PhysicsHeader*) this;
  170. PhysicsController* controller = (PhysicsController*) (((char*) this) + ph->controller_offset);
  171. return *controller;
  172. }
  173. //-----------------------------------------------------------------------------
  174. uint32_t num_actors() const
  175. {
  176. return ((PhysicsHeader*) this)->num_actors;
  177. }
  178. //-----------------------------------------------------------------------------
  179. PhysicsActor actor(uint32_t i) const
  180. {
  181. CE_ASSERT(i < num_actors(), "Index out of bounds");
  182. const PhysicsHeader* ph = (PhysicsHeader*) this;
  183. PhysicsActor* actor = (PhysicsActor*) (((char*) this) + ph->actors_offset);
  184. return actor[i];
  185. }
  186. //-----------------------------------------------------------------------------
  187. uint32_t num_shapes_indices() const
  188. {
  189. return ((PhysicsHeader*) this)->num_shapes_indices;
  190. }
  191. //-----------------------------------------------------------------------------
  192. uint32_t shape_index(uint32_t i) const
  193. {
  194. CE_ASSERT(i < num_shapes_indices(), "Index out of bounds");
  195. const PhysicsHeader* ph = (PhysicsHeader*) this;
  196. uint32_t* index = (uint32_t*) (((char*) this) + ph->shapes_indices_offset);
  197. return index[i];
  198. }
  199. //-----------------------------------------------------------------------------
  200. uint32_t num_shapes() const
  201. {
  202. return ((PhysicsHeader*) this)->num_shapes;
  203. }
  204. //-----------------------------------------------------------------------------
  205. PhysicsShape shape(uint32_t i) const
  206. {
  207. CE_ASSERT(i < num_shapes(), "Index out of bounds");
  208. const PhysicsHeader* ph = (PhysicsHeader*) this;
  209. PhysicsShape* shape = (PhysicsShape*) (((char*) this) + ph->shapes_offset);
  210. return shape[i];
  211. }
  212. //-----------------------------------------------------------------------------
  213. uint32_t num_joints() const
  214. {
  215. return ((PhysicsHeader*) this)->num_joints;
  216. }
  217. //-----------------------------------------------------------------------------
  218. PhysicsJoint joint(uint32_t i) const
  219. {
  220. CE_ASSERT(i < num_joints(), "Index out of bounds");
  221. const PhysicsHeader* ph = (PhysicsHeader*) this;
  222. PhysicsJoint* joint = (PhysicsJoint*) (((char*) this) + ph->joints_offset);
  223. return joint[i];
  224. }
  225. private:
  226. // Disable construction
  227. PhysicsResource();
  228. };
  229. struct PhysicsConfigHeader
  230. {
  231. uint32_t num_materials;
  232. uint32_t materials_offset;
  233. uint32_t num_shapes;
  234. uint32_t shapes_offset;
  235. uint32_t num_actors;
  236. uint32_t actors_offset;
  237. uint32_t num_filters;
  238. uint32_t filters_offset;
  239. };
  240. struct PhysicsMaterial
  241. {
  242. float static_friction;
  243. float dynamic_friction;
  244. float restitution;
  245. // uint8_t restitution_combine_mode;
  246. // uint8_t friction_combine_mode;
  247. };
  248. struct PhysicsCollisionFilter
  249. {
  250. uint32_t me;
  251. uint32_t mask;
  252. };
  253. struct PhysicsShape2
  254. {
  255. StringId32 collision_filter;
  256. bool trigger;
  257. };
  258. struct PhysicsActor2
  259. {
  260. enum
  261. {
  262. DYNAMIC = (1 << 0),
  263. KINEMATIC = (1 << 1),
  264. DISABLE_GRAVITY = (1 << 2)
  265. };
  266. float linear_damping;
  267. float angular_damping;
  268. uint8_t flags;
  269. };
  270. //-----------------------------------------------------------------------------
  271. struct PhysicsConfigResource
  272. {
  273. //-----------------------------------------------------------------------------
  274. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  275. {
  276. File* file = bundle.open(id);
  277. const size_t file_size = file->size();
  278. void* res = allocator.allocate(file_size);
  279. file->read(res, file_size);
  280. bundle.close(file);
  281. return res;
  282. }
  283. //-----------------------------------------------------------------------------
  284. static void online(void* resource)
  285. {
  286. }
  287. //-----------------------------------------------------------------------------
  288. static void unload(Allocator& allocator, void* resource)
  289. {
  290. CE_ASSERT_NOT_NULL(resource);
  291. allocator.deallocate(resource);
  292. }
  293. //-----------------------------------------------------------------------------
  294. static void offline(void* resource)
  295. {
  296. }
  297. //-----------------------------------------------------------------------------
  298. uint32_t num_materials() const
  299. {
  300. return ((PhysicsConfigHeader*) this)->num_materials;
  301. }
  302. /// Returns the material with the given @a name
  303. PhysicsMaterial material(StringId32 name) const
  304. {
  305. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  306. StringId32* begin = (StringId32*) (((char*) this) + h->materials_offset);
  307. StringId32* end = begin + num_materials();
  308. StringId32* id = std::find(begin, end, name);
  309. CE_ASSERT(id != end, "Material not found");
  310. return material_by_index(id - begin);
  311. }
  312. PhysicsMaterial material_by_index(uint32_t i) const
  313. {
  314. CE_ASSERT(i < num_materials(), "Index out of bounds");
  315. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  316. const PhysicsMaterial* base = (PhysicsMaterial*) (((char*) this) + h->materials_offset + sizeof(StringId32) * num_materials());
  317. return base[i];
  318. }
  319. //-----------------------------------------------------------------------------
  320. uint32_t num_shapes() const
  321. {
  322. return ((PhysicsConfigHeader*) this)->num_shapes;
  323. }
  324. //-----------------------------------------------------------------------------
  325. PhysicsShape2 shape(StringId32 name) const
  326. {
  327. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  328. StringId32* begin = (StringId32*) (((char*) this) + h->shapes_offset);
  329. StringId32* end = begin + num_shapes();
  330. StringId32* id = std::find(begin, end, name);
  331. CE_ASSERT(id != end, "Shape not found");
  332. return shape_by_index(id - begin);
  333. }
  334. //-----------------------------------------------------------------------------
  335. PhysicsShape2 shape_by_index(uint32_t i) const
  336. {
  337. CE_ASSERT(i < num_shapes(), "Index out of bounds");
  338. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  339. const PhysicsShape2* base = (PhysicsShape2*) (((char*) this) + h->shapes_offset + sizeof(StringId32) * num_shapes());
  340. return base[i];
  341. }
  342. //-----------------------------------------------------------------------------
  343. uint32_t num_actors() const
  344. {
  345. return ((PhysicsConfigHeader*) this)->num_actors;
  346. }
  347. /// Returns the actor with the given @a name
  348. PhysicsActor2 actor(StringId32 name) const
  349. {
  350. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  351. StringId32* begin = (StringId32*) (((char*) this) + h->actors_offset);
  352. StringId32* end = begin + num_actors();
  353. StringId32* id = std::find(begin, end, name);
  354. CE_ASSERT(id != end, "Actor not found");
  355. return actor_by_index(id - begin);
  356. }
  357. //-----------------------------------------------------------------------------
  358. PhysicsActor2 actor_by_index(uint32_t i) const
  359. {
  360. CE_ASSERT(i < num_actors(), "Index out of bounds");
  361. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  362. const PhysicsActor2* base = (PhysicsActor2*) (((char*) this) + h->actors_offset + sizeof(StringId32) * num_actors());
  363. return base[i];
  364. }
  365. uint32_t num_filters() const
  366. {
  367. return ((PhysicsConfigHeader*) this)->num_filters;
  368. }
  369. PhysicsCollisionFilter filter(StringId32 name) const
  370. {
  371. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  372. StringId32* begin = (StringId32*) (((char*) this) + h->filters_offset);
  373. StringId32* end = begin + num_filters();
  374. StringId32* id = std::find(begin, end, name);
  375. CE_ASSERT(id != end, "Filter not found");
  376. return filter_by_index(id - begin);
  377. }
  378. PhysicsCollisionFilter filter_by_index(uint32_t i) const
  379. {
  380. CE_ASSERT(i < num_filters(), "Index out of bounds");
  381. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  382. const PhysicsCollisionFilter* base = (PhysicsCollisionFilter*) (((char*) this) + h->filters_offset + sizeof(StringId32) * num_filters());
  383. return base[i];
  384. }
  385. private:
  386. // Disable construction
  387. PhysicsConfigResource();
  388. };
  389. } // namespace crown