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.

756 lines
24 KiB

  1. //
  2. // Lol Engine — BtPhys tutorial
  3. //
  4. // Copyright © 2012—2020 Sam Hocevar <sam@hocevar.net>
  5. // © 2009—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #if HAVE_CONFIG_H
  14. # include "config.h"
  15. #endif
  16. #include <lol/engine.h>
  17. #include <lol/color>
  18. #include "loldebug.h"
  19. using namespace lol;
  20. #include "physics/lolphysics.h"
  21. #include "physics/easyphysics.h"
  22. #define CAT_MODE 0
  23. #define OBJ_SIZE 2.f
  24. #define NB_SPRITE 4
  25. #define PARTICLE_SIZE 4
  26. #include "physicobject.h"
  27. #include "btphystest.h"
  28. using namespace lol::phys;
  29. #define CUBE_HALF_EXTENTS .5f
  30. #define EXTRA_HEIGHT 1.f
  31. #define BASE_TIME 2.f
  32. #define ZERO_TIME (BASE_TIME + rand(-BASE_TIME * .4f, BASE_TIME * .4f))
  33. #define ZERO_SPEED 3.5f
  34. #define JUMP_HEIGHT 30.f
  35. #define JUMP_STRAFE .5f
  36. #define TARGET_TIMER 10.f + (rand(4.f) - 2.f)
  37. int gNumObjects = 16;
  38. #if CAT_MODE
  39. #define USE_WALL 1
  40. #define USE_BODIES 1
  41. #else
  42. #define USE_WALL 1
  43. #define USE_PLATFORM 0
  44. #define USE_ROPE 0
  45. #define USE_BODIES 1
  46. #define USE_ROTATION 1
  47. #define USE_CHARACTER 0
  48. #define USE_STAIRS 0
  49. #endif
  50. LOLFX_RESOURCE_DECLARE(front_camera_sprite);
  51. BtPhysTest::BtPhysTest(bool editor)
  52. {
  53. m_init_status = 0;
  54. }
  55. void BtPhysTest::InitApp()
  56. {
  57. m_init_status = 1;
  58. m_loop_value = .0f;
  59. #if CAT_MODE
  60. /* cat datas setup */
  61. m_cat_texture = Tiler::Register("data/CatsSheet.png", ivec2::zero, ivec2(0,1));
  62. m_fov_dp = .0f;
  63. m_loc_dp = .0f;
  64. #endif //CAT_MODE
  65. /* Create a camera that matches the settings of XNA BtPhysTest */
  66. m_camera = new Camera();
  67. #if CAT_MODE
  68. m_camera->SetView(vec3(70.f, 50.f, 0.f),
  69. vec3(0.f, 0.f, 0.f),
  70. vec3(0, 1, 0));
  71. m_camera->SetProjection(radians(60.f), .1f, 1000.f, (float)Video::GetSize().x, (float)Video::GetSize().y / (float)Video::GetSize().x);
  72. m_target_timer = TARGET_TIMER;
  73. m_cam_target = -1;
  74. #else
  75. m_camera->SetView(vec3(50.f, 50.f, 0.f),
  76. vec3(0.f, 0.f, 0.f),
  77. vec3(0, 1, 0));
  78. m_camera->SetProjection(radians(45.f), .1f, 1000.f, (float)Video::GetSize().x, (float)Video::GetSize().y / (float)Video::GetSize().x);
  79. #endif
  80. Scene& scene = Scene::GetScene();
  81. scene.PushCamera(m_camera);
  82. m_ready = false;
  83. m_simulation = new Simulation();
  84. m_simulation->SetWorldLimit(vec3(-1000.0f, -1000.0f, -1000.0f), vec3(1000.0f, 1000.0f, 1000.0f));
  85. m_simulation->Init();
  86. vec3 NewGravity = vec3(.0f, -10.0f, .0f);
  87. m_simulation->SetGravity(NewGravity);
  88. m_simulation->SetContinuousDetection(true);
  89. m_simulation->SetTimestep(1.f / 120.f);
  90. Ticker::Ref(m_simulation);
  91. /* Add a white directional light */
  92. m_light1 = new Light();
  93. m_light1->SetPosition(vec3(0.2f, 0.2f, 0.f));
  94. m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
  95. m_light1->SetType(LightType::Directional);
  96. Ticker::Ref(m_light1);
  97. /* Add an orangeish point light */
  98. m_light2 = new Light();
  99. m_light2->SetPosition(vec3(-15.f, 15.f, 15.f));
  100. m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
  101. m_light2->SetType(LightType::Point);
  102. Ticker::Ref(m_light2);
  103. float offset = 29.5f;
  104. vec3 pos_offset = vec3(.0f, 30.f, .0f);
  105. #if USE_STAIRS
  106. {
  107. vec3 new_offset = vec3(1.0f, .125f, .0f);
  108. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  109. vec3 NewPosition = pos_offset + vec3(5.0f, -29.f, 15.0f);
  110. {
  111. NewRotation = quat::fromeuler_xyz(0.f, 0.f, radians(30.f));
  112. NewPosition += vec3(4.0f, .0f, -4.0f);
  113. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  114. Ticker::Ref(NewPhyobj);
  115. m_stairs_list << NewPhyobj;
  116. }
  117. {
  118. NewRotation = quat::fromeuler_xyz(0.f, 0.f, radians(40.f));
  119. NewPosition += vec3(4.0f, .0f, -4.0f);
  120. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  121. Ticker::Ref(NewPhyobj);
  122. m_stairs_list << NewPhyobj;
  123. }
  124. NewPosition = pos_offset + vec3(5.0f, -29.5f, 15.0f);
  125. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  126. for (int i=0; i < 15; i++)
  127. {
  128. NewPosition += new_offset;
  129. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  130. Ticker::Ref(NewPhyobj);
  131. m_stairs_list << NewPhyobj;
  132. }
  133. }
  134. #endif //USE_STAIRS
  135. #if USE_WALL
  136. {
  137. for (int i=0; i < 6; i++)
  138. {
  139. vec3 NewPosition = vec3(.0f);
  140. quat NewRotation = quat(1.f);
  141. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation);
  142. int idx = i/2;
  143. NewPosition = pos_offset;
  144. NewPosition[idx] += offset;
  145. offset *= -1.f;
  146. if (idx != 1)
  147. {
  148. vec3 NewAxis = vec3(.0f);
  149. NewAxis[2 - idx] = 1;
  150. NewRotation = quat::rotate(radians(90.f), NewAxis);
  151. }
  152. NewPhyobj->SetTransform(NewPosition, NewRotation);
  153. Ticker::Ref(NewPhyobj);
  154. m_ground_list << NewPhyobj;
  155. }
  156. }
  157. #endif //USE_WALL
  158. PhysicsObject* BasePhyobj = nullptr;
  159. #if USE_PLATFORM
  160. {
  161. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  162. vec3 NewPosition = pos_offset + vec3(5.0f, -25.0f, -15.0f);
  163. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  164. m_platform_list << NewPhyobj;
  165. Ticker::Ref(NewPhyobj);
  166. NewPosition = pos_offset + vec3(-15.0f, -25.0f, 5.0f);
  167. NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  168. BasePhyobj = NewPhyobj;
  169. m_platform_list << NewPhyobj;
  170. Ticker::Ref(NewPhyobj);
  171. NewRotation = quat::fromeuler_xyz(0.f, 0.f, radians(90.f));
  172. NewPosition = pos_offset + vec3(-20.0f, -25.0f, 5.0f);
  173. NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  174. NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, true);
  175. m_platform_list << NewPhyobj;
  176. Ticker::Ref(NewPhyobj);
  177. //NewPosition += vec3(-0.0f, .0f, .0f);
  178. //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  179. //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, false);
  180. //m_platform_list << NewPhyobj;
  181. //Ticker::Ref(NewPhyobj);
  182. //NewPosition += vec3(-2.0f, .0f, .0f);
  183. //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  184. //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), false, false);
  185. //m_platform_list << NewPhyobj;
  186. //Ticker::Ref(NewPhyobj);
  187. }
  188. #endif //USE_PLATFORM
  189. #if USE_CHARACTER
  190. {
  191. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  192. vec3 NewPosition = pos_offset + vec3(-5.0f, -10.0f, 15.0f);
  193. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 2);
  194. m_character_list << NewPhyobj;
  195. Ticker::Ref(NewPhyobj);
  196. //NewPhyobj->GetCharacter()->AttachTo(BasePhyobj->GetPhysic(), true, true);
  197. }
  198. #endif //USE_CHARACTER
  199. #if USE_BODIES
  200. {
  201. for (int x=0; x < 6; x++)
  202. {
  203. for (int y=0; y < 2; y++)
  204. {
  205. for (int z=0; z < 5; z++)
  206. {
  207. PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
  208. vec3(-20.f, 15.f, -20.f) +
  209. #if CAT_MODE
  210. vec3(rand(4.f), rand(2.f), rand(4.f)) -
  211. vec3(2.f , 1.f , 2.f) +
  212. #endif //CAT_MODE
  213. vec3(8.f * (float)x, 8.f * (float)y, 8.f * (float)z));
  214. m_physobj_list.push(animated_object { new_physobj, ZERO_TIME });
  215. Ticker::Ref(new_physobj);
  216. }
  217. }
  218. }
  219. }
  220. #endif //USE_BODIES
  221. #if USE_ROPE
  222. {
  223. array<PhysicsObject*> RopeElements;
  224. for (int i = 0; i < 14; i++)
  225. {
  226. PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
  227. vec3(0.f, 15.f, -20.f) +
  228. vec3(0.f, 0.f, 2.f * (float)i), 1);
  229. RopeElements << new_physobj;
  230. m_physobj_list.push(new_physobj, ZERO_TIME);
  231. Ticker::Ref(new_physobj);
  232. if (RopeElements.count() > 1)
  233. {
  234. EasyConstraint* new_constraint = new EasyConstraint();
  235. vec3 A2B = .5f * (RopeElements[i]->GetPhysic()->GetTransform()[3].xyz -
  236. RopeElements[i - 1]->GetPhysic()->GetTransform()[3].xyz);
  237. new_constraint->SetPhysObjA(RopeElements[i - 1]->GetPhysic(), lol::mat4::translate(A2B));
  238. new_constraint->SetPhysObjB(RopeElements[i]->GetPhysic(), lol::mat4::translate(-A2B));
  239. new_constraint->InitConstraintToPoint2Point();
  240. new_constraint->DisableCollisionBetweenObjs(true);
  241. new_constraint->AddToSimulation(m_simulation);
  242. m_constraint_list << new_constraint;
  243. }
  244. }
  245. }
  246. #endif //USE_ROPE
  247. }
  248. BtPhysTest::~BtPhysTest()
  249. {
  250. Scene& scene = Scene::GetScene();
  251. scene.PopCamera(m_camera);
  252. Ticker::Unref(m_light1);
  253. Ticker::Unref(m_light2);
  254. #if CAT_MODE
  255. /* cat datas setup */
  256. Shader::Destroy(m_cat_shader);
  257. Tiler::Deregister(m_cat_texture);
  258. #endif //CAT_MODE
  259. while (m_constraint_list.count())
  260. {
  261. EasyConstraint* CurPop = m_constraint_list.last();
  262. m_constraint_list.pop();
  263. CurPop->RemoveFromSimulation(m_simulation);
  264. delete CurPop;
  265. }
  266. array<PhysicsObject*> objects
  267. = m_ground_list
  268. + m_stairs_list
  269. + m_character_list
  270. + m_platform_list;
  271. while (m_physobj_list.count())
  272. {
  273. objects << m_physobj_list.last().obj;
  274. m_physobj_list.pop();
  275. }
  276. m_ground_list.clear();
  277. m_stairs_list.clear();
  278. m_character_list.clear();
  279. m_platform_list.clear();
  280. while (objects.count())
  281. {
  282. PhysicsObject* CurPop = objects.pop();
  283. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  284. Ticker::Unref(CurPop);
  285. }
  286. //while (m_ground_list.count())
  287. //{
  288. // PhysicsObject* CurPop = m_ground_list.last();
  289. // m_ground_list.pop();
  290. // CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  291. // Ticker::Unref(CurPop);
  292. //}
  293. //while (m_stairs_list.count())
  294. //{
  295. // PhysicsObject* CurPop = m_stairs_list.last();
  296. // m_stairs_list.pop();
  297. // CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  298. // Ticker::Unref(CurPop);
  299. //}
  300. //while (m_character_list.count())
  301. //{
  302. // PhysicsObject* CurPop = m_character_list.last();
  303. // m_character_list.pop();
  304. // CurPop->GetCharacter()->RemoveFromSimulation(m_simulation);
  305. // Ticker::Unref(CurPop);
  306. //}
  307. //while (m_platform_list.count())
  308. //{
  309. // PhysicsObject* CurPop = m_platform_list.last();
  310. // m_platform_list.pop();
  311. // CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  312. // Ticker::Unref(CurPop);
  313. //}
  314. //while (m_physobj_list.count())
  315. //{
  316. // PhysicsObject* CurPop = m_physobj_list.last().obj;
  317. // m_physobj_list.pop();
  318. // CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  319. // Ticker::Unref(CurPop);
  320. //}
  321. Ticker::Unref(m_simulation);
  322. }
  323. void BtPhysTest::tick_game(float seconds)
  324. {
  325. WorldEntity::tick_game(seconds);
  326. if (!m_init_status)
  327. {
  328. if (Scene::GetCount())
  329. InitApp();
  330. return;
  331. }
  332. else if (m_init_status == 1)
  333. {
  334. m_init_status++;
  335. return;
  336. }
  337. auto context = Debug::DrawContext::New(color::white, 1.f);
  338. Debug::DrawGrid(vec3::zero, vec3::axis_x, vec3::axis_y, vec3::axis_z, 10.f);
  339. if (input::keyboard()->key_released(input::key::SC_Escape))
  340. Ticker::Shutdown();
  341. m_loop_value += seconds;
  342. if (m_loop_value > F_PI * 2.0f)
  343. m_loop_value -= F_PI * 2.0f;
  344. vec3 GroundBarycenter = vec3(.0f);
  345. vec3 PhysObjBarycenter = vec3(.0f);
  346. float factor = .0f;
  347. #if CAT_MODE
  348. #if USE_BODIES
  349. vec3 cam_center(0.f);
  350. float cam_factor = .0f;
  351. vec2 screen_min_max[2] = { vec2(FLT_MAX), vec2(-FLT_MAX) };
  352. Scene& scene = Scene::GetScene();
  353. mat4 world_cam = scene.GetCamera()->GetView();
  354. mat4 cam_screen = scene.GetCamera()->GetProjection();
  355. m_target_timer -= seconds;
  356. if (m_target_timer < .0f)
  357. {
  358. m_target_timer = TARGET_TIMER;
  359. if (m_cam_target == -1)
  360. m_cam_target = rand(m_physobj_list.count());
  361. else
  362. m_cam_target = -1;
  363. }
  364. for (int i = 0; i < m_physobj_list.count(); i++)
  365. {
  366. PhysicsObject* PhysObj = m_physobj_list[i].obj;
  367. float &obj_timer = m_physobj_list[i].anim;
  368. vec3 obj_loc = PhysObj->GetPhysic()->GetTransform()[3].xyz;
  369. if (m_cam_target == -1 || m_cam_target == i)
  370. {
  371. cam_center += obj_loc;
  372. cam_factor += 1.f;
  373. mat4 LocalPos = mat4::translate(obj_loc);
  374. vec3 vpos;
  375. LocalPos = world_cam * LocalPos;
  376. mat4 LocalPos0 = LocalPos;
  377. int j = 2;
  378. while (j-- > 0)
  379. {
  380. if (j == 1)
  381. LocalPos = mat4::translate(vec3(-4.f)) * LocalPos0;
  382. else
  383. LocalPos = mat4::translate(vec3(4.f)) * LocalPos0;
  384. LocalPos = cam_screen * LocalPos;
  385. vpos = (LocalPos[3] / LocalPos[3].w).xyz;
  386. screen_min_max[0] = min(vpos.xy, screen_min_max[0]);
  387. screen_min_max[1] = max(vpos.xy, screen_min_max[1]);
  388. }
  389. }
  390. //Jump handling
  391. //if (length(PhysObj->GetPhysic()->GetLinearVelocity()) < ZERO_SPEED)
  392. if (lol::fabs(PhysObj->GetPhysic()->GetLinearVelocity().y) < ZERO_SPEED)
  393. obj_timer -= seconds;
  394. if (obj_timer < .0f)
  395. {
  396. PhysObj->GetPhysic()->AddImpulse(JUMP_HEIGHT *
  397. vec3(JUMP_STRAFE, 1.f, JUMP_STRAFE) *
  398. vec3(rand(-1.f, 1.f), 1.0f, rand(-1.f, 1.f)) *
  399. PhysObj->GetPhysic()->GetMass());
  400. obj_timer = ZERO_TIME;
  401. }
  402. }
  403. float fov_ratio = max(max(lol::fabs(screen_min_max[0].x), lol::fabs(screen_min_max[0].y)),
  404. max(lol::fabs(screen_min_max[1].x), lol::fabs(screen_min_max[1].y)));
  405. vec3 new_target = cam_center / cam_factor;
  406. float fov_dp = .0f;
  407. float loc_dp = .0f;
  408. //ideally fov is on the target
  409. if (lol::fabs(fov_ratio - 1.f) < .2f)
  410. fov_dp = ((m_cam_target == -1)?(.7f):(.2f));
  411. else
  412. fov_dp = ((m_cam_target == -1)?(1.7f):(.9f));
  413. //ideally loc is on the target
  414. if (length(new_target - m_camera->GetTarget()) < 6.f)
  415. loc_dp = ((m_cam_target == -1)?(.5f):(.03f));
  416. else
  417. loc_dp = ((m_cam_target == -1)?(.9f):(.5f));
  418. m_fov_dp = damp(m_fov_dp, fov_dp, 0.08f, seconds);
  419. m_loc_dp = damp(m_loc_dp, loc_dp, 0.08f, seconds);
  420. m_camera->SetFov(damp(m_camera->GetFov(), m_camera->GetFov() * fov_ratio * 1.1f, m_fov_dp, seconds));
  421. vec3 tmp = damp(m_camera->GetTarget(), new_target, m_loc_dp, seconds);
  422. m_camera->SetView((mat4::rotate(radians(10.f) * seconds, vec3(.0f, 1.f, .0f)) * vec4(m_camera->GetPosition(), 1.0f)).xyz,
  423. tmp, vec3(0, 1, 0));
  424. #endif //USE_BODIES
  425. #endif //CAT_MODE
  426. #if USE_WALL
  427. {
  428. for (int i = 0; i < m_ground_list.count(); i++)
  429. {
  430. PhysicsObject* PhysObj = m_ground_list[i];
  431. mat4 GroundMat = PhysObj->GetTransform();
  432. GroundBarycenter += GroundMat[3].xyz;
  433. factor += 1.f;
  434. }
  435. GroundBarycenter /= factor;
  436. for (int i = 0; i < m_ground_list.count(); i++)
  437. {
  438. PhysicsObject* PhysObj = m_ground_list[i];
  439. mat4 GroundMat = PhysObj->GetTransform();
  440. vec3 CenterToGround = GroundMat[3].xyz - GroundBarycenter;
  441. vec3 CenterToCam = m_camera->GetPosition() - GroundBarycenter;
  442. if (dot(normalize(CenterToCam - CenterToGround),
  443. normalize(CenterToGround)) > 0.f)
  444. PhysObj->SetRender(false);
  445. else
  446. PhysObj->SetRender(true);
  447. }
  448. }
  449. #endif //USE_WALL
  450. #if USE_ROTATION
  451. {
  452. for (int i = 0; i < m_ground_list.count(); i++)
  453. {
  454. PhysicsObject* PhysObj = m_ground_list[i];
  455. mat4 GroundMat = PhysObj->GetTransform();
  456. mat4 CenterMx = mat4::translate(GroundBarycenter);
  457. GroundMat = inverse(CenterMx) * GroundMat;
  458. GroundMat = CenterMx *
  459. mat4(quat::fromeuler_xyz(vec3(.0f, radians(20.f), radians(20.0f)) * seconds))
  460. * GroundMat;
  461. PhysObj->SetTransform(GroundMat[3].xyz, quat(mat3(GroundMat)));
  462. }
  463. }
  464. #endif //USE_ROTATION
  465. #if USE_PLATFORM
  466. {
  467. for (int i = 0; i < m_platform_list.count(); i++)
  468. {
  469. PhysicsObject* PhysObj = m_platform_list[i];
  470. mat4 GroundMat = PhysObj->GetTransform();
  471. if (i == 0)
  472. {
  473. GroundMat = GroundMat * mat4(quat::fromeuler_xyz(vec3(radians(20.f), .0f, .0f) * seconds));
  474. PhysObj->SetTransform(GroundMat[3].xyz, quat(mat3(GroundMat)));
  475. }
  476. else if (i == 1)
  477. {
  478. GroundMat =
  479. mat4::translate(vec3(-15.0f, 5.0f, lol::cos(m_loop_value) * 8.f)) *
  480. mat4(quat::fromeuler_xyz(vec3(.0f, lol::cos(m_loop_value) * radians(20.f), .0f)));
  481. PhysObj->SetTransform(GroundMat[3].xyz, quat(mat3(GroundMat)));
  482. }
  483. }
  484. }
  485. #endif //USE_PLATFORM
  486. #if USE_CHARACTER
  487. {
  488. auto keyboard = input::keyboard();
  489. for (int i = 0; i < m_character_list.count(); i++)
  490. {
  491. PhysicsObject* PhysObj = m_character_list[i];
  492. EasyCharacterController* Character = (EasyCharacterController*)PhysObj->GetCharacter();
  493. mat4 CtlrMx = Character->GetTransform();
  494. vec3 movement(0.f);
  495. movement.z = (keyboard->key(input::key::SC_Right) ? 1.f : 0.f)
  496. - (keyboard->key(input::key::SC_Left) ? 1.f : 0.f);
  497. movement.x = (keyboard->key(input::key::SC_Up) ? 1.f : 0.f)
  498. - (keyboard->key(input::key::SC_Down) ? 1.f : 0.f);
  499. movement.y = (keyboard->key(input::key::SC_PageUp) ? 1.f : 0.f)
  500. - (keyboard->key(input::key::SC_PageDown) ? 1.f : 0.f);
  501. vec3 CharMove = movement * seconds * vec3(4.f, 10.f, 4.f);
  502. if (input::keyboard()->key_released(input::key::SC_Space))
  503. Character->Jump();
  504. Character->SetMovementForFrame(CharMove);
  505. RayCastResult HitResult;
  506. if (m_simulation->RayHits(HitResult, ERT_Closest, Character->GetTransform()[3].xyz, (Character->GetTransform()[3].xyz + vec3(.0f, -1.f, .0f)), Character))
  507. Character->AttachTo(HitResult.m_collider_list[0], true, true);
  508. else
  509. Character->AttachTo(nullptr);
  510. }
  511. }
  512. #endif //USE_CHARACTER
  513. #if USE_CHARACTER
  514. {
  515. PhysObjBarycenter = vec3(.0f);
  516. factor = .0f;
  517. for (int i = 0; i < m_character_list.count(); i++)
  518. {
  519. PhysicsObject* PhysObj = m_character_list[i];
  520. mat4 GroundMat = PhysObj->GetTransform();
  521. PhysObjBarycenter += GroundMat[3].xyz;
  522. factor += 1.f;
  523. }
  524. PhysObjBarycenter /= factor;
  525. #if 0
  526. m_camera->SetTarget(m_camera->GetTarget() + (seconds / (seconds + 0.18f)) * (PhysObjBarycenter - m_camera->GetTarget()));
  527. vec3 CamPosCenter = m_camera->GetTarget() + vec3(.0f, 5.0f, .0f);
  528. m_camera->SetPosition(CamPosCenter + normalize(m_camera->GetPosition() - CamPosCenter) * 20.0f);
  529. #endif
  530. }
  531. #else
  532. {
  533. PhysObjBarycenter = vec3(.0f);
  534. for (int i = 0; i < m_physobj_list.count(); i++)
  535. {
  536. PhysicsObject* PhysObj = m_physobj_list[i].obj;
  537. mat4 GroundMat = PhysObj->GetTransform();
  538. PhysObjBarycenter += GroundMat[3].xyz;
  539. factor += 1.f;
  540. }
  541. PhysObjBarycenter /= factor;
  542. #if 0
  543. m_camera->SetTarget(PhysObjBarycenter);
  544. m_camera->SetPosition(GroundBarycenter + normalize(GroundBarycenter - PhysObjBarycenter) * 60.0f);
  545. #endif
  546. }
  547. #endif //USE_CHARACTER
  548. }
  549. void BtPhysTest::tick_draw(float seconds, Scene &scene)
  550. {
  551. WorldEntity::tick_draw(seconds, scene);
  552. if (m_init_status != 2)
  553. return;
  554. if (!m_ready)
  555. {
  556. #if CAT_MODE
  557. /* cat datas setup */
  558. m_cat_shader = Shader::Create(LOLFX_RESOURCE_NAME(front_camera_sprite));
  559. #if USE_BODIES
  560. for (int i = 0; i < m_physobj_list.count(); i++)
  561. {
  562. PhysicsObject* PhysObj = m_physobj_list[i].obj;
  563. m_cat_sdata = new CatShaderData(((1 << VertexUsage::Position) |
  564. (1 << VertexUsage::Color) |
  565. (1 << VertexUsage::TexCoord) |
  566. (1 << VertexUsage::TexCoordExt)),
  567. m_cat_shader);
  568. m_cat_sdata->m_tex_uniform = m_cat_texture->GetTexture()->GetTextureUniform();
  569. m_cat_sdata->m_sprite_flip = ((rand(2) == 1)?(1.f):(0.f)) / (float)(NB_SPRITE * PARTICLE_SIZE);
  570. PhysObj->SetCustomShaderData(m_cat_sdata);
  571. m_cat_sdata = nullptr;
  572. }
  573. #endif //USE_BODIES
  574. #endif //CAT_MODE
  575. /* FIXME: this object never cleans up */
  576. m_ready = true;
  577. }
  578. else
  579. {
  580. #if CAT_MODE
  581. for (int i = 0; i < m_physobj_list.count(); i++)
  582. {
  583. PhysicsObject* PhysObj = m_physobj_list[i].obj;
  584. CatShaderData* ShaderData = (CatShaderData*)PhysObj->GetCustomShaderData();
  585. ShaderData->m_sprite_orientation = damp(ShaderData->m_sprite_orientation,
  586. F_PI_4 * ((ShaderData->m_sprite_flip * 2.f * (float)(NB_SPRITE * PARTICLE_SIZE)) - 1.f) *
  587. clamp(PhysObj->GetPhysic()->GetLinearVelocity().y / 20.0f, -1.f, 1.f),
  588. 0.1f, seconds);
  589. }
  590. #endif //CAT_MODE
  591. }
  592. //Video::set_clear_color(vec4(0.0f, 0.0f, 0.12f, 1.0f));
  593. }
  594. //-----------------------------------------------------------------------------
  595. // CShaderData
  596. //-----------------------------------------------------------------------------
  597. CatShaderData::CatShaderData(uint32_t vert_decl_flags, std::shared_ptr<Shader> shader)
  598. : GpuShaderData(vert_decl_flags, shader, DebugRenderMode::Default)
  599. {
  600. m_sprite_orientation = .0f;
  601. m_sprite_flip = .0f;
  602. SetupDefaultData();
  603. }
  604. //-----------------------------------------------------------------------------
  605. void CatShaderData::SetupDefaultData()
  606. {
  607. AddUniform("u_model_view");
  608. AddUniform("u_normal_mat");
  609. AddUniform("u_proj");
  610. AddUniform("u_texture");
  611. AddUniform("u_sprite_orientation");
  612. AddUniform("u_sprite_flip");
  613. }
  614. //-----------------------------------------------------------------------------
  615. void CatShaderData::SetupShaderDatas(mat4 const &model)
  616. {
  617. Scene& scene = Scene::GetScene();
  618. mat4 proj = scene.GetCamera()->GetProjection();
  619. mat4 view = scene.GetCamera()->GetView();
  620. mat4 modelview = view * model;
  621. mat3 normalmat = transpose(inverse(mat3(view)));
  622. m_shader->SetUniform(*GetUniform("u_model_view"), modelview);
  623. m_shader->SetUniform(*GetUniform("u_normal_mat"), normalmat);
  624. m_shader->SetUniform(*GetUniform("u_proj"), proj);
  625. m_shader->SetUniform(*GetUniform("u_texture"), m_tex_uniform, 0);
  626. m_shader->SetUniform(*GetUniform("u_sprite_orientation"), m_sprite_orientation);
  627. m_shader->SetUniform(*GetUniform("u_sprite_flip"), m_sprite_flip);
  628. }
  629. int main(int argc, char **argv)
  630. {
  631. sys::init(argc, argv);
  632. Application app("BtPhysTest", ivec2(1280, 960), 60.0f);
  633. new BtPhysTest(argc > 1);
  634. app.ShowPointer(false);
  635. app.Run();
  636. return EXIT_SUCCESS;
  637. }