25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

486 satır
15 KiB

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