Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

486 wiersze
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. #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. BtPhysTest::BtPhysTest(bool editor)
  32. {
  33. m_loop_value = .0f;
  34. /* Register an input controller for the keyboard */
  35. m_controller = new Controller(KEY_MAX, 0);
  36. m_controller->GetKey(KEY_MOVE_FORWARD).Bind("Keyboard", "Up");
  37. m_controller->GetKey(KEY_MOVE_BACK).Bind("Keyboard", "Down");
  38. m_controller->GetKey(KEY_MOVE_LEFT).Bind("Keyboard", "Left");
  39. m_controller->GetKey(KEY_MOVE_RIGHT).Bind("Keyboard", "Right");
  40. m_controller->GetKey(KEY_MOVE_JUMP).Bind("Keyboard", "Space");
  41. m_controller->GetKey(KEY_MOVE_UP).Bind("Keyboard", "PageUp");
  42. m_controller->GetKey(KEY_MOVE_DOWN).Bind("Keyboard", "PageDown");
  43. m_controller->GetKey(KEY_QUIT).Bind("Keyboard", "Escape");
  44. /* Create a camera that matches the settings of XNA BtPhysTest */
  45. m_camera = new Camera();
  46. m_camera->SetView(vec3(50.f, 50.f, 0.f),
  47. vec3(0.f, 0.f, 0.f),
  48. vec3(0, 1, 0));
  49. m_camera->SetProjection(mat4::perspective(45.f, 1280.f, 960.f, .1f, 1000.f));
  50. g_scene->PushCamera(m_camera);
  51. m_ready = false;
  52. m_simulation = new Simulation();
  53. m_simulation->SetWorldLimit(vec3(-1000.0f, -1000.0f, -1000.0f), vec3(1000.0f, 1000.0f, 1000.0f));
  54. m_simulation->Init();
  55. vec3 NewGravity = vec3(.0f, -10.0f, .0f);
  56. m_simulation->SetGravity(NewGravity);
  57. m_simulation->SetContinuousDetection(true);
  58. m_simulation->SetTimestep(1.f / 120.f);
  59. Ticker::Ref(m_simulation);
  60. /* Add a white directional light */
  61. m_light1 = new Light();
  62. m_light1->SetPosition(vec4(0.2f, 0.2f, 0.f, 0.f));
  63. m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
  64. Ticker::Ref(m_light1);
  65. /* Add an orangeish point light */
  66. m_light2 = new Light();
  67. m_light2->SetPosition(vec4(-15.f, 15.f, 15.f, 1.f));
  68. m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
  69. Ticker::Ref(m_light2);
  70. float offset = 29.5f;
  71. vec3 pos_offset = vec3(.0f, 30.f, .0f);
  72. if (USE_STAIRS)
  73. {
  74. vec3 new_offset = vec3(1.0f, .125f, .0f);
  75. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  76. vec3 NewPosition = pos_offset + vec3(5.0f, -29.f, 15.0f);
  77. {
  78. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 30.f);
  79. NewPosition += vec3(4.0f, .0f, -4.0f);
  80. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  81. Ticker::Ref(NewPhyobj);
  82. m_stairs_list << NewPhyobj;
  83. }
  84. {
  85. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 40.f);
  86. NewPosition += vec3(4.0f, .0f, -4.0f);
  87. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  88. Ticker::Ref(NewPhyobj);
  89. m_stairs_list << NewPhyobj;
  90. }
  91. NewPosition = pos_offset + vec3(5.0f, -29.5f, 15.0f);
  92. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  93. for (int i=0; i < 15; i++)
  94. {
  95. NewPosition += new_offset;
  96. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  97. Ticker::Ref(NewPhyobj);
  98. m_stairs_list << NewPhyobj;
  99. }
  100. }
  101. if (USE_WALL)
  102. {
  103. for (int i=0; i < 6; i++)
  104. {
  105. vec3 NewPosition = vec3(.0f);
  106. quat NewRotation = quat(1.f);
  107. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation);
  108. int idx = i/2;
  109. NewPosition = pos_offset;
  110. NewPosition[idx] += offset;
  111. offset *= -1.f;
  112. if (idx != 1)
  113. {
  114. vec3 NewAxis = vec3(.0f);
  115. NewAxis[2 - idx] = 1;
  116. NewRotation = quat::rotate(90.f, NewAxis);
  117. }
  118. NewPhyobj->SetTransform(NewPosition, NewRotation);
  119. Ticker::Ref(NewPhyobj);
  120. m_ground_list << NewPhyobj;
  121. }
  122. }
  123. PhysicsObject* BasePhyobj = NULL;
  124. if (USE_PLATFORM)
  125. {
  126. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  127. vec3 NewPosition = pos_offset + vec3(5.0f, -25.0f, -15.0f);
  128. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  129. m_platform_list << NewPhyobj;
  130. Ticker::Ref(NewPhyobj);
  131. NewPosition = pos_offset + vec3(-15.0f, -25.0f, 5.0f);
  132. NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  133. BasePhyobj = NewPhyobj;
  134. m_platform_list << NewPhyobj;
  135. Ticker::Ref(NewPhyobj);
  136. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 90.f);
  137. NewPosition = pos_offset + vec3(-20.0f, -25.0f, 5.0f);
  138. NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  139. NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, true);
  140. m_platform_list << NewPhyobj;
  141. Ticker::Ref(NewPhyobj);
  142. //NewPosition += vec3(-0.0f, .0f, .0f);
  143. //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  144. //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, false);
  145. //m_platform_list << NewPhyobj;
  146. //Ticker::Ref(NewPhyobj);
  147. //NewPosition += vec3(-2.0f, .0f, .0f);
  148. //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  149. //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), false, false);
  150. //m_platform_list << NewPhyobj;
  151. //Ticker::Ref(NewPhyobj);
  152. }
  153. if (USE_CHARACTER)
  154. {
  155. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  156. vec3 NewPosition = pos_offset + vec3(-5.0f, -10.0f, 15.0f);
  157. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 2);
  158. m_character_list << NewPhyobj;
  159. Ticker::Ref(NewPhyobj);
  160. //NewPhyobj->GetCharacter()->AttachTo(BasePhyobj->GetPhysic(), true, true);
  161. }
  162. if (USE_BODIES)
  163. {
  164. for (int x=0; x < 6; x++)
  165. {
  166. for (int y=0; y < 6; y++)
  167. {
  168. for (int z=0; z < 5; z++)
  169. {
  170. PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
  171. vec3(-20.f, 15.f, -20.f) +
  172. vec3(8.f * (float)x, 8.f * (float)y, 8.f * (float)z));
  173. m_physobj_list << new_physobj;
  174. Ticker::Ref(new_physobj);
  175. }
  176. }
  177. }
  178. }
  179. if (USE_ROPE)
  180. {
  181. Array<PhysicsObject*> RopeElements;
  182. for (int i = 0; i < 14; i++)
  183. {
  184. PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
  185. vec3(0.f, 15.f, -20.f) +
  186. vec3(0.f, 0.f, 2.f * (float)i), 1);
  187. RopeElements << new_physobj;
  188. m_physobj_list << new_physobj;
  189. Ticker::Ref(new_physobj);
  190. if (RopeElements.Count() > 1)
  191. {
  192. EasyConstraint* new_constraint = new EasyConstraint();
  193. vec3 A2B = .5f * (RopeElements[i]->GetPhysic()->GetTransform().v3.xyz -
  194. RopeElements[i - 1]->GetPhysic()->GetTransform().v3.xyz);
  195. new_constraint->SetPhysObjA(RopeElements[i - 1]->GetPhysic(), lol::mat4::translate(A2B));
  196. new_constraint->SetPhysObjB(RopeElements[i]->GetPhysic(), lol::mat4::translate(-A2B));
  197. new_constraint->InitConstraintToPoint2Point();
  198. new_constraint->DisableCollisionBetweenObjs(true);
  199. new_constraint->AddToSimulation(m_simulation);
  200. m_constraint_list << new_constraint;
  201. }
  202. }
  203. }
  204. }
  205. void BtPhysTest::TickGame(float seconds)
  206. {
  207. WorldEntity::TickGame(seconds);
  208. if (m_controller->GetKey(KEY_QUIT).IsReleased())
  209. Ticker::Shutdown();
  210. m_loop_value += seconds;
  211. if (m_loop_value > F_PI * 2.0f)
  212. m_loop_value -= F_PI * 2.0f;
  213. vec3 GroundBarycenter = vec3(.0f);
  214. vec3 PhysObjBarycenter = vec3(.0f);
  215. float factor = .0f;
  216. if (USE_WALL)
  217. {
  218. for (int i = 0; i < m_ground_list.Count(); i++)
  219. {
  220. PhysicsObject* PhysObj = m_ground_list[i];
  221. mat4 GroundMat = PhysObj->GetTransform();
  222. GroundBarycenter += GroundMat.v3.xyz;
  223. factor += 1.f;
  224. }
  225. GroundBarycenter /= factor;
  226. for (int i = 0; i < m_ground_list.Count(); i++)
  227. {
  228. PhysicsObject* PhysObj = m_ground_list[i];
  229. mat4 GroundMat = PhysObj->GetTransform();
  230. vec3 CenterToGround = GroundMat.v3.xyz - GroundBarycenter;
  231. vec3 CenterToCam = m_camera->GetPosition() - GroundBarycenter;
  232. if (dot(normalize(CenterToCam - CenterToGround),
  233. normalize(CenterToGround)) > 0.f)
  234. PhysObj->SetRender(false);
  235. else
  236. PhysObj->SetRender(true);
  237. }
  238. }
  239. if (USE_ROTATION)
  240. {
  241. for (int i = 0; i < m_ground_list.Count(); i++)
  242. {
  243. PhysicsObject* PhysObj = m_ground_list[i];
  244. mat4 GroundMat = PhysObj->GetTransform();
  245. mat4 CenterMx = mat4::translate(GroundBarycenter);
  246. GroundMat = inverse(CenterMx) * GroundMat;
  247. GroundMat = CenterMx *
  248. mat4(quat::fromeuler_xyz(vec3(.0f, 20.f, 20.0f) * seconds))
  249. * GroundMat;
  250. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  251. }
  252. }
  253. if (USE_PLATFORM)
  254. {
  255. for (int i = 0; i < m_platform_list.Count(); i++)
  256. {
  257. PhysicsObject* PhysObj = m_platform_list[i];
  258. mat4 GroundMat = PhysObj->GetTransform();
  259. if (i == 0)
  260. {
  261. GroundMat = GroundMat * mat4(quat::fromeuler_xyz(vec3(20.f, .0f, .0f) * seconds));
  262. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  263. }
  264. else if (i == 1)
  265. {
  266. GroundMat =
  267. mat4::translate(vec3(-15.0f, 5.0f, lol::cos(m_loop_value) * 8.f)) *
  268. mat4(quat::fromeuler_xyz(vec3(.0f, lol::cos(m_loop_value) * 20.f, .0f)));
  269. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  270. }
  271. }
  272. }
  273. if (USE_CHARACTER)
  274. {
  275. for (int i = 0; i < m_character_list.Count(); i++)
  276. {
  277. PhysicsObject* PhysObj = m_character_list[i];
  278. EasyCharacterController* Character = (EasyCharacterController*)PhysObj->GetCharacter();
  279. mat4 CtlrMx = Character->GetTransform();
  280. vec3 movement(0.f);
  281. movement.z = (m_controller->GetKey(KEY_MOVE_RIGHT).IsDown() ? 1.f : 0.f)
  282. - (m_controller->GetKey(KEY_MOVE_LEFT).IsDown() ? 1.f : 0.f);
  283. movement.x = (m_controller->GetKey(KEY_MOVE_FORWARD).IsDown() ? 1.f : 0.f)
  284. - (m_controller->GetKey(KEY_MOVE_BACK).IsDown() ? 1.f : 0.f);
  285. movement.y = (m_controller->GetKey(KEY_MOVE_UP).IsDown() ? 1.f : 0.f)
  286. - (m_controller->GetKey(KEY_MOVE_DOWN).IsDown() ? 1.f : 0.f);
  287. vec3 CharMove = movement * seconds * vec3(4.f, 10.f, 4.f);
  288. if (m_controller->GetKey(KEY_MOVE_JUMP).IsReleased())
  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. }