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 години
преди 12 години
преди 11 години
преди 11 години
преди 12 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. //
  2. // BtPhysTest
  3. //
  4. // Copyright: (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  5. // (c) 2012-2013 Sam Hocevar <sam@hocevar.net>
  6. //
  7. #if defined HAVE_CONFIG_H
  8. # include "config.h"
  9. #endif
  10. #include "core.h"
  11. #include "loldebug.h"
  12. using namespace lol;
  13. #ifndef HAVE_PHYS_USE_BULLET
  14. #define HAVE_PHYS_USE_BULLET
  15. #endif /* HAVE_PHYS_USE_BULLET */
  16. #include "physics/lolphysics.h"
  17. #include "physics/easyphysics.h"
  18. #include "physicobject.h"
  19. #include "btphystest.h"
  20. using namespace lol::phys;
  21. #define CUBE_HALF_EXTENTS .5f
  22. #define EXTRA_HEIGHT 1.f
  23. int gNumObjects = 64;
  24. #define USE_WALL 1
  25. #define USE_PLATFORM 1
  26. #define USE_ROPE 0
  27. #define USE_BODIES 1
  28. #define USE_ROTATION 0
  29. #define USE_CHARACTER 1
  30. #define USE_STAIRS 1
  31. #define IPT_MOVE_FORWARD "Move_Forward"
  32. #define IPT_MOVE_BACKWARD "Move_Backward"
  33. #define IPT_MOVE_LEFT "Move_Left"
  34. #define IPT_MOVE_RIGHT "Move_Right"
  35. #define IPT_MOVE_UP "Move_Up"
  36. #define IPT_MOVE_DOWN "Move_Down"
  37. #define IPT_MOVE_JUMP "Move_Jump"
  38. BtPhysTest::BtPhysTest(bool editor)
  39. {
  40. m_loop_value = .0f;
  41. /* Create a camera that matches the settings of XNA BtPhysTest */
  42. m_camera = new Camera();
  43. m_camera->SetView(vec3(50.f, 50.f, 0.f),
  44. vec3(0.f, 0.f, 0.f),
  45. vec3(0, 1, 0));
  46. m_camera->SetProjection(mat4::perspective(45.f, 1280.f, 960.f, .1f, 1000.f));
  47. g_scene->PushCamera(m_camera);
  48. m_ready = false;
  49. m_simulation = new Simulation();
  50. m_simulation->SetWorldLimit(vec3(-1000.0f, -1000.0f, -1000.0f), vec3(1000.0f, 1000.0f, 1000.0f));
  51. m_simulation->Init();
  52. vec3 NewGravity = vec3(.0f, -10.0f, .0f);
  53. m_simulation->SetGravity(NewGravity);
  54. m_simulation->SetContinuousDetection(true);
  55. m_simulation->SetTimestep(1.f / 120.f);
  56. Ticker::Ref(m_simulation);
  57. /* Add a white directional light */
  58. m_light1 = new Light();
  59. m_light1->SetPosition(vec4(0.2f, 0.2f, 0.f, 0.f));
  60. m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
  61. Ticker::Ref(m_light1);
  62. /* Add an orangeish point light */
  63. m_light2 = new Light();
  64. m_light2->SetPosition(vec4(-15.f, 15.f, 15.f, 1.f));
  65. m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
  66. Ticker::Ref(m_light2);
  67. float offset = 29.5f;
  68. vec3 pos_offset = vec3(.0f, 30.f, .0f);
  69. if (USE_STAIRS)
  70. {
  71. vec3 new_offset = vec3(1.0f, .125f, .0f);
  72. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  73. vec3 NewPosition = pos_offset + vec3(5.0f, -29.f, 15.0f);
  74. {
  75. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 30.f);
  76. NewPosition += vec3(4.0f, .0f, -4.0f);
  77. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  78. Ticker::Ref(NewPhyobj);
  79. m_stairs_list << NewPhyobj;
  80. }
  81. {
  82. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 40.f);
  83. NewPosition += vec3(4.0f, .0f, -4.0f);
  84. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  85. Ticker::Ref(NewPhyobj);
  86. m_stairs_list << NewPhyobj;
  87. }
  88. NewPosition = pos_offset + vec3(5.0f, -29.5f, 15.0f);
  89. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  90. for (int i=0; i < 15; i++)
  91. {
  92. NewPosition += new_offset;
  93. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  94. Ticker::Ref(NewPhyobj);
  95. m_stairs_list << NewPhyobj;
  96. }
  97. }
  98. if (USE_WALL)
  99. {
  100. for (int i=0; i < 6; i++)
  101. {
  102. vec3 NewPosition = vec3(.0f);
  103. quat NewRotation = quat(1.f);
  104. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation);
  105. int idx = i/2;
  106. NewPosition = pos_offset;
  107. NewPosition[idx] += offset;
  108. offset *= -1.f;
  109. if (idx != 1)
  110. {
  111. vec3 NewAxis = vec3(.0f);
  112. NewAxis[2 - idx] = 1;
  113. NewRotation = quat::rotate(90.f, NewAxis);
  114. }
  115. NewPhyobj->SetTransform(NewPosition, NewRotation);
  116. Ticker::Ref(NewPhyobj);
  117. m_ground_list << NewPhyobj;
  118. }
  119. }
  120. PhysicsObject* BasePhyobj = NULL;
  121. if (USE_PLATFORM)
  122. {
  123. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  124. vec3 NewPosition = pos_offset + vec3(5.0f, -25.0f, -15.0f);
  125. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  126. m_platform_list << NewPhyobj;
  127. Ticker::Ref(NewPhyobj);
  128. NewPosition = pos_offset + vec3(-15.0f, -25.0f, 5.0f);
  129. NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  130. BasePhyobj = NewPhyobj;
  131. m_platform_list << NewPhyobj;
  132. Ticker::Ref(NewPhyobj);
  133. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 90.f);
  134. NewPosition = pos_offset + vec3(-20.0f, -25.0f, 5.0f);
  135. NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  136. NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, true);
  137. m_platform_list << NewPhyobj;
  138. Ticker::Ref(NewPhyobj);
  139. //NewPosition += vec3(-0.0f, .0f, .0f);
  140. //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  141. //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, false);
  142. //m_platform_list << NewPhyobj;
  143. //Ticker::Ref(NewPhyobj);
  144. //NewPosition += vec3(-2.0f, .0f, .0f);
  145. //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  146. //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), false, false);
  147. //m_platform_list << NewPhyobj;
  148. //Ticker::Ref(NewPhyobj);
  149. }
  150. if (USE_CHARACTER)
  151. {
  152. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  153. vec3 NewPosition = pos_offset + vec3(-5.0f, -10.0f, 15.0f);
  154. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 2);
  155. m_character_list << NewPhyobj;
  156. Ticker::Ref(NewPhyobj);
  157. Input::LinkActionToKey(IPT_MOVE_FORWARD, Key::Up);
  158. Input::LinkActionToKey(IPT_MOVE_BACKWARD, Key::Down);
  159. Input::LinkActionToKey(IPT_MOVE_LEFT, Key::Left);
  160. Input::LinkActionToKey(IPT_MOVE_RIGHT, Key::Right);
  161. Input::LinkActionToKey(IPT_MOVE_JUMP, Key::Space);
  162. Input::LinkActionToKey(IPT_MOVE_UP, Key::PageUp);
  163. Input::LinkActionToKey(IPT_MOVE_DOWN, Key::PageDown);
  164. //NewPhyobj->GetCharacter()->AttachTo(BasePhyobj->GetPhysic(), true, true);
  165. }
  166. if (USE_BODIES)
  167. {
  168. for (int x=0; x < 6; x++)
  169. {
  170. for (int y=0; y < 6; y++)
  171. {
  172. for (int z=0; z < 5; z++)
  173. {
  174. PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
  175. vec3(-20.f, 15.f, -20.f) +
  176. vec3(8.f * (float)x, 8.f * (float)y, 8.f * (float)z));
  177. m_physobj_list << new_physobj;
  178. Ticker::Ref(new_physobj);
  179. }
  180. }
  181. }
  182. }
  183. if (USE_ROPE)
  184. {
  185. Array<PhysicsObject*> RopeElements;
  186. for (int i = 0; i < 14; i++)
  187. {
  188. PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
  189. vec3(0.f, 15.f, -20.f) +
  190. vec3(0.f, 0.f, 2.f * (float)i), 1);
  191. RopeElements << new_physobj;
  192. m_physobj_list << new_physobj;
  193. Ticker::Ref(new_physobj);
  194. if (RopeElements.Count() > 1)
  195. {
  196. EasyConstraint* new_constraint = new EasyConstraint();
  197. vec3 A2B = .5f * (RopeElements[i]->GetPhysic()->GetTransform().v3.xyz -
  198. RopeElements[i - 1]->GetPhysic()->GetTransform().v3.xyz);
  199. new_constraint->SetPhysObjA(RopeElements[i - 1]->GetPhysic(), lol::mat4::translate(A2B));
  200. new_constraint->SetPhysObjB(RopeElements[i]->GetPhysic(), lol::mat4::translate(-A2B));
  201. new_constraint->InitConstraintToPoint2Point();
  202. new_constraint->DisableCollisionBetweenObjs(true);
  203. new_constraint->AddToSimulation(m_simulation);
  204. m_constraint_list << new_constraint;
  205. }
  206. }
  207. }
  208. }
  209. void BtPhysTest::TickGame(float seconds)
  210. {
  211. WorldEntity::TickGame(seconds);
  212. if (Input::WasReleased(Key::Escape))
  213. Ticker::Shutdown();
  214. m_loop_value += seconds;
  215. if (m_loop_value > F_PI * 2.0f)
  216. m_loop_value -= F_PI * 2.0f;
  217. vec3 GroundBarycenter = vec3(.0f);
  218. vec3 PhysObjBarycenter = vec3(.0f);
  219. float factor = .0f;
  220. if (USE_WALL)
  221. {
  222. for (int i = 0; i < m_ground_list.Count(); i++)
  223. {
  224. PhysicsObject* PhysObj = m_ground_list[i];
  225. mat4 GroundMat = PhysObj->GetTransform();
  226. GroundBarycenter += GroundMat.v3.xyz;
  227. factor += 1.f;
  228. }
  229. GroundBarycenter /= factor;
  230. for (int i = 0; i < m_ground_list.Count(); i++)
  231. {
  232. PhysicsObject* PhysObj = m_ground_list[i];
  233. mat4 GroundMat = PhysObj->GetTransform();
  234. vec3 CenterToGround = GroundMat.v3.xyz - GroundBarycenter;
  235. vec3 CenterToCam = m_camera->GetPosition() - GroundBarycenter;
  236. if (dot(normalize(CenterToCam - CenterToGround),
  237. normalize(CenterToGround)) > 0.f)
  238. PhysObj->SetRender(false);
  239. else
  240. PhysObj->SetRender(true);
  241. }
  242. }
  243. if (USE_ROTATION)
  244. {
  245. for (int i = 0; i < m_ground_list.Count(); i++)
  246. {
  247. PhysicsObject* PhysObj = m_ground_list[i];
  248. mat4 GroundMat = PhysObj->GetTransform();
  249. mat4 CenterMx = mat4::translate(GroundBarycenter);
  250. GroundMat = inverse(CenterMx) * GroundMat;
  251. GroundMat = CenterMx *
  252. mat4(quat::fromeuler_xyz(vec3(.0f, 20.f, 20.0f) * seconds))
  253. * GroundMat;
  254. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  255. }
  256. }
  257. if (USE_PLATFORM)
  258. {
  259. for (int i = 0; i < m_platform_list.Count(); i++)
  260. {
  261. PhysicsObject* PhysObj = m_platform_list[i];
  262. mat4 GroundMat = PhysObj->GetTransform();
  263. if (i == 0)
  264. {
  265. GroundMat = GroundMat * mat4(quat::fromeuler_xyz(vec3(20.f, .0f, .0f) * seconds));
  266. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  267. }
  268. else if (i == 1)
  269. {
  270. GroundMat =
  271. mat4::translate(vec3(-15.0f, 5.0f, lol::cos(m_loop_value) * 8.f)) *
  272. mat4(quat::fromeuler_xyz(vec3(.0f, lol::cos(m_loop_value) * 20.f, .0f)));
  273. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  274. }
  275. }
  276. }
  277. if (USE_CHARACTER)
  278. {
  279. for (int i = 0; i < m_character_list.Count(); i++)
  280. {
  281. PhysicsObject* PhysObj = m_character_list[i];
  282. EasyCharacterController* Character = (EasyCharacterController*)PhysObj->GetCharacter();
  283. mat4 CtlrMx = Character->GetTransform();
  284. int HMovement = Input::GetStatus(IPT_MOVE_RIGHT) - Input::GetStatus(IPT_MOVE_LEFT);
  285. int VMovement = Input::GetStatus(IPT_MOVE_FORWARD) - Input::GetStatus(IPT_MOVE_BACKWARD);
  286. int RMovement = Input::GetStatus(IPT_MOVE_UP) - Input::GetStatus(IPT_MOVE_DOWN);
  287. vec3 CharMove = vec3((float)VMovement * seconds * 4.f, (float)RMovement * seconds * 10.f, (float)HMovement * seconds * 4.f);
  288. if (Input::WasReleased(IPT_MOVE_JUMP))
  289. Character->Jump();
  290. Character->SetMovementForFrame(CharMove);
  291. RayCastResult HitResult;
  292. if (m_simulation->RayHits(HitResult, ERT_Closest, Character->GetTransform().v3.xyz, (Character->GetTransform().v3.xyz + vec3(.0f, -1.f, .0f)), Character))
  293. Character->AttachTo(HitResult.m_collider_list[0], true, true);
  294. else
  295. Character->AttachTo(NULL);
  296. }
  297. }
  298. if (USE_CHARACTER)
  299. {
  300. PhysObjBarycenter = vec3(.0f);
  301. factor = .0f;
  302. for (int i = 0; i < m_character_list.Count(); i++)
  303. {
  304. PhysicsObject* PhysObj = m_character_list[i];
  305. mat4 GroundMat = PhysObj->GetTransform();
  306. PhysObjBarycenter += GroundMat.v3.xyz;
  307. factor += 1.f;
  308. }
  309. PhysObjBarycenter /= factor;
  310. #if 0
  311. m_camera->SetTarget(m_camera->GetTarget() + (seconds / (seconds + 0.18f)) * (PhysObjBarycenter - m_camera->GetTarget()));
  312. vec3 CamPosCenter = m_camera->GetTarget() + vec3(.0f, 5.0f, .0f);
  313. m_camera->SetPosition(CamPosCenter + normalize(m_camera->GetPosition() - CamPosCenter) * 20.0f);
  314. #endif
  315. }
  316. else
  317. {
  318. PhysObjBarycenter = vec3(.0f);
  319. for (int i = 0; i < m_physobj_list.Count(); i++)
  320. {
  321. PhysicsObject* PhysObj = m_physobj_list[i];
  322. mat4 GroundMat = PhysObj->GetTransform();
  323. PhysObjBarycenter += GroundMat.v3.xyz;
  324. factor += 1.f;
  325. }
  326. PhysObjBarycenter /= factor;
  327. #if 0
  328. m_camera->SetTarget(PhysObjBarycenter);
  329. m_camera->SetPosition(GroundBarycenter + normalize(GroundBarycenter - PhysObjBarycenter) * 60.0f);
  330. #endif
  331. }
  332. }
  333. void BtPhysTest::TickDraw(float seconds)
  334. {
  335. WorldEntity::TickDraw(seconds);
  336. if (!m_ready)
  337. {
  338. /* FIXME: this object never cleans up */
  339. m_ready = true;
  340. }
  341. //Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
  342. }
  343. BtPhysTest::~BtPhysTest()
  344. {
  345. g_scene->PopCamera(m_camera);
  346. Ticker::Unref(m_light1);
  347. Ticker::Unref(m_light2);
  348. while (m_constraint_list.Count())
  349. {
  350. EasyConstraint* CurPop = m_constraint_list.Last();
  351. m_constraint_list.Pop();
  352. CurPop->RemoveFromSimulation(m_simulation);
  353. delete CurPop;
  354. }
  355. while (m_ground_list.Count())
  356. {
  357. PhysicsObject* CurPop = m_ground_list.Last();
  358. m_ground_list.Pop();
  359. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  360. Ticker::Unref(CurPop);
  361. }
  362. while (m_stairs_list.Count())
  363. {
  364. PhysicsObject* CurPop = m_stairs_list.Last();
  365. m_stairs_list.Pop();
  366. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  367. Ticker::Unref(CurPop);
  368. }
  369. while (m_character_list.Count())
  370. {
  371. PhysicsObject* CurPop = m_character_list.Last();
  372. m_character_list.Pop();
  373. CurPop->GetCharacter()->RemoveFromSimulation(m_simulation);
  374. Ticker::Unref(CurPop);
  375. }
  376. while (m_platform_list.Count())
  377. {
  378. PhysicsObject* CurPop = m_platform_list.Last();
  379. m_platform_list.Pop();
  380. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  381. Ticker::Unref(CurPop);
  382. }
  383. while (m_physobj_list.Count())
  384. {
  385. PhysicsObject* CurPop = m_physobj_list.Last();
  386. m_physobj_list.Pop();
  387. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  388. Ticker::Unref(CurPop);
  389. }
  390. Ticker::Unref(m_simulation);
  391. }
  392. int main(int argc, char **argv)
  393. {
  394. System::Init(argc, argv);
  395. Application app("BtPhysTest", ivec2(1280, 720), 60.0f);
  396. new BtPhysTest(argc > 1);
  397. app.ShowPointer(false);
  398. app.Run();
  399. return EXIT_SUCCESS;
  400. }