No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

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