您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

physicobject.h 12 KiB

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