You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // Orbital
  3. //
  4. // Copyright: (c) 2009-2012 Cdric Lecacheur <jordx@free.fr>
  5. // (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
  6. // (c) 2012 Sam Hocevar <sam@hocevar.net>
  7. //
  8. /* FIXME: this file is pure crap; it's only a test. */
  9. #if !defined __PHYSICOBJECT_H__
  10. #define __PHYSICOBJECT_H__
  11. #include "core.h"
  12. #include "easymesh/easymesh.h"
  13. #include "Physics/Include/EasyPhysics.h"
  14. #include "Physics/Include/EasyCharacterController.h"
  15. #include "Physics/Include/EasyConstraint.h"
  16. using namespace lol;
  17. using namespace lol::phys;
  18. class PhysicsObject : public WorldEntity
  19. {
  20. public:
  21. PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation)
  22. : m_ready(false), m_should_render(true), m_is_character(false)
  23. {
  24. m_mesh.Compile("[sc#ddd afcb60 1 60 -.1]");
  25. vec3 BoxSize = vec3(60.f, 1.f, 60.f);
  26. m_physics.SetCollisionChannel(0, 0xFF);
  27. m_physics.SetShapeToBox(BoxSize);
  28. m_physics.SetMass(.0f);
  29. m_physics.SetTransform(base_location, base_rotation);
  30. m_physics.InitBodyToRigid(true);
  31. m_physics.AddToSimulation(new_sim);
  32. }
  33. PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation, int dummy)
  34. : m_ready(false), m_should_render(true), m_is_character(false)
  35. {
  36. if (dummy == 1) //for Rope purpose
  37. {
  38. m_mesh.Compile("[sc#ddd afcb20 1 20 -.1]");
  39. vec3 BoxSize = vec3(20.f, 1.f, 20.f);
  40. m_physics.SetCollisionChannel(0, 0xFF);
  41. m_physics.SetShapeToBox(BoxSize);
  42. m_physics.SetMass(.0f);
  43. m_physics.SetTransform(base_location, base_rotation);
  44. m_physics.InitBodyToRigid(true);
  45. m_physics.AddToSimulation(new_sim);
  46. }
  47. else if (dummy == 2) //for character purpose
  48. {
  49. m_is_character = true;
  50. //m_mesh.Compile("[sc#f00 afcb10 10 10 -.1]");
  51. m_mesh.Compile(
  52. "[sc#000 scb#000"
  53. //"[sc#aaa scb#aaa"
  54. "[ad8 2 0 rx180 ty-1]"
  55. "[asph8 .5 .5 .5 ty1]"
  56. "[ac32 2 .5 .5 0 0]"
  57. "[asph6 .1 .1 .1 ty.9 tx.5 tz.15]"
  58. "[asph6 .1 .1 .1 ty.9 tx.5 tz-.15]"
  59. "[asph8 .05 .5 .05 ty.6 tz.5]"
  60. "[asph8 .05 .5 .05 ty.6 tz-.5]"
  61. "]"
  62. "[sc#fd0 scb#fd0"
  63. "[ac8 .4 .1 0 0 0 ty.25 rz-90 ty.7 tx.5]"
  64. "]"
  65. "["
  66. "[sc#fff scb#fff"
  67. "[ad8 2 0 rx180 ty-1]"
  68. "[asph8 .5 .5 .5 ty1]"
  69. "[ac32 1.9 .5 .5 0 0]"
  70. "]"
  71. " ty-.1 tx.05]"
  72. );
  73. vec3 BoxSize = vec3(.5f, 2.f, .5f);
  74. m_character.SetCollisionChannel(0, 0xFF);
  75. m_character.SetShapeToCapsule(BoxSize.x, BoxSize.y);
  76. m_character.SetMass(.0f);
  77. m_character.SetTransform(base_location, base_rotation);
  78. m_character.InitBodyToGhost();
  79. m_character.AddToSimulation(new_sim);
  80. }
  81. }
  82. PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1)
  83. : m_ready(false), m_should_render(true), m_is_character(false)
  84. {
  85. Array<char const *> MeshRand;
  86. MeshRand << "[sc#add afcb2 2 2 -.1]";
  87. MeshRand << "[sc#dad afcb2 2 2 -.1]";
  88. MeshRand << "[sc#dda afcb2 2 2 -.1]";
  89. MeshRand << "[sc#daa afcb2 2 2 -.1]";
  90. MeshRand << "[sc#ada afcb2 2 2 -.1]";
  91. MeshRand << "[sc#aad afcb2 2 2 -.1]";
  92. int SphereLimit = MeshRand.Count();
  93. MeshRand << "[sc#add asph1 2 2 2]";
  94. MeshRand << "[sc#dad asph1 2 2 2]";
  95. MeshRand << "[sc#dda asph1 2 2 2]";
  96. MeshRand << "[sc#daa asph1 2 2 2]";
  97. MeshRand << "[sc#ada asph1 2 2 2]";
  98. MeshRand << "[sc#aad asph1 2 2 2]";
  99. int ConeLimit = MeshRand.Count();
  100. MeshRand << "[sc#add scb#add ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  101. MeshRand << "[sc#dad scb#dad ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  102. MeshRand << "[sc#dda scb#dda ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  103. MeshRand << "[sc#daa scb#daa ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  104. MeshRand << "[sc#ada scb#ada ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  105. MeshRand << "[sc#aad scb#aad ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  106. int CylLimit = MeshRand.Count();
  107. MeshRand << "[sc#add scb#add ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  108. MeshRand << "[sc#dad scb#dad ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  109. MeshRand << "[sc#dda scb#dda ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  110. MeshRand << "[sc#daa scb#daa ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  111. MeshRand << "[sc#ada scb#ada ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  112. MeshRand << "[sc#aad scb#aad ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  113. int CapsLimit = MeshRand.Count();
  114. MeshRand << "[sc#add scb#add acap1 2 1]";
  115. MeshRand << "[sc#dad scb#dad acap1 2 1]";
  116. MeshRand << "[sc#dda scb#dda acap1 2 1]";
  117. MeshRand << "[sc#daa scb#daa acap1 2 1]";
  118. MeshRand << "[sc#ada scb#ada acap1 2 1]";
  119. MeshRand << "[sc#aad scb#aad acap1 2 1]";
  120. switch (RandValue)
  121. {
  122. case 0:
  123. {
  124. RandValue = (int)(lol::RandF() * (SphereLimit - 1));
  125. break;
  126. }
  127. case 1:
  128. {
  129. RandValue = SphereLimit + (int)(lol::RandF() * ((ConeLimit - SphereLimit) - 1));
  130. break;
  131. }
  132. case 2:
  133. {
  134. RandValue = ConeLimit + (int)(lol::RandF() * ((CylLimit - ConeLimit) - 1));
  135. break;
  136. }
  137. case 3:
  138. {
  139. RandValue = CylLimit + (int)(lol::RandF() * ((CapsLimit - CylLimit) - 1));
  140. break;
  141. }
  142. case 4:
  143. {
  144. RandValue = CapsLimit + (int)(lol::RandF() * ((MeshRand.Count() - CapsLimit) - 1));
  145. break;
  146. }
  147. default:
  148. {
  149. RandValue = (int)(lol::RandF() * (MeshRand.Count() - 1));
  150. }
  151. }
  152. m_mesh.Compile(MeshRand[RandValue]);
  153. vec3 BoxSize = vec3(2.0f);
  154. int ColGroup = 1;
  155. if (RandValue < SphereLimit)
  156. {
  157. m_physics.SetShapeToBox(BoxSize);
  158. ColGroup += 0;
  159. }
  160. else if (RandValue < ConeLimit)
  161. {
  162. m_physics.SetShapeToSphere(BoxSize.x * 2.f);
  163. ColGroup += 1;
  164. }
  165. else if (RandValue < CylLimit)
  166. {
  167. m_physics.SetShapeToCone(BoxSize.x, BoxSize.y);
  168. ColGroup += 2;
  169. }
  170. else if (RandValue < CapsLimit)
  171. {
  172. m_physics.SetShapeToCylinder(BoxSize);
  173. ColGroup += 3;
  174. }
  175. else
  176. {
  177. m_physics.SetShapeToCapsule(BoxSize.x, BoxSize.y);
  178. ColGroup += 4;
  179. }
  180. m_physics.SetCollisionChannel(0, 0xFF);
  181. //m_physics.SetCollisionChannel(ColGroup, (1<<ColGroup)|(1));
  182. m_physics.SetMass(base_mass);
  183. m_physics.SetTransform(base_location);
  184. m_physics.InitBodyToRigid();
  185. m_physics.AddToSimulation(new_sim);
  186. }
  187. void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)))
  188. {
  189. if (m_is_character)
  190. m_character.SetTransform(base_location, base_rotation);
  191. else
  192. m_physics.SetTransform(base_location, base_rotation);
  193. }
  194. lol::mat4 GetTransform()
  195. {
  196. if (m_is_character)
  197. return m_character.GetTransform();
  198. else
  199. return m_physics.GetTransform();
  200. }
  201. void SetRender(bool should_render)
  202. {
  203. m_should_render = should_render;
  204. }
  205. EasyMesh *GetMesh() { return &m_mesh; }
  206. EasyPhysic *GetPhysic() { return &m_physics; }
  207. EasyPhysic *GetCharacter() { return &m_character; }
  208. ~PhysicsObject()
  209. {
  210. }
  211. char const *GetName() { return "<PhysicsObject>"; }
  212. protected:
  213. virtual void TickGame(float seconds)
  214. {
  215. WorldEntity::TickGame(seconds);
  216. }
  217. virtual void TickDraw(float seconds)
  218. {
  219. WorldEntity::TickDraw(seconds);
  220. if (!m_ready)
  221. {
  222. m_mesh.MeshConvert();
  223. m_ready = true;
  224. }
  225. if (m_should_render)
  226. {
  227. if (m_is_character)
  228. m_mesh.Render(m_character.GetTransform());
  229. else
  230. m_mesh.Render(m_physics.GetTransform());
  231. }
  232. }
  233. private:
  234. //Base datas
  235. EasyMesh m_mesh;
  236. EasyPhysic m_physics;
  237. EasyCharacterController m_character;
  238. bool m_ready;
  239. bool m_should_render;
  240. bool m_is_character;
  241. };
  242. #endif /* __PHYSICOBJECT_H__ */