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.
 
 
 

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