You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 12 години
преди 12 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. /* 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 = NB_SPRITE;
  136. //SPRITE
  137. vec2 start_point = vec2((float)rand(nb_sprite), (float)rand(nb_sprite)) / vec2((float)nb_sprite);
  138. //vec2(0.f, .0f) / vec2((float)nb_sprite);
  139. vec2 size = vec2(1.f) / vec2((float)nb_sprite);
  140. m_mesh.BD()->SetTexCoordCustomBuild(MeshType::Quad, MeshFaceType::QuadDefault,
  141. start_point, start_point + size);
  142. m_mesh.BD()->SetTexCoordCustomBuild2(MeshType::Quad, MeshFaceType::QuadDefault,
  143. vec2(-PARTICLE_SIZE), vec2(PARTICLE_SIZE));
  144. MeshRand << "[sc#ffff aq 0 0]";
  145. MeshRand << "[sc#faaf aq 0 0]";
  146. MeshRand << "[sc#afaf aq 0 0]";
  147. MeshRand << "[sc#aaff aq 0 0]";
  148. #else
  149. MeshRand << "[sc#add asph1 2]";
  150. MeshRand << "[sc#dad asph1 2]";
  151. MeshRand << "[sc#dda asph1 2]";
  152. MeshRand << "[sc#daa asph1 2]";
  153. MeshRand << "[sc#ada asph1 2]";
  154. MeshRand << "[sc#aad asph1 2]";
  155. #endif
  156. MeshLimit << MeshRand.Count();
  157. MeshType << 1;
  158. #endif //USE_SPHERE
  159. #if USE_CONE
  160. MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  161. MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  162. MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  163. MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  164. MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  165. MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
  166. MeshLimit << MeshRand.Count();
  167. MeshType << 2;
  168. #endif //USE_CONE
  169. #if USE_CYLINDER
  170. MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  171. MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  172. MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  173. MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  174. MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  175. MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
  176. MeshLimit << MeshRand.Count();
  177. MeshType << 3;
  178. #endif //USE_CYLINDER
  179. #if USE_CAPSULE
  180. MeshRand << "[sc#add scb#add acap1 2 1]";
  181. MeshRand << "[sc#dad scb#dad acap1 2 1]";
  182. MeshRand << "[sc#dda scb#dda acap1 2 1]";
  183. MeshRand << "[sc#daa scb#daa acap1 2 1]";
  184. MeshRand << "[sc#ada scb#ada acap1 2 1]";
  185. MeshRand << "[sc#aad scb#aad acap1 2 1]";
  186. MeshLimit << MeshRand.Count();
  187. MeshType << 4;
  188. #endif //USE_CAPSULE
  189. int RandLimit = RandValue;
  190. if (MeshLimit.Count() <= RandValue || RandValue < 0)
  191. RandLimit = rand(MeshLimit.Count() - 1);
  192. RandValue = rand(MeshLimit[RandLimit], MeshLimit[RandLimit + 1]);
  193. m_physics = new EasyPhysic(this);
  194. m_mesh.Compile(MeshRand[RandValue]);
  195. m_mesh.Scale(vec3(OBJ_SIZE));
  196. vec3 BoxSize = vec3(2.0f) * OBJ_SIZE;
  197. int ColGroup = 1;
  198. switch (MeshType[RandLimit])
  199. {
  200. case 0:
  201. {
  202. m_physics->SetShapeToBox(BoxSize);
  203. ColGroup += 0;
  204. break;
  205. }
  206. case 1:
  207. {
  208. m_physics->SetShapeToSphere(BoxSize.x);
  209. ColGroup += 1;
  210. break;
  211. }
  212. case 2:
  213. {
  214. m_physics->SetShapeToCone(BoxSize.x, BoxSize.y);
  215. ColGroup += 2;
  216. break;
  217. }
  218. case 3:
  219. {
  220. m_physics->SetShapeToCylinder(BoxSize);
  221. ColGroup += 3;
  222. break;
  223. }
  224. case 4:
  225. {
  226. m_physics->SetShapeToCapsule(BoxSize.x, BoxSize.y);
  227. ColGroup += 4;
  228. break;
  229. }
  230. default:
  231. {
  232. }
  233. }
  234. m_physics->SetHitRestitution(1.0f);
  235. m_physics->SetCollisionChannel(0, 0xFF);
  236. //m_physics->SetCollisionChannel(ColGroup, (1 << ColGroup)|(1));
  237. m_physics->SetMass(base_mass);
  238. m_physics->SetTransform(base_location);
  239. m_physics->InitBodyToRigid();
  240. m_physics->AddToSimulation(new_sim);
  241. }
  242. void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)))
  243. {
  244. if (m_is_character)
  245. m_character->SetTransform(base_location, base_rotation);
  246. else
  247. m_physics->SetTransform(base_location, base_rotation);
  248. }
  249. lol::mat4 GetTransform()
  250. {
  251. if (m_is_character)
  252. return m_character->GetTransform();
  253. else
  254. return m_physics->GetTransform();
  255. }
  256. void SetRender(bool should_render)
  257. {
  258. m_should_render = should_render;
  259. }
  260. void SetCustomShaderData(GpuShaderData* custom_shader)
  261. {
  262. m_custom_shader = custom_shader;
  263. }
  264. GpuShaderData* GetCustomShaderData()
  265. {
  266. return m_custom_shader;
  267. }
  268. EasyMesh *GetMesh() { return &m_mesh; }
  269. EasyPhysic *GetPhysic() { return m_physics; }
  270. EasyCharacterController *GetCharacter() { return m_character; }
  271. ~PhysicsObject()
  272. {
  273. }
  274. char const *GetName() { return "<PhysicsObject>"; }
  275. protected:
  276. virtual void TickGame(float seconds)
  277. {
  278. WorldEntity::TickGame(seconds);
  279. }
  280. virtual void TickDraw(float seconds)
  281. {
  282. WorldEntity::TickDraw(seconds);
  283. #if CAT_MODE
  284. if (!m_is_phys || m_custom_shader)
  285. #endif //CAT_MODE
  286. {
  287. if (!m_ready)
  288. {
  289. if (m_custom_shader)
  290. m_mesh.MeshConvert(m_custom_shader);
  291. else
  292. m_mesh.MeshConvert();
  293. m_ready = true;
  294. }
  295. else if (m_should_render)
  296. {
  297. if (m_is_character)
  298. m_mesh.Render(m_character->GetTransform());
  299. else
  300. m_mesh.Render(m_physics->GetTransform());
  301. }
  302. }
  303. }
  304. private:
  305. //Base datas
  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. #if CAT_MODE
  314. bool m_is_phys;
  315. #endif //CAT_MODE
  316. };
  317. #endif /* __PHYSICOBJECT_H__ */