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.
 
 
 

255 rivejä
6.6 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. //m_mesh.Compile("[sc#f00 afcb10 10 10 -.1]");
  38. vec3 BoxSize = vec3(20.f, 1.f, 20.f);
  39. m_physics.SetCollisionChannel(0, 0xFF);
  40. m_physics.SetShapeToBox(BoxSize);
  41. m_physics.SetMass(.0f);
  42. m_physics.SetTransform(base_location, base_rotation);
  43. m_physics.InitBodyToRigid(true);
  44. m_physics.AddToSimulation(new_sim);
  45. }
  46. else if (dummy == 2) //for character purpose
  47. {
  48. m_is_character = true;
  49. m_mesh.Compile("[sc#f00 afcb10 10 10 -.1]");
  50. //m_mesh.Compile("[sc#fff scb#fff ac1 2 2 2 0 0]");
  51. vec3 BoxSize = vec3(2.f, 2.f, 2.f);
  52. m_character.SetCollisionChannel(0, 0xFF);
  53. m_character.SetShapeToCapsule(BoxSize.x, BoxSize.y);
  54. m_character.SetMass(.0f);
  55. m_character.SetTransform(base_location, base_rotation);
  56. m_character.InitBodyToGhost();
  57. m_character.AddToSimulation(new_sim);
  58. }
  59. }
  60. PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1)
  61. : m_ready(false), m_should_render(true), m_is_character(false)
  62. {
  63. Array<char const *> MeshRand;
  64. MeshRand << "[sc#add afcb2 2 2 -.1]";
  65. MeshRand << "[sc#dad afcb2 2 2 -.1]";
  66. MeshRand << "[sc#dda afcb2 2 2 -.1]";
  67. MeshRand << "[sc#daa afcb2 2 2 -.1]";
  68. MeshRand << "[sc#ada afcb2 2 2 -.1]";
  69. MeshRand << "[sc#aad afcb2 2 2 -.1]";
  70. int SphereLimit = MeshRand.Count();
  71. MeshRand << "[sc#add asph1 2 2 2]";
  72. MeshRand << "[sc#dad asph1 2 2 2]";
  73. MeshRand << "[sc#dda asph1 2 2 2]";
  74. MeshRand << "[sc#daa asph1 2 2 2]";
  75. MeshRand << "[sc#ada asph1 2 2 2]";
  76. MeshRand << "[sc#aad asph1 2 2 2]";
  77. int ConeLimit = MeshRand.Count();
  78. MeshRand << "[sc#add scb#add ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]";
  79. MeshRand << "[sc#dad scb#dad ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]";
  80. MeshRand << "[sc#dda scb#dda ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]";
  81. MeshRand << "[sc#daa scb#daa ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]";
  82. MeshRand << "[sc#ada scb#ada ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]";
  83. MeshRand << "[sc#aad scb#aad ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]";
  84. int CylLimit = MeshRand.Count();
  85. MeshRand << "[sc#add scb#add ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]";
  86. MeshRand << "[sc#dad scb#dad ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]";
  87. MeshRand << "[sc#dda scb#dda ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]";
  88. MeshRand << "[sc#daa scb#daa ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]";
  89. MeshRand << "[sc#ada scb#ada ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]";
  90. MeshRand << "[sc#aad scb#aad ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]";
  91. int CapsLimit = MeshRand.Count();
  92. MeshRand << "[sc#add scb#add acap1 2 1]";
  93. MeshRand << "[sc#dad scb#dad acap1 2 1]";
  94. MeshRand << "[sc#dda scb#dda acap1 2 1]";
  95. MeshRand << "[sc#daa scb#daa acap1 2 1]";
  96. MeshRand << "[sc#ada scb#ada acap1 2 1]";
  97. MeshRand << "[sc#aad scb#aad acap1 2 1]";
  98. switch (RandValue)
  99. {
  100. case 0:
  101. {
  102. RandValue = (int)(lol::RandF() * (SphereLimit - 1));
  103. break;
  104. }
  105. case 1:
  106. {
  107. RandValue = SphereLimit + (int)(lol::RandF() * ((ConeLimit - SphereLimit) - 1));
  108. break;
  109. }
  110. case 2:
  111. {
  112. RandValue = ConeLimit + (int)(lol::RandF() * ((CylLimit - ConeLimit) - 1));
  113. break;
  114. }
  115. case 3:
  116. {
  117. RandValue = CylLimit + (int)(lol::RandF() * ((CapsLimit - CylLimit) - 1));
  118. break;
  119. }
  120. case 4:
  121. {
  122. RandValue = CapsLimit + (int)(lol::RandF() * ((MeshRand.Count() - CapsLimit) - 1));
  123. break;
  124. }
  125. default:
  126. {
  127. RandValue = (int)(lol::RandF() * (MeshRand.Count() - 1));
  128. }
  129. }
  130. m_mesh.Compile(MeshRand[RandValue]);
  131. vec3 BoxSize = vec3(2.0f);
  132. int ColGroup = 1;
  133. if (RandValue < SphereLimit)
  134. {
  135. m_physics.SetShapeToBox(BoxSize);
  136. ColGroup += 0;
  137. }
  138. else if (RandValue < ConeLimit)
  139. {
  140. m_physics.SetShapeToSphere(BoxSize.x * 2.f);
  141. ColGroup += 1;
  142. }
  143. else if (RandValue < CylLimit)
  144. {
  145. m_physics.SetShapeToCone(BoxSize.x, BoxSize.y);
  146. ColGroup += 2;
  147. }
  148. else if (RandValue < CapsLimit)
  149. {
  150. m_physics.SetShapeToCylinder(BoxSize);
  151. ColGroup += 3;
  152. }
  153. else
  154. {
  155. m_physics.SetShapeToCapsule(BoxSize.x, BoxSize.y);
  156. ColGroup += 4;
  157. }
  158. m_physics.SetCollisionChannel(0, 0xFF);
  159. //m_physics.SetCollisionChannel(ColGroup, (1<<ColGroup)|(1));
  160. m_physics.SetMass(base_mass);
  161. m_physics.SetTransform(base_location);
  162. m_physics.InitBodyToRigid();
  163. m_physics.AddToSimulation(new_sim);
  164. }
  165. void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)))
  166. {
  167. if (m_is_character)
  168. m_character.SetTransform(base_location, base_rotation);
  169. else
  170. m_physics.SetTransform(base_location, base_rotation);
  171. }
  172. lol::mat4 GetTransform()
  173. {
  174. if (m_is_character)
  175. return m_character.GetTransform();
  176. else
  177. return m_physics.GetTransform();
  178. }
  179. void SetRender(bool should_render)
  180. {
  181. m_should_render = should_render;
  182. }
  183. EasyMesh *GetMesh() { return &m_mesh; }
  184. EasyPhysic *GetPhysic() { return &m_physics; }
  185. EasyPhysic *GetCharacter() { return &m_character; }
  186. ~PhysicsObject()
  187. {
  188. }
  189. char const *GetName() { return "<PhysicsObject>"; }
  190. protected:
  191. virtual void TickGame(float seconds)
  192. {
  193. WorldEntity::TickGame(seconds);
  194. }
  195. virtual void TickDraw(float seconds)
  196. {
  197. WorldEntity::TickDraw(seconds);
  198. if (!m_ready)
  199. {
  200. m_mesh.MeshConvert();
  201. m_ready = true;
  202. }
  203. if (m_should_render)
  204. {
  205. if (m_is_character)
  206. m_mesh.Render(m_character.GetTransform());
  207. else
  208. m_mesh.Render(m_physics.GetTransform());
  209. }
  210. }
  211. private:
  212. //Base datas
  213. EasyMesh m_mesh;
  214. EasyPhysic m_physics;
  215. EasyCharacterController m_character;
  216. bool m_ready;
  217. bool m_should_render;
  218. bool m_is_character;
  219. };
  220. #endif /* __PHYSICOBJECT_H__ */