Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

759 lignes
24 KiB

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