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.

PhysicObject.h 7.3 KiB

12 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_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. int SphereLimit = MeshRand.Count();
  96. MeshRand << "[sc#add asph1 2 2 2]";
  97. MeshRand << "[sc#dad asph1 2 2 2]";
  98. MeshRand << "[sc#dda asph1 2 2 2]";
  99. MeshRand << "[sc#daa asph1 2 2 2]";
  100. MeshRand << "[sc#ada asph1 2 2 2]";
  101. MeshRand << "[sc#aad asph1 2 2 2]";
  102. int ConeLimit = MeshRand.Count();
  103. MeshRand << "[sc#add scb#add ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  104. MeshRand << "[sc#dad scb#dad ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  105. MeshRand << "[sc#dda scb#dda ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  106. MeshRand << "[sc#daa scb#daa ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  107. MeshRand << "[sc#ada scb#ada ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  108. MeshRand << "[sc#aad scb#aad ad1 2 0 rx180 ty-1 ac4 2 2 0 0 0]";
  109. int CylLimit = MeshRand.Count();
  110. MeshRand << "[sc#add scb#add ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  111. MeshRand << "[sc#dad scb#dad ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  112. MeshRand << "[sc#dda scb#dda ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  113. MeshRand << "[sc#daa scb#daa ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  114. MeshRand << "[sc#ada scb#ada ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  115. MeshRand << "[sc#aad scb#aad ad1 2 0 rx180 ty-1 my ac4 2 2 2 0 0]";
  116. int CapsLimit = MeshRand.Count();
  117. MeshRand << "[sc#add scb#add acap1 2 1]";
  118. MeshRand << "[sc#dad scb#dad acap1 2 1]";
  119. MeshRand << "[sc#dda scb#dda acap1 2 1]";
  120. MeshRand << "[sc#daa scb#daa acap1 2 1]";
  121. MeshRand << "[sc#ada scb#ada acap1 2 1]";
  122. MeshRand << "[sc#aad scb#aad acap1 2 1]";
  123. switch (RandValue)
  124. {
  125. case 0:
  126. {
  127. RandValue = (int)(lol::RandF() * (SphereLimit - 1));
  128. break;
  129. }
  130. case 1:
  131. {
  132. RandValue = SphereLimit + (int)(lol::RandF() * ((ConeLimit - SphereLimit) - 1));
  133. break;
  134. }
  135. case 2:
  136. {
  137. RandValue = ConeLimit + (int)(lol::RandF() * ((CylLimit - ConeLimit) - 1));
  138. break;
  139. }
  140. case 3:
  141. {
  142. RandValue = CylLimit + (int)(lol::RandF() * ((CapsLimit - CylLimit) - 1));
  143. break;
  144. }
  145. case 4:
  146. {
  147. RandValue = CapsLimit + (int)(lol::RandF() * ((MeshRand.Count() - CapsLimit) - 1));
  148. break;
  149. }
  150. default:
  151. {
  152. RandValue = (int)(lol::RandF() * (MeshRand.Count() - 1));
  153. }
  154. }
  155. m_physics = new EasyPhysic(this);
  156. m_mesh.Compile(MeshRand[RandValue]);
  157. vec3 BoxSize = vec3(2.0f);
  158. int ColGroup = 1;
  159. if (RandValue < SphereLimit)
  160. {
  161. m_physics->SetShapeToBox(BoxSize);
  162. ColGroup += 0;
  163. }
  164. else if (RandValue < ConeLimit)
  165. {
  166. m_physics->SetShapeToSphere(BoxSize.x * 2.f);
  167. ColGroup += 1;
  168. }
  169. else if (RandValue < CylLimit)
  170. {
  171. m_physics->SetShapeToCone(BoxSize.x, BoxSize.y);
  172. ColGroup += 2;
  173. }
  174. else if (RandValue < CapsLimit)
  175. {
  176. m_physics->SetShapeToCylinder(BoxSize);
  177. ColGroup += 3;
  178. }
  179. else
  180. {
  181. m_physics->SetShapeToCapsule(BoxSize.x, BoxSize.y);
  182. ColGroup += 4;
  183. }
  184. m_physics->SetCollisionChannel(0, 0xFF);
  185. //m_physics->SetCollisionChannel(ColGroup, (1<<ColGroup)|(1));
  186. m_physics->SetMass(base_mass);
  187. m_physics->SetTransform(base_location);
  188. m_physics->InitBodyToRigid();
  189. m_physics->AddToSimulation(new_sim);
  190. }
  191. void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)))
  192. {
  193. if (m_is_character)
  194. m_character->SetTransform(base_location, base_rotation);
  195. else
  196. m_physics->SetTransform(base_location, base_rotation);
  197. }
  198. lol::mat4 GetTransform()
  199. {
  200. if (m_is_character)
  201. return m_character->GetTransform();
  202. else
  203. return m_physics->GetTransform();
  204. }
  205. void SetRender(bool should_render)
  206. {
  207. m_should_render = should_render;
  208. }
  209. EasyMesh *GetMesh() { return &m_mesh; }
  210. EasyPhysic *GetPhysic() { return m_physics; }
  211. EasyPhysic *GetCharacter() { return m_character; }
  212. ~PhysicsObject()
  213. {
  214. }
  215. char const *GetName() { return "<PhysicsObject>"; }
  216. protected:
  217. virtual void TickGame(float seconds)
  218. {
  219. WorldEntity::TickGame(seconds);
  220. }
  221. virtual void TickDraw(float seconds)
  222. {
  223. WorldEntity::TickDraw(seconds);
  224. if (!m_ready)
  225. {
  226. m_mesh.MeshConvert();
  227. m_ready = true;
  228. }
  229. if (m_should_render)
  230. {
  231. if (m_is_character)
  232. m_mesh.Render(m_character->GetTransform());
  233. else
  234. m_mesh.Render(m_physics->GetTransform());
  235. }
  236. }
  237. private:
  238. //Base datas
  239. EasyMesh m_mesh;
  240. EasyPhysic* m_physics;
  241. EasyCharacterController* m_character;
  242. bool m_ready;
  243. bool m_should_render;
  244. bool m_is_character;
  245. };
  246. #endif /* __PHYSICOBJECT_H__ */