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.

473 lignes
16 KiB

  1. //
  2. // Lol Engine — Bullet physics test
  3. //
  4. // Copyright © 2012—2020 Sam Hocevar <sam@hocevar.net>
  5. // © 2009—2013 Cédric Lecacheur <jordx@free.fr>
  6. // © 2009—2013 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  7. //
  8. // Lol Engine is free software. It comes without any warranty, to
  9. // the extent permitted by applicable law. You can redistribute it
  10. // and/or modify it under the terms of the Do What the Fuck You Want
  11. // to Public License, Version 2, as published by the WTFPL Task Force.
  12. // See http://www.wtfpl.net/ for more details.
  13. //
  14. #pragma once
  15. /* FIXME: this file is pure crap; it's only a test. */
  16. #include <lol/engine.h>
  17. #include "easymesh/easymesh.h"
  18. #include "physics/easyphysics.h"
  19. #include "physics/easycharactercontroller.h"
  20. #include "physics/easyconstraint.h"
  21. using namespace lol;
  22. using namespace lol::phys;
  23. #if CAT_MODE
  24. #define USE_SPHERE 1
  25. #else
  26. #define USE_BOX 1
  27. #define USE_SPHERE 1
  28. #define USE_CONE 1
  29. #define USE_CYLINDER 1
  30. #define USE_CAPSULE 1
  31. #endif
  32. static vec4 hex_to_color(std::string const &s)
  33. {
  34. std::string c = s[0] == '#' ? &s[1] : s;
  35. uint32_t tmp = std::strtol(c.c_str(), nullptr, 16);
  36. if (c.length() == 3)
  37. {
  38. tmp = 0x11000000u * (tmp >> 8)
  39. | 0x00110000u * ((tmp >> 4) & 0xf)
  40. | 0x00001100u * (tmp & 0xf)
  41. | 0x000000ffu;
  42. }
  43. else if (c.length() == 4)
  44. {
  45. tmp = 0x11000000u * (tmp >> 12)
  46. | 0x00110000u * ((tmp >> 8) & 0xf)
  47. | 0x00001100u * ((tmp >> 4) & 0xf)
  48. | 0x00000011u * (tmp & 0xf);
  49. }
  50. else if (c.length() == 6)
  51. {
  52. tmp = 0xffu | 0x100u * tmp;
  53. }
  54. ivec4 v(tmp >> 24, (tmp >> 16) & 0xff, (tmp >> 8) & 0xff, tmp & 0xff);
  55. return vec4(v) * (1.f / 255.f);
  56. }
  57. class PhysicsObject : public WorldEntity
  58. {
  59. public:
  60. PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation)
  61. : m_custom_shader(0),
  62. m_ready(false),
  63. m_should_render(true),
  64. m_is_character(false),
  65. m_is_phys(false)
  66. {
  67. m_physics = new EasyPhysic(this);
  68. //m_mesh.Compile("[sc#ddd afcb 60 1 60 -.1]");
  69. m_mesh.SetCurColor(color::white);
  70. m_mesh.AppendFlatChamfBox(vec3(60.f, 1.f, 60.f), -.1f);
  71. vec3 BoxSize = vec3(60.f, 1.f, 60.f);
  72. m_physics->SetCollisionChannel(0, 0xFF);
  73. m_physics->SetShapeToBox(BoxSize);
  74. m_physics->SetMass(.0f);
  75. m_physics->SetTransform(base_location, base_rotation);
  76. m_physics->InitBodyToRigid(true);
  77. m_physics->AddToSimulation(new_sim);
  78. }
  79. PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation, int dummy)
  80. : m_custom_shader(0),
  81. m_ready(false),
  82. m_should_render(true),
  83. m_is_character(false),
  84. m_is_phys(false)
  85. {
  86. if (dummy == 1) //for platform purpose
  87. {
  88. m_physics = new EasyPhysic(this);
  89. m_mesh.Compile("[sc#ddd afcb 20 1 20 -.1]");
  90. vec3 BoxSize = vec3(20.f, 1.f, 20.f);
  91. m_physics->SetCollisionChannel(0, 0xFF);
  92. m_physics->SetShapeToBox(BoxSize);
  93. m_physics->SetMass(.0f);
  94. m_physics->SetTransform(base_location, base_rotation);
  95. m_physics->InitBodyToRigid(true);
  96. m_physics->AddToSimulation(new_sim);
  97. }
  98. else if (dummy == 2) //for character purpose
  99. {
  100. m_character = new EasyCharacterController(this);
  101. m_is_character = true;
  102. //m_mesh.Compile("[sc#f00 afcb10 10 10 -.1]");
  103. m_mesh.Compile(
  104. "[sc#000 scb#000"
  105. //"[sc#aaa scb#aaa"
  106. "[ad8 2 0 rx180 ty-1]"
  107. "[asph8 .5 ty1]"
  108. "[ac32 2 .5 .5 0 0]"
  109. "[asph6 .1 ty.9 tx.5 tz.15]"
  110. "[asph6 .1 ty.9 tx.5 tz-.15]"
  111. "[asph8 .05 sy10 ty.6 tz.5]"
  112. "[asph8 .05 sy10 ty.6 tz-.5]"
  113. "]"
  114. "[sc#fd0 scb#fd0"
  115. "[ac8 .4 .1 0 0 0 ty.25 rz-90 ty.7 tx.5]"
  116. "]"
  117. "["
  118. "[sc#fff scb#fff"
  119. "[ad8 2 0 rx180 ty-1]"
  120. "[asph8 .5 ty1]"
  121. "[ac32 1.9 .5 .5 0 0]"
  122. "]"
  123. " ty-.1 tx.05]"
  124. );
  125. vec3 BoxSize = vec3(1.f, 2.f, 1.f);
  126. m_character->SetCollisionChannel(0, 0xFF);
  127. m_character->SetShapeToCapsule(BoxSize.x, BoxSize.y);
  128. m_character->SetMass(.0f);
  129. //m_character->SetStepHeight(1.f);
  130. m_character->SetTransform(base_location, base_rotation);
  131. m_character->InitBodyToGhost();
  132. m_character->AddToSimulation(new_sim);
  133. }
  134. else if (dummy == 3) //for Stairs purpose
  135. {
  136. m_physics = new EasyPhysic(this);
  137. m_mesh.Compile("[sc#aae afcb4 .25 4 -.01]");
  138. vec3 BoxSize = vec3(4.f, .25f, 4.f);
  139. m_physics->SetCollisionChannel(0, 0xFF);
  140. m_physics->SetShapeToBox(BoxSize);
  141. m_physics->SetMass(.0f);
  142. m_physics->SetTransform(base_location, base_rotation);
  143. m_physics->InitBodyToRigid(true);
  144. m_physics->AddToSimulation(new_sim);
  145. }
  146. }
  147. PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1)
  148. : m_custom_shader(0),
  149. m_ready(false),
  150. m_should_render(true),
  151. m_is_character(false),
  152. m_is_phys(false)
  153. {
  154. static array<EasyMesh> MeshRand;
  155. static array<int> MeshLimit;
  156. static array<int> MeshType;
  157. if (!MeshRand.count())
  158. {
  159. array<std::string> colors_base = { "#add", "#dad", "#dda", "#daa", "#ada", "#aad" };
  160. MeshLimit << 0;
  161. #if USE_BOX
  162. {
  163. array<std::string> colors = colors_base;
  164. while (colors.count())
  165. {
  166. EasyMesh m;
  167. vec4 color = hex_to_color(colors.pop());
  168. m.SetCurColor(color);
  169. m.AppendFlatChamfBox(vec3(1.7f), .4f);
  170. m.OpenBrace();
  171. {
  172. m.SetCurColor(color::black);
  173. m.ToggleScaleWinding();
  174. m.AppendFlatChamfBox(vec3(1.9f), .4f);
  175. m.ScaleX(-1.f);
  176. m.ScaleY(-1.f);
  177. m.ScaleZ(-1.f);
  178. }
  179. m.CloseBrace();
  180. MeshRand << m;
  181. }
  182. }
  183. //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]";
  184. //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]";
  185. //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]";
  186. //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]";
  187. //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]";
  188. //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]";
  189. MeshLimit << MeshRand.count();
  190. MeshType << 0;
  191. #endif //USE_BOX
  192. #if USE_SPHERE
  193. #if CAT_MODE
  194. int nb_sprite = NB_SPRITE;
  195. //SPRITE
  196. vec2 start_point = vec2((float)rand(nb_sprite), (float)rand(nb_sprite)) / vec2((float)nb_sprite);
  197. //vec2(0.f, .0f) / vec2((float)nb_sprite);
  198. vec2 size = vec2(1.f) / vec2((float)nb_sprite);
  199. m_mesh.BD()->SetTexCoordCustomBuild(MeshType::Quad, MeshFaceType::QuadDefault,
  200. start_point, start_point + size);
  201. m_mesh.BD()->SetTexCoordCustomBuild2(MeshType::Quad, MeshFaceType::QuadDefault,
  202. vec2(-PARTICLE_SIZE), vec2(PARTICLE_SIZE));
  203. MeshRand << "[tpbn tvnc sc#ffff aq 0 0]";
  204. MeshRand << "[tpbn tvnc sc#faaf aq 0 0]";
  205. MeshRand << "[tpbn tvnc sc#afaf aq 0 0]";
  206. MeshRand << "[tpbn tvnc sc#aaff aq 0 0]";
  207. #else
  208. {
  209. array<std::string> colors = colors_base;
  210. while (colors.count())
  211. {
  212. EasyMesh m;
  213. vec4 color = hex_to_color(colors.pop());
  214. m.SetCurColor(color);
  215. m.AppendSphere(1, 2.f);
  216. MeshRand << m;
  217. }
  218. }
  219. //MeshRand << "[sc#add asph1 2]";
  220. //MeshRand << "[sc#dad asph1 2]";
  221. //MeshRand << "[sc#dda asph1 2]";
  222. //MeshRand << "[sc#daa asph1 2]";
  223. //MeshRand << "[sc#ada asph1 2]";
  224. //MeshRand << "[sc#aad asph1 2]";
  225. #endif
  226. MeshLimit << MeshRand.count();
  227. MeshType << 1;
  228. #endif //USE_SPHERE
  229. #if USE_CONE
  230. {
  231. array<std::string> colors = colors_base;
  232. while (colors.count())
  233. {
  234. EasyMesh m;
  235. vec4 color = hex_to_color(colors.pop());
  236. m.SetCurColor(color);
  237. m.SetCurColorB(color);
  238. m.AppendDisc(8, 2.f);
  239. m.RotateX(180.f);
  240. m.TranslateY(-1.f);
  241. m.AppendCylinder(8, 2.f, 2.f, 0.f);
  242. MeshRand << m;
  243. }
  244. }
  245. //MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  246. //MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  247. //MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  248. //MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  249. //MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  250. //MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  251. MeshLimit << MeshRand.count();
  252. MeshType << 2;
  253. #endif //USE_CONE
  254. #if USE_CYLINDER
  255. {
  256. array<std::string> colors = colors_base;
  257. while (colors.count())
  258. {
  259. EasyMesh m;
  260. vec4 color = hex_to_color(colors.pop());
  261. m.SetCurColor(color);
  262. m.SetCurColorB(color);
  263. m.AppendDisc(8, 2.f);
  264. m.RotateX(180.f);
  265. m.TranslateY(-1.f);
  266. m.MirrorY();
  267. m.AppendCylinder(8.f, 2.f, 2.f, 2.f);
  268. MeshRand << m;
  269. }
  270. }
  271. //MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  272. //MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  273. //MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  274. //MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  275. //MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  276. //MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  277. MeshLimit << MeshRand.count();
  278. MeshType << 3;
  279. #endif //USE_CYLINDER
  280. #if USE_CAPSULE
  281. {
  282. array<std::string> colors = colors_base;
  283. while (colors.count())
  284. {
  285. EasyMesh m;
  286. vec4 color = hex_to_color(colors.pop());
  287. m.SetCurColor(color);
  288. m.SetCurColorB(color);
  289. m.AppendCapsule(1, 2.f, 1.f);
  290. MeshRand << m;
  291. }
  292. }
  293. //MeshRand << "[sc#add scb#add acap1 2 1]";
  294. //MeshRand << "[sc#dad scb#dad acap1 2 1]";
  295. //MeshRand << "[sc#dda scb#dda acap1 2 1]";
  296. //MeshRand << "[sc#daa scb#daa acap1 2 1]";
  297. //MeshRand << "[sc#ada scb#ada acap1 2 1]";
  298. //MeshRand << "[sc#aad scb#aad acap1 2 1]";
  299. MeshLimit << MeshRand.count();
  300. MeshType << 4;
  301. #endif //USE_CAPSULE
  302. }
  303. int RandLimit = RandValue;
  304. if (MeshLimit.count() <= RandValue || RandValue < 0)
  305. RandLimit = rand((int)MeshLimit.count() - 1);
  306. RandValue = rand(MeshLimit[RandLimit], MeshLimit[RandLimit + 1]);
  307. m_physics = new EasyPhysic(this);
  308. //m_mesh.Compile(MeshRand[RandValue]);
  309. m_mesh = MeshRand[RandValue];
  310. m_mesh.Scale(vec3(OBJ_SIZE));
  311. vec3 BoxSize = vec3(2.0f) * OBJ_SIZE;
  312. int ColGroup = 1;
  313. switch (MeshType[RandLimit])
  314. {
  315. case 0:
  316. {
  317. m_physics->SetShapeToBox(BoxSize);
  318. ColGroup += 0;
  319. break;
  320. }
  321. case 1:
  322. {
  323. m_physics->SetShapeToSphere(BoxSize.x);
  324. ColGroup += 1;
  325. break;
  326. }
  327. case 2:
  328. {
  329. m_physics->SetShapeToCone(BoxSize.x, BoxSize.y);
  330. ColGroup += 2;
  331. break;
  332. }
  333. case 3:
  334. {
  335. m_physics->SetShapeToCylinder(BoxSize);
  336. ColGroup += 3;
  337. break;
  338. }
  339. case 4:
  340. {
  341. m_physics->SetShapeToCapsule(BoxSize.x, BoxSize.y);
  342. ColGroup += 4;
  343. break;
  344. }
  345. default:
  346. {
  347. }
  348. }
  349. m_physics->SetHitRestitution(1.0f);
  350. m_physics->SetCollisionChannel(0, 0xFF);
  351. //m_physics->SetCollisionChannel(ColGroup, (1 << ColGroup)|(1));
  352. m_physics->SetMass(base_mass);
  353. m_physics->SetTransform(base_location);
  354. m_physics->InitBodyToRigid();
  355. m_physics->AddToSimulation(new_sim);
  356. }
  357. void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation = lol::quat(lol::mat3(1.0f)))
  358. {
  359. if (m_is_character)
  360. m_character->SetTransform(base_location, base_rotation);
  361. else
  362. m_physics->SetTransform(base_location, base_rotation);
  363. }
  364. lol::mat4 GetTransform()
  365. {
  366. if (m_is_character)
  367. return m_character->GetTransform();
  368. else
  369. return m_physics->GetTransform();
  370. }
  371. void SetRender(bool should_render)
  372. {
  373. m_should_render = should_render;
  374. }
  375. void SetCustomShaderData(GpuShaderData* custom_shader)
  376. {
  377. m_custom_shader = custom_shader;
  378. }
  379. GpuShaderData* GetCustomShaderData()
  380. {
  381. return m_custom_shader;
  382. }
  383. EasyMesh *GetMesh() { return &m_mesh; }
  384. EasyPhysic *GetPhysic() { return m_physics; }
  385. EasyCharacterController *GetCharacter() { return m_character; }
  386. ~PhysicsObject()
  387. {
  388. }
  389. std::string GetName() const { return "<PhysicsObject>"; }
  390. protected:
  391. virtual void tick_game(float seconds)
  392. {
  393. WorldEntity::tick_game(seconds);
  394. }
  395. virtual void tick_draw(float seconds, Scene &scene)
  396. {
  397. WorldEntity::tick_draw(seconds, scene);
  398. #if CAT_MODE
  399. if (!m_is_phys || m_custom_shader)
  400. #endif //CAT_MODE
  401. {
  402. if (!m_ready)
  403. {
  404. m_mesh.MeshConvert();
  405. /* FIXME: m_custom_shader is ignored */
  406. m_ready = true;
  407. }
  408. else if (m_should_render)
  409. {
  410. if (m_is_character)
  411. m_mesh.Render(scene, m_character->GetTransform());
  412. else
  413. m_mesh.Render(scene, m_physics->GetTransform());
  414. }
  415. }
  416. }
  417. private:
  418. // Base data
  419. EasyMesh m_mesh;
  420. EasyPhysic* m_physics;
  421. EasyCharacterController* m_character;
  422. GpuShaderData* m_custom_shader;
  423. bool m_ready;
  424. bool m_should_render;
  425. bool m_is_character;
  426. bool m_is_phys; /* Only used in CAT_MODE */
  427. };