您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

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