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.

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