25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

274 lines
7.0 KiB

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