Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

494 rader
15 KiB

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