PhysicsResource.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. uint32_t num_shapes; // Number of shapes
  66. };
  67. //-----------------------------------------------------------------------------
  68. struct PhysicsShapeType
  69. {
  70. enum Enum
  71. {
  72. SPHERE,
  73. CAPSULE,
  74. BOX,
  75. PLANE
  76. };
  77. };
  78. //-----------------------------------------------------------------------------
  79. struct PhysicsShape
  80. {
  81. StringId32 name; // Name of the shape
  82. StringId32 shape_class; // Shape class from global.physics_config
  83. StringId32 type; // Type of the shape
  84. StringId32 material; // Material from global.physics_config
  85. float data_0;
  86. float data_1;
  87. float data_2;
  88. float data_3;
  89. };
  90. //-----------------------------------------------------------------------------
  91. struct PhysicsJointType
  92. {
  93. enum Enum
  94. {
  95. FIXED,
  96. SPHERICAL,
  97. REVOLUTE,
  98. PRISMATIC,
  99. DISTANCE,
  100. D6
  101. };
  102. };
  103. //-----------------------------------------------------------------------------
  104. struct PhysicsJoint
  105. {
  106. StringId32 name;
  107. uint32_t type;
  108. StringId32 actor_0;
  109. StringId32 actor_1;
  110. Vector3 anchor_0;
  111. Vector3 anchor_1;
  112. bool breakable;
  113. float break_force;
  114. float break_torque;
  115. // Revolute/Prismatic Joint Limits
  116. float lower_limit;
  117. float upper_limit;
  118. // Spherical Joint Limits
  119. float y_limit_angle;
  120. float z_limit_angle;
  121. // Distance Joint Limits
  122. float max_distance;
  123. // JointLimitPair/cone param
  124. float contact_dist;
  125. float restitution;
  126. float spring;
  127. float damping;
  128. float distance;
  129. };
  130. //-----------------------------------------------------------------------------
  131. struct PhysicsResource
  132. {
  133. //-----------------------------------------------------------------------------
  134. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  135. {
  136. File* file = bundle.open(id);
  137. const size_t file_size = file->size();
  138. void* res = allocator.allocate(file_size);
  139. file->read(res, file_size);
  140. bundle.close(file);
  141. return res;
  142. }
  143. //-----------------------------------------------------------------------------
  144. static void online(void* resource)
  145. {
  146. }
  147. //-----------------------------------------------------------------------------
  148. static void unload(Allocator& allocator, void* resource)
  149. {
  150. CE_ASSERT_NOT_NULL(resource);
  151. allocator.deallocate(resource);
  152. }
  153. //-----------------------------------------------------------------------------
  154. static void offline(void* resource)
  155. {
  156. }
  157. //-----------------------------------------------------------------------------
  158. bool has_controller() const
  159. {
  160. return ((PhysicsHeader*) this)->num_controllers == 1;
  161. }
  162. //-----------------------------------------------------------------------------
  163. PhysicsController controller() const
  164. {
  165. CE_ASSERT(has_controller(), "Controller does not exist");
  166. const PhysicsHeader* ph = (PhysicsHeader*) this;
  167. PhysicsController* controller = (PhysicsController*) (((char*) this) + ph->controller_offset);
  168. return *controller;
  169. }
  170. //-----------------------------------------------------------------------------
  171. uint32_t num_actors() const
  172. {
  173. return ((PhysicsHeader*) this)->num_actors;
  174. }
  175. //-----------------------------------------------------------------------------
  176. PhysicsActor actor(uint32_t i) const
  177. {
  178. CE_ASSERT(i < num_actors(), "Index out of bounds");
  179. const PhysicsHeader* ph = (PhysicsHeader*) this;
  180. PhysicsActor* actor = (PhysicsActor*) (((char*) this) + ph->actors_offset);
  181. return actor[i];
  182. }
  183. //-----------------------------------------------------------------------------
  184. uint32_t num_shapes_indices() const
  185. {
  186. return ((PhysicsHeader*) this)->num_shapes_indices;
  187. }
  188. //-----------------------------------------------------------------------------
  189. uint32_t shape_index(uint32_t i) const
  190. {
  191. CE_ASSERT(i < num_shapes_indices(), "Index out of bounds");
  192. const PhysicsHeader* ph = (PhysicsHeader*) this;
  193. uint32_t* index = (uint32_t*) (((char*) this) + ph->shapes_indices_offset);
  194. return index[i];
  195. }
  196. //-----------------------------------------------------------------------------
  197. uint32_t num_shapes() const
  198. {
  199. return ((PhysicsHeader*) this)->num_shapes;
  200. }
  201. //-----------------------------------------------------------------------------
  202. PhysicsShape shape(uint32_t i) const
  203. {
  204. CE_ASSERT(i < num_shapes(), "Index out of bounds");
  205. const PhysicsHeader* ph = (PhysicsHeader*) this;
  206. PhysicsShape* shape = (PhysicsShape*) (((char*) this) + ph->shapes_offset);
  207. return shape[i];
  208. }
  209. //-----------------------------------------------------------------------------
  210. uint32_t num_joints() const
  211. {
  212. return ((PhysicsHeader*) this)->num_joints;
  213. }
  214. //-----------------------------------------------------------------------------
  215. PhysicsJoint joint(uint32_t i) const
  216. {
  217. CE_ASSERT(i < num_joints(), "Index out of bounds");
  218. const PhysicsHeader* ph = (PhysicsHeader*) this;
  219. PhysicsJoint* joint = (PhysicsJoint*) (((char*) this) + ph->joints_offset);
  220. return joint[i];
  221. }
  222. private:
  223. // Disable construction
  224. PhysicsResource();
  225. };
  226. struct PhysicsConfigHeader
  227. {
  228. uint32_t num_materials;
  229. uint32_t materials_offset;
  230. uint32_t num_shapes;
  231. uint32_t shapes_offset;
  232. uint32_t num_actors;
  233. uint32_t actors_offset;
  234. uint32_t num_filters;
  235. uint32_t filters_offset;
  236. };
  237. struct PhysicsMaterial
  238. {
  239. float static_friction;
  240. float dynamic_friction;
  241. float restitution;
  242. // uint8_t restitution_combine_mode;
  243. // uint8_t friction_combine_mode;
  244. };
  245. struct PhysicsCollisionFilter
  246. {
  247. uint32_t me;
  248. uint32_t mask;
  249. };
  250. struct PhysicsShape2
  251. {
  252. uint32_t collision_filter;
  253. bool trigger;
  254. };
  255. struct PhysicsActor2
  256. {
  257. enum
  258. {
  259. DYNAMIC = (1 << 0),
  260. KINEMATIC = (1 << 1),
  261. DISABLE_GRAVITY = (1 << 2)
  262. };
  263. uint32_t collision_filter;
  264. float linear_damping;
  265. float angular_damping;
  266. uint8_t flags;
  267. };
  268. //-----------------------------------------------------------------------------
  269. struct PhysicsConfigResource
  270. {
  271. //-----------------------------------------------------------------------------
  272. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  273. {
  274. File* file = bundle.open(id);
  275. const size_t file_size = file->size();
  276. void* res = allocator.allocate(file_size);
  277. file->read(res, file_size);
  278. bundle.close(file);
  279. return res;
  280. }
  281. //-----------------------------------------------------------------------------
  282. static void online(void* resource)
  283. {
  284. }
  285. //-----------------------------------------------------------------------------
  286. static void unload(Allocator& allocator, void* resource)
  287. {
  288. CE_ASSERT_NOT_NULL(resource);
  289. allocator.deallocate(resource);
  290. }
  291. //-----------------------------------------------------------------------------
  292. static void offline(void* resource)
  293. {
  294. }
  295. //-----------------------------------------------------------------------------
  296. uint32_t num_materials() const
  297. {
  298. return ((PhysicsConfigHeader*) this)->num_materials;
  299. }
  300. /// Returns the material with the given @a name
  301. PhysicsMaterial material(StringId32 name) const
  302. {
  303. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  304. StringId32* begin = (StringId32*) (((char*) this) + h->materials_offset);
  305. StringId32* end = begin + num_materials();
  306. StringId32* id = std::find(begin, end, name);
  307. CE_ASSERT(id != end, "Material not found");
  308. return material_by_index(id - begin);
  309. }
  310. PhysicsMaterial material_by_index(uint32_t i) const
  311. {
  312. CE_ASSERT(i < num_materials(), "Index out of bounds");
  313. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  314. const PhysicsMaterial* base = (PhysicsMaterial*) (((char*) this) + h->materials_offset + sizeof(StringId32) * num_materials());
  315. return base[i];
  316. }
  317. //-----------------------------------------------------------------------------
  318. uint32_t num_shapes() const
  319. {
  320. return ((PhysicsConfigHeader*) this)->num_shapes;
  321. }
  322. //-----------------------------------------------------------------------------
  323. PhysicsShape2 shape(StringId32 name) const
  324. {
  325. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  326. StringId32* begin = (StringId32*) (((char*) this) + h->shapes_offset);
  327. StringId32* end = begin + num_shapes();
  328. StringId32* id = std::find(begin, end, name);
  329. CE_ASSERT(id != end, "Shape not found");
  330. return shape_by_index(id - begin);
  331. }
  332. //-----------------------------------------------------------------------------
  333. PhysicsShape2 shape_by_index(uint32_t i) const
  334. {
  335. CE_ASSERT(i < num_shapes(), "Index out of bounds");
  336. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  337. const PhysicsShape2* base = (PhysicsShape2*) (((char*) this) + h->shapes_offset + sizeof(StringId32) * num_shapes());
  338. return base[i];
  339. }
  340. //-----------------------------------------------------------------------------
  341. uint32_t num_actors() const
  342. {
  343. return ((PhysicsConfigHeader*) this)->num_actors;
  344. }
  345. /// Returns the actor with the given @a name
  346. PhysicsActor2 actor(StringId32 name) const
  347. {
  348. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  349. StringId32* begin = (StringId32*) (((char*) this) + h->actors_offset);
  350. StringId32* end = begin + num_actors();
  351. StringId32* id = std::find(begin, end, name);
  352. CE_ASSERT(id != end, "Actor not found");
  353. return actor_by_index(id - begin);
  354. }
  355. //-----------------------------------------------------------------------------
  356. PhysicsActor2 actor_by_index(uint32_t i) const
  357. {
  358. CE_ASSERT(i < num_actors(), "Index out of bounds");
  359. const PhysicsConfigHeader* h = (PhysicsConfigHeader*) this;
  360. const PhysicsActor2* base = (PhysicsActor2*) (((char*) this) + h->actors_offset + sizeof(StringId32) * num_actors());
  361. return base[i];
  362. }
  363. private:
  364. // Disable construction
  365. PhysicsConfigResource();
  366. };
  367. } // namespace crown