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.

physicobject.h 11 KiB

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