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

PhysicObject.h 10 KiB

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