Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

358 lignes
12 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. #if CAT_MODE
  19. #define USE_SPHERE 1
  20. #else
  21. #define USE_BOX 1
  22. #define USE_SPHERE 1
  23. #define USE_CONE 1
  24. #define USE_CYLINDER 1
  25. #define USE_CAPSULE 1
  26. #endif
  27. class PhysicsObject : public WorldEntity
  28. {
  29. public:
  30. PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation)
  31. : m_ready(false), m_should_render(true), m_is_character(false), m_custom_shader(0)
  32. {
  33. #if CAT_MODE
  34. m_is_phys = false;
  35. #endif //CAT_MODE
  36. m_physics = new EasyPhysic(this);
  37. m_mesh.Compile("[sc#ddd afcb 60 1 60 -.1]");
  38. vec3 BoxSize = vec3(60.f, 1.f, 60.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. PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation, int dummy)
  47. : m_ready(false), m_should_render(true), m_is_character(false), m_custom_shader(0)
  48. {
  49. #if CAT_MODE
  50. m_is_phys = false;
  51. #endif //CAT_MODE
  52. if (dummy == 1) //for platform purpose
  53. {
  54. m_physics = new EasyPhysic(this);
  55. m_mesh.Compile("[sc#ddd afcb 20 1 20 -.1]");
  56. vec3 BoxSize = vec3(20.f, 1.f, 20.f);
  57. m_physics->SetCollisionChannel(0, 0xFF);
  58. m_physics->SetShapeToBox(BoxSize);
  59. m_physics->SetMass(.0f);
  60. m_physics->SetTransform(base_location, base_rotation);
  61. m_physics->InitBodyToRigid(true);
  62. m_physics->AddToSimulation(new_sim);
  63. }
  64. else if (dummy == 2) //for character purpose
  65. {
  66. m_character = new EasyCharacterController(this);
  67. m_is_character = true;
  68. //m_mesh.Compile("[sc#f00 afcb10 10 10 -.1]");
  69. m_mesh.Compile(
  70. "[sc#000 scb#000"
  71. //"[sc#aaa scb#aaa"
  72. "[ad8 2 0 rx180 ty-1]"
  73. "[asph8 .5 ty1]"
  74. "[ac32 2 .5 .5 0 0]"
  75. "[asph6 .1 ty.9 tx.5 tz.15]"
  76. "[asph6 .1 ty.9 tx.5 tz-.15]"
  77. "[asph8 .05 sy10 ty.6 tz.5]"
  78. "[asph8 .05 sy10 ty.6 tz-.5]"
  79. "]"
  80. "[sc#fd0 scb#fd0"
  81. "[ac8 .4 .1 0 0 0 ty.25 rz-90 ty.7 tx.5]"
  82. "]"
  83. "["
  84. "[sc#fff scb#fff"
  85. "[ad8 2 0 rx180 ty-1]"
  86. "[asph8 .5 ty1]"
  87. "[ac32 1.9 .5 .5 0 0]"
  88. "]"
  89. " ty-.1 tx.05]"
  90. );
  91. vec3 BoxSize = vec3(1.f, 2.f, 1.f);
  92. m_character->SetCollisionChannel(0, 0xFF);
  93. m_character->SetShapeToCapsule(BoxSize.x, BoxSize.y);
  94. m_character->SetMass(.0f);
  95. //m_character->SetStepHeight(1.f);
  96. m_character->SetTransform(base_location, base_rotation);
  97. m_character->InitBodyToGhost();
  98. m_character->AddToSimulation(new_sim);
  99. }
  100. else if (dummy == 3) //for Stairs purpose
  101. {
  102. m_physics = new EasyPhysic(this);
  103. m_mesh.Compile("[sc#aae afcb4 .25 4 -.01]");
  104. vec3 BoxSize = vec3(4.f, .25f, 4.f);
  105. m_physics->SetCollisionChannel(0, 0xFF);
  106. m_physics->SetShapeToBox(BoxSize);
  107. m_physics->SetMass(.0f);
  108. m_physics->SetTransform(base_location, base_rotation);
  109. m_physics->InitBodyToRigid(true);
  110. m_physics->AddToSimulation(new_sim);
  111. }
  112. }
  113. PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1)
  114. : m_ready(false), m_should_render(true), m_is_character(false), m_custom_shader(0)
  115. {
  116. Array<char const *> MeshRand;
  117. Array<int> MeshLimit;
  118. Array<int> MeshType;
  119. #if CAT_MODE
  120. m_is_phys = true;
  121. #endif //CAT_MODE
  122. MeshLimit << 0;
  123. #if USE_BOX
  124. 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]";
  125. 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]";
  126. 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]";
  127. 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]";
  128. 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]";
  129. 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]";
  130. MeshLimit << MeshRand.Count();
  131. MeshType << 0;
  132. #endif //USE_BOX
  133. #if USE_SPHERE
  134. #if CAT_MODE
  135. int nb_sprite = 4;
  136. //SPRITE
  137. vec2 start_point = vec2((float)rand(nb_sprite), (float)rand(nb_sprite)) / vec2((float)nb_sprite);
  138. vec2 size = vec2(1.f) / vec2((float)nb_sprite);
  139. m_mesh.BD()->SetTexCoordCustomBuild(MeshType::Quad, MeshFaceType::QuadDefault,
  140. start_point, start_point + size);
  141. m_mesh.BD()->SetTexCoordCustomBuild2(MeshType::Quad, MeshFaceType::QuadDefault,
  142. vec2(-4.f), vec2(4.f));
  143. MeshRand << "[sc#ffff aq 0 0]";
  144. MeshRand << "[sc#ffff aq 0 0]";
  145. MeshRand << "[sc#ffff aq 0 0]";
  146. MeshRand << "[sc#fbbf aq 0 0]";
  147. MeshRand << "[sc#bbff aq 0 0]";
  148. MeshRand << "[sc#bfbf aq 0 0]";
  149. #else
  150. MeshRand << "[sc#add asph1 2]";
  151. MeshRand << "[sc#dad asph1 2]";
  152. MeshRand << "[sc#dda asph1 2]";
  153. MeshRand << "[sc#daa asph1 2]";
  154. MeshRand << "[sc#ada asph1 2]";
  155. MeshRand << "[sc#aad asph1 2]";
  156. #endif
  157. MeshLimit << MeshRand.Count();
  158. MeshType << 1;
  159. #endif //USE_SPHERE
  160. #if USE_CONE
  161. MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  162. MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  163. MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  164. MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  165. MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  166. MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  167. MeshLimit << MeshRand.Count();
  168. MeshType << 2;
  169. #endif //USE_CONE
  170. #if USE_CYLINDER
  171. MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  172. MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  173. MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  174. MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  175. MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  176. MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  177. MeshLimit << MeshRand.Count();
  178. MeshType << 3;
  179. #endif //USE_CYLINDER
  180. #if USE_CAPSULE
  181. MeshRand << "[sc#add scb#add acap1 2 1]";
  182. MeshRand << "[sc#dad scb#dad acap1 2 1]";
  183. MeshRand << "[sc#dda scb#dda acap1 2 1]";
  184. MeshRand << "[sc#daa scb#daa acap1 2 1]";
  185. MeshRand << "[sc#ada scb#ada acap1 2 1]";
  186. MeshRand << "[sc#aad scb#aad acap1 2 1]";
  187. MeshLimit << MeshRand.Count();
  188. MeshType << 4;
  189. #endif //USE_CAPSULE
  190. int RandLimit = RandValue;
  191. if (MeshLimit.Count() <= RandValue || RandValue < 0)
  192. RandLimit = rand(MeshLimit.Count() - 1);
  193. RandValue = rand(MeshLimit[RandLimit], MeshLimit[RandLimit + 1]);
  194. m_physics = new EasyPhysic(this);
  195. m_mesh.Compile(MeshRand[RandValue]);
  196. m_mesh.Scale(vec3(OBJ_SIZE));
  197. vec3 BoxSize = vec3(2.0f) * OBJ_SIZE;
  198. int ColGroup = 1;
  199. switch (MeshType[RandLimit])
  200. {
  201. case 0:
  202. {
  203. m_physics->SetShapeToBox(BoxSize);
  204. ColGroup += 0;
  205. break;
  206. }
  207. case 1:
  208. {
  209. m_physics->SetShapeToSphere(BoxSize.x);
  210. ColGroup += 1;
  211. break;
  212. }
  213. case 2:
  214. {
  215. m_physics->SetShapeToCone(BoxSize.x, BoxSize.y);
  216. ColGroup += 2;
  217. break;
  218. }
  219. case 3:
  220. {
  221. m_physics->SetShapeToCylinder(BoxSize);
  222. ColGroup += 3;
  223. break;
  224. }
  225. case 4:
  226. {
  227. m_physics->SetShapeToCapsule(BoxSize.x, BoxSize.y);
  228. ColGroup += 4;
  229. break;
  230. }
  231. default:
  232. {
  233. }
  234. }
  235. m_physics->SetHitRestitution(1.0f);
  236. m_physics->SetCollisionChannel(0, 0xFF);
  237. //m_physics->SetCollisionChannel(ColGroup, (1 << ColGroup)|(1));
  238. m_physics->SetMass(base_mass);
  239. m_physics->SetTransform(base_location);
  240. m_physics->InitBodyToRigid();
  241. m_physics->AddToSimulation(new_sim);
  242. }
  243. void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)))
  244. {
  245. if (m_is_character)
  246. m_character->SetTransform(base_location, base_rotation);
  247. else
  248. m_physics->SetTransform(base_location, base_rotation);
  249. }
  250. lol::mat4 GetTransform()
  251. {
  252. if (m_is_character)
  253. return m_character->GetTransform();
  254. else
  255. return m_physics->GetTransform();
  256. }
  257. void SetRender(bool should_render)
  258. {
  259. m_should_render = should_render;
  260. }
  261. void SetCustomShaderData(GpuShaderData* custom_shader)
  262. {
  263. m_custom_shader = custom_shader;
  264. }
  265. EasyMesh *GetMesh() { return &m_mesh; }
  266. EasyPhysic *GetPhysic() { return m_physics; }
  267. EasyCharacterController *GetCharacter() { return m_character; }
  268. ~PhysicsObject()
  269. {
  270. }
  271. char const *GetName() { return "<PhysicsObject>"; }
  272. protected:
  273. virtual void TickGame(float seconds)
  274. {
  275. WorldEntity::TickGame(seconds);
  276. }
  277. virtual void TickDraw(float seconds)
  278. {
  279. WorldEntity::TickDraw(seconds);
  280. #if CAT_MODE
  281. if (!m_is_phys || m_custom_shader)
  282. #endif //CAT_MODE
  283. {
  284. if (!m_ready)
  285. {
  286. if (m_custom_shader)
  287. m_mesh.MeshConvert(m_custom_shader);
  288. else
  289. m_mesh.MeshConvert();
  290. m_ready = true;
  291. }
  292. else if (m_should_render)
  293. {
  294. if (m_is_character)
  295. m_mesh.Render(m_character->GetTransform());
  296. else
  297. m_mesh.Render(m_physics->GetTransform());
  298. }
  299. }
  300. }
  301. private:
  302. //Base datas
  303. EasyMesh m_mesh;
  304. EasyPhysic* m_physics;
  305. EasyCharacterController* m_character;
  306. GpuShaderData* m_custom_shader;
  307. bool m_ready;
  308. bool m_should_render;
  309. bool m_is_character;
  310. #if CAT_MODE
  311. bool m_is_phys;
  312. #endif //CAT_MODE
  313. };
  314. #endif /* __PHYSICOBJECT_H__ */