Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

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