PhysicsResource.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. namespace crown
  30. {
  31. struct PhysicsHeader
  32. {
  33. uint32_t version;
  34. uint32_t num_controllers; // 0 or 1, ATM
  35. uint32_t controller_offset;
  36. uint32_t num_actors;
  37. uint32_t actors_offset;
  38. };
  39. struct PhysicsController
  40. {
  41. StringId32 name;
  42. float height; // Height of the capsule
  43. float radius; // Radius of the capsule
  44. float slope_limit; // The maximum slope which the character can walk up in radians.
  45. float step_offset; // Maximum height of an obstacle which the character can climb.
  46. float contact_offset; // Skin around the object within which contacts will be generated. Use it to avoid numerical precision issues.
  47. };
  48. struct PhysicsActor
  49. {
  50. StringId32 name;
  51. StringId32 node;
  52. uint32_t num_shapes;
  53. };
  54. struct PhysicsShapeType
  55. {
  56. enum Enum
  57. {
  58. SPHERE,
  59. CAPSULE,
  60. BOX,
  61. PLANE
  62. };
  63. };
  64. struct PhysicsShape
  65. {
  66. StringId32 name;
  67. uint32_t type;
  68. };
  69. //-----------------------------------------------------------------------------
  70. struct PhysicsResource
  71. {
  72. //-----------------------------------------------------------------------------
  73. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  74. {
  75. File* file = bundle.open(id);
  76. const size_t file_size = file->size();
  77. void* res = allocator.allocate(file_size);
  78. file->read(res, file_size);
  79. bundle.close(file);
  80. return res;
  81. }
  82. //-----------------------------------------------------------------------------
  83. static void online(void* resource)
  84. {
  85. }
  86. //-----------------------------------------------------------------------------
  87. static void unload(Allocator& allocator, void* resource)
  88. {
  89. CE_ASSERT_NOT_NULL(resource);
  90. allocator.deallocate(resource);
  91. }
  92. //-----------------------------------------------------------------------------
  93. static void offline(void* resource)
  94. {
  95. }
  96. //-----------------------------------------------------------------------------
  97. bool has_controller() const
  98. {
  99. return ((PhysicsHeader*) this)->num_controllers == 1;
  100. }
  101. //-----------------------------------------------------------------------------
  102. PhysicsController controller() const
  103. {
  104. CE_ASSERT(has_controller(), "Controller does not exist");
  105. const PhysicsHeader* ph = (PhysicsHeader*) this;
  106. PhysicsController* controller = (PhysicsController*) (((char*) this) + ph->controller_offset);
  107. return *controller;
  108. }
  109. //-----------------------------------------------------------------------------
  110. uint32_t num_actors() const
  111. {
  112. return ((PhysicsHeader*) this)->num_actors;
  113. }
  114. //-----------------------------------------------------------------------------
  115. PhysicsActor actor(uint32_t i) const
  116. {
  117. CE_ASSERT(i < num_actors(), "Index out of bounds");
  118. const PhysicsHeader* ph = (PhysicsHeader*) this;
  119. PhysicsActor* actor = (PhysicsActor*) (((char*) this) + ph->actors_offset);
  120. return actor[i];
  121. }
  122. private:
  123. // Disable construction
  124. PhysicsResource();
  125. };
  126. } // namespace crown