PhysicsResource.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "Types.h"
  25. #include "Allocator.h"
  26. #include "File.h"
  27. #include "Bundle.h"
  28. #include "ResourceManager.h"
  29. #include "Vector3.h"
  30. namespace crown
  31. {
  32. //-----------------------------------------------------------------------------
  33. struct PhysicsHeader
  34. {
  35. uint32_t version;
  36. uint32_t num_controllers; // 0 or 1, ATM
  37. uint32_t controller_offset;
  38. uint32_t num_actors;
  39. uint32_t actors_offset;
  40. uint32_t num_shapes_indices;
  41. uint32_t shapes_indices_offset;
  42. uint32_t num_shapes;
  43. uint32_t shapes_offset;
  44. uint32_t num_joints;
  45. uint32_t joints_offset;
  46. };
  47. //-----------------------------------------------------------------------------
  48. struct PhysicsController
  49. {
  50. StringId32 name;
  51. float height; // Height of the capsule
  52. float radius; // Radius of the capsule
  53. float slope_limit; // The maximum slope which the character can walk up in radians.
  54. float step_offset; // Maximum height of an obstacle which the character can climb.
  55. float contact_offset; // Skin around the object within which contacts will be generated. Use it to avoid numerical precision issues.
  56. };
  57. //-----------------------------------------------------------------------------
  58. struct PhysicsActorType
  59. {
  60. enum Enum
  61. {
  62. STATIC,
  63. DYNAMIC_PHYSICAL,
  64. DYNAMIC_KINEMATIC
  65. };
  66. };
  67. //-----------------------------------------------------------------------------
  68. struct PhysicsActorGroup
  69. {
  70. enum Enum
  71. {
  72. GROUP_0 = (1<<0),
  73. GROUP_1 = (1<<1),
  74. GROUP_2 = (1<<2),
  75. GROUP_3 = (1<<3),
  76. GROUP_4 = (1<<4),
  77. GROUP_5 = (1<<5),
  78. GROUP_6 = (1<<6),
  79. GROUP_7 = (1<<7),
  80. GROUP_8 = (1<<8),
  81. GROUP_9 = (1<<9),
  82. GROUP_10 = (1<<10),
  83. GROUP_11 = (1<<11),
  84. GROUP_12 = (1<<12),
  85. GROUP_13 = (1<<13),
  86. GROUP_14 = (1<<14),
  87. GROUP_15 = (1<<15),
  88. GROUP_16 = (1<<16),
  89. GROUP_17 = (1<<17),
  90. GROUP_18 = (1<<18),
  91. GROUP_19 = (1<<19),
  92. GROUP_20 = (1<<20),
  93. GROUP_21 = (1<<21),
  94. GROUP_22 = (1<<22),
  95. GROUP_23 = (1<<23),
  96. GROUP_24 = (1<<24),
  97. GROUP_25 = (1<<25),
  98. GROUP_26 = (1<<26),
  99. GROUP_27 = (1<<27),
  100. GROUP_28 = (1<<28),
  101. GROUP_29 = (1<<29),
  102. GROUP_30 = (1<<30),
  103. GROUP_31 = (1<<31)
  104. };
  105. };
  106. //-----------------------------------------------------------------------------
  107. struct PhysicsActor
  108. {
  109. StringId32 name;
  110. StringId32 node;
  111. uint32_t type;
  112. uint32_t group;
  113. uint32_t mask;
  114. float static_friction;
  115. float dynamic_friction;
  116. float restitution;
  117. uint32_t num_shapes;
  118. };
  119. //-----------------------------------------------------------------------------
  120. struct PhysicsShapeType
  121. {
  122. enum Enum
  123. {
  124. SPHERE,
  125. CAPSULE,
  126. BOX,
  127. PLANE
  128. };
  129. };
  130. //-----------------------------------------------------------------------------
  131. struct PhysicsShape
  132. {
  133. StringId32 name;
  134. uint32_t type;
  135. bool trigger;
  136. float data_0;
  137. float data_1;
  138. float data_2;
  139. float data_3;
  140. };
  141. //-----------------------------------------------------------------------------
  142. struct PhysicsJointType
  143. {
  144. enum Enum
  145. {
  146. FIXED,
  147. SPHERICAL,
  148. REVOLUTE,
  149. PRISMATIC,
  150. DISTANCE,
  151. D6
  152. };
  153. };
  154. //-----------------------------------------------------------------------------
  155. struct PhysicsJoint
  156. {
  157. StringId32 name;
  158. uint32_t type;
  159. StringId32 actor_0;
  160. StringId32 actor_1;
  161. Vector3 anchor_0;
  162. Vector3 anchor_1;
  163. bool breakable;
  164. float break_force;
  165. float break_torque;
  166. // Revolute/Prismatic Joint Limits
  167. float lower_limit;
  168. float upper_limit;
  169. // Spherical Joint Limits
  170. float y_limit_angle;
  171. float z_limit_angle;
  172. // Distance Joint Limits
  173. float max_distance;
  174. // JointLimitPair/cone param
  175. float contact_dist;
  176. float restitution;
  177. float spring;
  178. float damping;
  179. float distance;
  180. };
  181. //-----------------------------------------------------------------------------
  182. struct PhysicsResource
  183. {
  184. //-----------------------------------------------------------------------------
  185. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  186. {
  187. File* file = bundle.open(id);
  188. const size_t file_size = file->size();
  189. void* res = allocator.allocate(file_size);
  190. file->read(res, file_size);
  191. bundle.close(file);
  192. return res;
  193. }
  194. //-----------------------------------------------------------------------------
  195. static void online(void* resource)
  196. {
  197. }
  198. //-----------------------------------------------------------------------------
  199. static void unload(Allocator& allocator, void* resource)
  200. {
  201. CE_ASSERT_NOT_NULL(resource);
  202. allocator.deallocate(resource);
  203. }
  204. //-----------------------------------------------------------------------------
  205. static void offline(void* resource)
  206. {
  207. }
  208. //-----------------------------------------------------------------------------
  209. bool has_controller() const
  210. {
  211. return ((PhysicsHeader*) this)->num_controllers == 1;
  212. }
  213. //-----------------------------------------------------------------------------
  214. PhysicsController controller() const
  215. {
  216. CE_ASSERT(has_controller(), "Controller does not exist");
  217. const PhysicsHeader* ph = (PhysicsHeader*) this;
  218. PhysicsController* controller = (PhysicsController*) (((char*) this) + ph->controller_offset);
  219. return *controller;
  220. }
  221. //-----------------------------------------------------------------------------
  222. uint32_t num_actors() const
  223. {
  224. return ((PhysicsHeader*) this)->num_actors;
  225. }
  226. //-----------------------------------------------------------------------------
  227. PhysicsActor actor(uint32_t i) const
  228. {
  229. CE_ASSERT(i < num_actors(), "Index out of bounds");
  230. const PhysicsHeader* ph = (PhysicsHeader*) this;
  231. PhysicsActor* actor = (PhysicsActor*) (((char*) this) + ph->actors_offset);
  232. return actor[i];
  233. }
  234. //-----------------------------------------------------------------------------
  235. uint32_t num_shapes_indices() const
  236. {
  237. return ((PhysicsHeader*) this)->num_shapes_indices;
  238. }
  239. //-----------------------------------------------------------------------------
  240. uint32_t shape_index(uint32_t i) const
  241. {
  242. CE_ASSERT(i < num_shapes_indices(), "Index out of bounds");
  243. const PhysicsHeader* ph = (PhysicsHeader*) this;
  244. uint32_t* index = (uint32_t*) (((char*) this) + ph->shapes_indices_offset);
  245. return index[i];
  246. }
  247. //-----------------------------------------------------------------------------
  248. uint32_t num_shapes() const
  249. {
  250. return ((PhysicsHeader*) this)->num_shapes;
  251. }
  252. //-----------------------------------------------------------------------------
  253. PhysicsShape shape(uint32_t i) const
  254. {
  255. CE_ASSERT(i < num_shapes(), "Index out of bounds");
  256. const PhysicsHeader* ph = (PhysicsHeader*) this;
  257. PhysicsShape* shape = (PhysicsShape*) (((char*) this) + ph->shapes_offset);
  258. return shape[i];
  259. }
  260. //-----------------------------------------------------------------------------
  261. uint32_t num_joints() const
  262. {
  263. return ((PhysicsHeader*) this)->num_joints;
  264. }
  265. //-----------------------------------------------------------------------------
  266. PhysicsJoint joint(uint32_t i) const
  267. {
  268. CE_ASSERT(i < num_joints(), "Index out of bounds");
  269. const PhysicsHeader* ph = (PhysicsHeader*) this;
  270. PhysicsJoint* joint = (PhysicsJoint*) (((char*) this) + ph->joints_offset);
  271. return joint[i];
  272. }
  273. private:
  274. // Disable construction
  275. PhysicsResource();
  276. };
  277. } // namespace crown