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.

297 lines
9.6 KiB

  1. //
  2. // Orbital
  3. //
  4. // Copyright: (c) 2009-2013 Cédric Lecacheur <jordx@free.fr>
  5. // (c) 2009-2013 Benjamin "Touky" 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. #include "physics/easycharactercontroller.h"
  15. #include "physics/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 afcb 60 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 afcb 20 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 ty1]"
  59. "[ac32 2 .5 .5 0 0]"
  60. "[asph6 .1 ty.9 tx.5 tz.15]"
  61. "[asph6 .1 ty.9 tx.5 tz-.15]"
  62. "[asph8 .05 sy10 ty.6 tz.5]"
  63. "[asph8 .05 sy10 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 ty1]"
  72. "[ac32 1.9 .5 .5 0 0]"
  73. "]"
  74. " ty-.1 tx.05]"
  75. );
  76. vec3 BoxSize = vec3(1.f, 2.f, 1.f);
  77. m_character->SetCollisionChannel(0, 0xFF);
  78. m_character->SetShapeToCapsule(BoxSize.x, BoxSize.y);
  79. m_character->SetMass(.0f);
  80. //m_character->SetStepHeight(1.f);
  81. m_character->SetTransform(base_location, base_rotation);
  82. m_character->InitBodyToGhost();
  83. m_character->AddToSimulation(new_sim);
  84. }
  85. else if (dummy == 3) //for Stairs purpose
  86. {
  87. m_physics = new EasyPhysic(this);
  88. m_mesh.Compile("[sc#aae afcb4 .25 4 -.01]");
  89. vec3 BoxSize = vec3(4.f, .25f, 4.f);
  90. m_physics->SetCollisionChannel(0, 0xFF);
  91. m_physics->SetShapeToBox(BoxSize);
  92. m_physics->SetMass(.0f);
  93. m_physics->SetTransform(base_location, base_rotation);
  94. m_physics->InitBodyToRigid(true);
  95. m_physics->AddToSimulation(new_sim);
  96. }
  97. }
  98. PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1)
  99. : m_ready(false), m_should_render(true), m_is_character(false)
  100. {
  101. Array<char const *> MeshRand;
  102. MeshRand << "[sc#add afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
  103. MeshRand << "[sc#dad afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
  104. MeshRand << "[sc#dda afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
  105. MeshRand << "[sc#daa afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
  106. MeshRand << "[sc#ada afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
  107. MeshRand << "[sc#aad afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
  108. int SphereLimit = MeshRand.Count();
  109. MeshRand << "[sc#add asph1 2]";
  110. MeshRand << "[sc#dad asph1 2]";
  111. MeshRand << "[sc#dda asph1 2]";
  112. MeshRand << "[sc#daa asph1 2]";
  113. MeshRand << "[sc#ada asph1 2]";
  114. MeshRand << "[sc#aad asph1 2]";
  115. int ConeLimit = MeshRand.Count();
  116. MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  117. MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  118. MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  119. MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  120. MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  121. MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  122. int CylLimit = MeshRand.Count();
  123. MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  124. MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  125. MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  126. MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  127. MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  128. MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  129. int CapsLimit = MeshRand.Count();
  130. MeshRand << "[sc#add scb#add acap1 2 1]";
  131. MeshRand << "[sc#dad scb#dad acap1 2 1]";
  132. MeshRand << "[sc#dda scb#dda acap1 2 1]";
  133. MeshRand << "[sc#daa scb#daa acap1 2 1]";
  134. MeshRand << "[sc#ada scb#ada acap1 2 1]";
  135. MeshRand << "[sc#aad scb#aad acap1 2 1]";
  136. switch (RandValue)
  137. {
  138. case 0:
  139. {
  140. RandValue = rand(SphereLimit);
  141. break;
  142. }
  143. case 1:
  144. {
  145. RandValue = rand(SphereLimit, ConeLimit);
  146. break;
  147. }
  148. case 2:
  149. {
  150. RandValue = rand(ConeLimit, CylLimit);
  151. break;
  152. }
  153. case 3:
  154. {
  155. RandValue = rand(CylLimit, CapsLimit);
  156. break;
  157. }
  158. case 4:
  159. {
  160. RandValue = rand(CapsLimit, MeshRand.Count());
  161. break;
  162. }
  163. default:
  164. {
  165. RandValue = rand(MeshRand.Count());
  166. }
  167. }
  168. m_physics = new EasyPhysic(this);
  169. m_mesh.Compile(MeshRand[RandValue]);
  170. vec3 BoxSize = vec3(2.0f);
  171. int ColGroup = 1;
  172. if (RandValue < SphereLimit)
  173. {
  174. m_physics->SetShapeToBox(BoxSize);
  175. ColGroup += 0;
  176. }
  177. else if (RandValue < ConeLimit)
  178. {
  179. m_physics->SetShapeToSphere(BoxSize.x * 2.f);
  180. ColGroup += 1;
  181. }
  182. else if (RandValue < CylLimit)
  183. {
  184. m_physics->SetShapeToCone(BoxSize.x, BoxSize.y);
  185. ColGroup += 2;
  186. }
  187. else if (RandValue < CapsLimit)
  188. {
  189. m_physics->SetShapeToCylinder(BoxSize);
  190. ColGroup += 3;
  191. }
  192. else
  193. {
  194. m_physics->SetShapeToCapsule(BoxSize.x, BoxSize.y);
  195. ColGroup += 4;
  196. }
  197. m_physics->SetCollisionChannel(0, 0xFF);
  198. //m_physics->SetCollisionChannel(ColGroup, (1 << ColGroup)|(1));
  199. m_physics->SetMass(base_mass);
  200. m_physics->SetTransform(base_location);
  201. m_physics->InitBodyToRigid();
  202. m_physics->AddToSimulation(new_sim);
  203. }
  204. void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)))
  205. {
  206. if (m_is_character)
  207. m_character->SetTransform(base_location, base_rotation);
  208. else
  209. m_physics->SetTransform(base_location, base_rotation);
  210. }
  211. lol::mat4 GetTransform()
  212. {
  213. if (m_is_character)
  214. return m_character->GetTransform();
  215. else
  216. return m_physics->GetTransform();
  217. }
  218. void SetRender(bool should_render)
  219. {
  220. m_should_render = should_render;
  221. }
  222. EasyMesh *GetMesh() { return &m_mesh; }
  223. EasyPhysic *GetPhysic() { return m_physics; }
  224. EasyCharacterController *GetCharacter() { return m_character; }
  225. ~PhysicsObject()
  226. {
  227. }
  228. char const *GetName() { return "<PhysicsObject>"; }
  229. protected:
  230. virtual void TickGame(float seconds)
  231. {
  232. WorldEntity::TickGame(seconds);
  233. }
  234. virtual void TickDraw(float seconds)
  235. {
  236. WorldEntity::TickDraw(seconds);
  237. if (!m_ready)
  238. {
  239. m_mesh.MeshConvert();
  240. m_ready = true;
  241. }
  242. if (m_should_render)
  243. {
  244. if (m_is_character)
  245. m_mesh.Render(m_character->GetTransform());
  246. else
  247. m_mesh.Render(m_physics->GetTransform());
  248. }
  249. }
  250. private:
  251. //Base datas
  252. EasyMesh m_mesh;
  253. EasyPhysic* m_physics;
  254. EasyCharacterController* m_character;
  255. bool m_ready;
  256. bool m_should_render;
  257. bool m_is_character;
  258. };
  259. #endif /* __PHYSICOBJECT_H__ */