Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

5 лет назад
11 лет назад
11 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
11 лет назад
11 лет назад
11 лет назад
5 лет назад
5 лет назад
5 лет назад
11 лет назад
5 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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.push_back(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.push_back(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.push_back(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.push_back(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.push_back(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.push_back(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.push_back(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.push_back(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.push_back(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.push_back(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_back(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. std::vector<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.push_back(new_physobj);
  230. m_physobj_list.push_back(animated_object { new_physobj, ZERO_TIME });
  231. Ticker::Ref(new_physobj);
  232. if (RopeElements.size() > 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.push_back(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.size())
  260. {
  261. EasyConstraint* CurPop = m_constraint_list.back();
  262. m_constraint_list.pop_back();
  263. CurPop->RemoveFromSimulation(m_simulation);
  264. delete CurPop;
  265. }
  266. std::vector<PhysicsObject*> objects;
  267. for (auto *obj : m_ground_list)
  268. objects.push_back(obj);
  269. for (auto *obj : m_stairs_list)
  270. objects.push_back(obj);
  271. for (auto *obj : m_character_list)
  272. objects.push_back(obj);
  273. for (auto *obj : m_platform_list)
  274. objects.push_back(obj);
  275. // Back insertion; is this really necessary?
  276. while (m_physobj_list.size())
  277. {
  278. objects.push_back(m_physobj_list.back().obj);
  279. m_physobj_list.pop_back();
  280. }
  281. m_ground_list.clear();
  282. m_stairs_list.clear();
  283. m_character_list.clear();
  284. m_platform_list.clear();
  285. while (objects.size())
  286. {
  287. PhysicsObject* CurPop = objects.back();
  288. objects.pop_back();
  289. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  290. Ticker::Unref(CurPop);
  291. }
  292. //while (m_ground_list.size())
  293. //{
  294. // PhysicsObject* CurPop = m_ground_list.back();
  295. // m_ground_list.pop_back();
  296. // CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  297. // Ticker::Unref(CurPop);
  298. //}
  299. //while (m_stairs_list.size())
  300. //{
  301. // PhysicsObject* CurPop = m_stairs_list.back();
  302. // m_stairs_list.pop_back();
  303. // CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  304. // Ticker::Unref(CurPop);
  305. //}
  306. //while (m_character_list.size())
  307. //{
  308. // PhysicsObject* CurPop = m_character_list.back();
  309. // m_character_list.pop_back();
  310. // CurPop->GetCharacter()->RemoveFromSimulation(m_simulation);
  311. // Ticker::Unref(CurPop);
  312. //}
  313. //while (m_platform_list.size())
  314. //{
  315. // PhysicsObject* CurPop = m_platform_list.back();
  316. // m_platform_list.pop_back();
  317. // CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  318. // Ticker::Unref(CurPop);
  319. //}
  320. //while (m_physobj_list.size())
  321. //{
  322. // PhysicsObject* CurPop = m_physobj_list.back().obj;
  323. // m_physobj_list.pop_back();
  324. // CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  325. // Ticker::Unref(CurPop);
  326. //}
  327. Ticker::Unref(m_simulation);
  328. }
  329. void BtPhysTest::tick_game(float seconds)
  330. {
  331. WorldEntity::tick_game(seconds);
  332. if (!m_init_status)
  333. {
  334. if (Scene::GetCount())
  335. InitApp();
  336. return;
  337. }
  338. else if (m_init_status == 1)
  339. {
  340. m_init_status++;
  341. return;
  342. }
  343. auto context = Debug::DrawContext::New(color::white, 1.f);
  344. Debug::DrawGrid(vec3::zero, vec3::axis_x, vec3::axis_y, vec3::axis_z, 10.f);
  345. if (input::keyboard()->key_released(input::key::SC_Escape))
  346. Ticker::Shutdown();
  347. m_loop_value += seconds;
  348. if (m_loop_value > F_PI * 2.0f)
  349. m_loop_value -= F_PI * 2.0f;
  350. vec3 GroundBarycenter = vec3(.0f);
  351. vec3 PhysObjBarycenter = vec3(.0f);
  352. float factor = .0f;
  353. #if CAT_MODE
  354. #if USE_BODIES
  355. vec3 cam_center(0.f);
  356. float cam_factor = .0f;
  357. vec2 screen_min_max[2] = { vec2(FLT_MAX), vec2(-FLT_MAX) };
  358. Scene& scene = Scene::GetScene();
  359. mat4 world_cam = scene.GetCamera()->GetView();
  360. mat4 cam_screen = scene.GetCamera()->GetProjection();
  361. m_target_timer -= seconds;
  362. if (m_target_timer < .0f)
  363. {
  364. m_target_timer = TARGET_TIMER;
  365. if (m_cam_target == -1)
  366. m_cam_target = rand(m_physobj_list.size());
  367. else
  368. m_cam_target = -1;
  369. }
  370. for (size_t i = 0; i < m_physobj_list.size(); i++)
  371. {
  372. PhysicsObject* PhysObj = m_physobj_list[i].obj;
  373. float &obj_timer = m_physobj_list[i].anim;
  374. vec3 obj_loc = PhysObj->GetPhysic()->GetTransform()[3].xyz;
  375. if (m_cam_target == -1 || m_cam_target == i)
  376. {
  377. cam_center += obj_loc;
  378. cam_factor += 1.f;
  379. mat4 LocalPos = mat4::translate(obj_loc);
  380. vec3 vpos;
  381. LocalPos = world_cam * LocalPos;
  382. mat4 LocalPos0 = LocalPos;
  383. int j = 2;
  384. while (j-- > 0)
  385. {
  386. if (j == 1)
  387. LocalPos = mat4::translate(vec3(-4.f)) * LocalPos0;
  388. else
  389. LocalPos = mat4::translate(vec3(4.f)) * LocalPos0;
  390. LocalPos = cam_screen * LocalPos;
  391. vpos = (LocalPos[3] / LocalPos[3].w).xyz;
  392. screen_min_max[0] = min(vpos.xy, screen_min_max[0]);
  393. screen_min_max[1] = max(vpos.xy, screen_min_max[1]);
  394. }
  395. }
  396. //Jump handling
  397. //if (length(PhysObj->GetPhysic()->GetLinearVelocity()) < ZERO_SPEED)
  398. if (lol::fabs(PhysObj->GetPhysic()->GetLinearVelocity().y) < ZERO_SPEED)
  399. obj_timer -= seconds;
  400. if (obj_timer < .0f)
  401. {
  402. PhysObj->GetPhysic()->AddImpulse(JUMP_HEIGHT *
  403. vec3(JUMP_STRAFE, 1.f, JUMP_STRAFE) *
  404. vec3(rand(-1.f, 1.f), 1.0f, rand(-1.f, 1.f)) *
  405. PhysObj->GetPhysic()->GetMass());
  406. obj_timer = ZERO_TIME;
  407. }
  408. }
  409. float fov_ratio = max(max(lol::fabs(screen_min_max[0].x), lol::fabs(screen_min_max[0].y)),
  410. max(lol::fabs(screen_min_max[1].x), lol::fabs(screen_min_max[1].y)));
  411. vec3 new_target = cam_center / cam_factor;
  412. float fov_dp = .0f;
  413. float loc_dp = .0f;
  414. //ideally fov is on the target
  415. if (lol::fabs(fov_ratio - 1.f) < .2f)
  416. fov_dp = ((m_cam_target == -1)?(.7f):(.2f));
  417. else
  418. fov_dp = ((m_cam_target == -1)?(1.7f):(.9f));
  419. //ideally loc is on the target
  420. if (length(new_target - m_camera->GetTarget()) < 6.f)
  421. loc_dp = ((m_cam_target == -1)?(.5f):(.03f));
  422. else
  423. loc_dp = ((m_cam_target == -1)?(.9f):(.5f));
  424. m_fov_dp = damp(m_fov_dp, fov_dp, 0.08f, seconds);
  425. m_loc_dp = damp(m_loc_dp, loc_dp, 0.08f, seconds);
  426. m_camera->SetFov(damp(m_camera->GetFov(), m_camera->GetFov() * fov_ratio * 1.1f, m_fov_dp, seconds));
  427. vec3 tmp = damp(m_camera->GetTarget(), new_target, m_loc_dp, seconds);
  428. m_camera->SetView((mat4::rotate(radians(10.f) * seconds, vec3(.0f, 1.f, .0f)) * vec4(m_camera->GetPosition(), 1.0f)).xyz,
  429. tmp, vec3(0, 1, 0));
  430. #endif //USE_BODIES
  431. #endif //CAT_MODE
  432. #if USE_WALL
  433. {
  434. for (size_t i = 0; i < m_ground_list.size(); i++)
  435. {
  436. PhysicsObject* PhysObj = m_ground_list[i];
  437. mat4 GroundMat = PhysObj->GetTransform();
  438. GroundBarycenter += GroundMat[3].xyz;
  439. factor += 1.f;
  440. }
  441. GroundBarycenter /= factor;
  442. for (size_t i = 0; i < m_ground_list.size(); i++)
  443. {
  444. PhysicsObject* PhysObj = m_ground_list[i];
  445. mat4 GroundMat = PhysObj->GetTransform();
  446. vec3 CenterToGround = GroundMat[3].xyz - GroundBarycenter;
  447. vec3 CenterToCam = m_camera->GetPosition() - GroundBarycenter;
  448. if (dot(normalize(CenterToCam - CenterToGround),
  449. normalize(CenterToGround)) > 0.f)
  450. PhysObj->SetRender(false);
  451. else
  452. PhysObj->SetRender(true);
  453. }
  454. }
  455. #endif //USE_WALL
  456. #if USE_ROTATION
  457. {
  458. for (size_t i = 0; i < m_ground_list.size(); i++)
  459. {
  460. PhysicsObject* PhysObj = m_ground_list[i];
  461. mat4 GroundMat = PhysObj->GetTransform();
  462. mat4 CenterMx = mat4::translate(GroundBarycenter);
  463. GroundMat = inverse(CenterMx) * GroundMat;
  464. GroundMat = CenterMx *
  465. mat4(quat::fromeuler_xyz(vec3(.0f, radians(20.f), radians(20.0f)) * seconds))
  466. * GroundMat;
  467. PhysObj->SetTransform(GroundMat[3].xyz, quat(mat3(GroundMat)));
  468. }
  469. }
  470. #endif //USE_ROTATION
  471. #if USE_PLATFORM
  472. {
  473. for (size_t i = 0; i < m_platform_list.size(); i++)
  474. {
  475. PhysicsObject* PhysObj = m_platform_list[i];
  476. mat4 GroundMat = PhysObj->GetTransform();
  477. if (i == 0)
  478. {
  479. GroundMat = GroundMat * mat4(quat::fromeuler_xyz(vec3(radians(20.f), .0f, .0f) * seconds));
  480. PhysObj->SetTransform(GroundMat[3].xyz, quat(mat3(GroundMat)));
  481. }
  482. else if (i == 1)
  483. {
  484. GroundMat =
  485. mat4::translate(vec3(-15.0f, 5.0f, lol::cos(m_loop_value) * 8.f)) *
  486. mat4(quat::fromeuler_xyz(vec3(.0f, lol::cos(m_loop_value) * radians(20.f), .0f)));
  487. PhysObj->SetTransform(GroundMat[3].xyz, quat(mat3(GroundMat)));
  488. }
  489. }
  490. }
  491. #endif //USE_PLATFORM
  492. #if USE_CHARACTER
  493. {
  494. auto keyboard = input::keyboard();
  495. for (size_t i = 0; i < m_character_list.size(); i++)
  496. {
  497. PhysicsObject* PhysObj = m_character_list[i];
  498. EasyCharacterController* Character = (EasyCharacterController*)PhysObj->GetCharacter();
  499. mat4 CtlrMx = Character->GetTransform();
  500. vec3 movement(0.f);
  501. movement.z = (keyboard->key(input::key::SC_Right) ? 1.f : 0.f)
  502. - (keyboard->key(input::key::SC_Left) ? 1.f : 0.f);
  503. movement.x = (keyboard->key(input::key::SC_Up) ? 1.f : 0.f)
  504. - (keyboard->key(input::key::SC_Down) ? 1.f : 0.f);
  505. movement.y = (keyboard->key(input::key::SC_PageUp) ? 1.f : 0.f)
  506. - (keyboard->key(input::key::SC_PageDown) ? 1.f : 0.f);
  507. vec3 CharMove = movement * seconds * vec3(4.f, 10.f, 4.f);
  508. if (input::keyboard()->key_released(input::key::SC_Space))
  509. Character->Jump();
  510. Character->SetMovementForFrame(CharMove);
  511. RayCastResult HitResult;
  512. if (m_simulation->RayHits(HitResult, ERT_Closest, Character->GetTransform()[3].xyz, (Character->GetTransform()[3].xyz + vec3(.0f, -1.f, .0f)), Character))
  513. Character->AttachTo(HitResult.m_collider_list[0], true, true);
  514. else
  515. Character->AttachTo(nullptr);
  516. }
  517. }
  518. #endif //USE_CHARACTER
  519. #if USE_CHARACTER
  520. {
  521. PhysObjBarycenter = vec3(.0f);
  522. factor = .0f;
  523. for (size_t i = 0; i < m_character_list.size(); i++)
  524. {
  525. PhysicsObject* PhysObj = m_character_list[i];
  526. mat4 GroundMat = PhysObj->GetTransform();
  527. PhysObjBarycenter += GroundMat[3].xyz;
  528. factor += 1.f;
  529. }
  530. PhysObjBarycenter /= factor;
  531. #if 0
  532. m_camera->SetTarget(m_camera->GetTarget() + (seconds / (seconds + 0.18f)) * (PhysObjBarycenter - m_camera->GetTarget()));
  533. vec3 CamPosCenter = m_camera->GetTarget() + vec3(.0f, 5.0f, .0f);
  534. m_camera->SetPosition(CamPosCenter + normalize(m_camera->GetPosition() - CamPosCenter) * 20.0f);
  535. #endif
  536. }
  537. #else
  538. {
  539. PhysObjBarycenter = vec3(.0f);
  540. for (size_t i = 0; i < m_physobj_list.size(); i++)
  541. {
  542. PhysicsObject* PhysObj = m_physobj_list[i].obj;
  543. mat4 GroundMat = PhysObj->GetTransform();
  544. PhysObjBarycenter += GroundMat[3].xyz;
  545. factor += 1.f;
  546. }
  547. PhysObjBarycenter /= factor;
  548. #if 0
  549. m_camera->SetTarget(PhysObjBarycenter);
  550. m_camera->SetPosition(GroundBarycenter + normalize(GroundBarycenter - PhysObjBarycenter) * 60.0f);
  551. #endif
  552. }
  553. #endif //USE_CHARACTER
  554. }
  555. void BtPhysTest::tick_draw(float seconds, Scene &scene)
  556. {
  557. WorldEntity::tick_draw(seconds, scene);
  558. if (m_init_status != 2)
  559. return;
  560. if (!m_ready)
  561. {
  562. #if CAT_MODE
  563. /* cat datas setup */
  564. m_cat_shader = Shader::Create(LOLFX_RESOURCE_NAME(front_camera_sprite));
  565. #if USE_BODIES
  566. for (size_t i = 0; i < m_physobj_list.size(); i++)
  567. {
  568. PhysicsObject* PhysObj = m_physobj_list[i].obj;
  569. m_cat_sdata = new CatShaderData(((1 << VertexUsage::Position) |
  570. (1 << VertexUsage::Color) |
  571. (1 << VertexUsage::TexCoord) |
  572. (1 << VertexUsage::TexCoordExt)),
  573. m_cat_shader);
  574. m_cat_sdata->m_tex_uniform = m_cat_texture->GetTexture()->GetTextureUniform();
  575. m_cat_sdata->m_sprite_flip = ((rand(2) == 1)?(1.f):(0.f)) / (float)(NB_SPRITE * PARTICLE_SIZE);
  576. PhysObj->SetCustomShaderData(m_cat_sdata);
  577. m_cat_sdata = nullptr;
  578. }
  579. #endif //USE_BODIES
  580. #endif //CAT_MODE
  581. /* FIXME: this object never cleans up */
  582. m_ready = true;
  583. }
  584. else
  585. {
  586. #if CAT_MODE
  587. for (size_t i = 0; i < m_physobj_list.size(); i++)
  588. {
  589. PhysicsObject* PhysObj = m_physobj_list[i].obj;
  590. CatShaderData* ShaderData = (CatShaderData*)PhysObj->GetCustomShaderData();
  591. ShaderData->m_sprite_orientation = damp(ShaderData->m_sprite_orientation,
  592. F_PI_4 * ((ShaderData->m_sprite_flip * 2.f * (float)(NB_SPRITE * PARTICLE_SIZE)) - 1.f) *
  593. clamp(PhysObj->GetPhysic()->GetLinearVelocity().y / 20.0f, -1.f, 1.f),
  594. 0.1f, seconds);
  595. }
  596. #endif //CAT_MODE
  597. }
  598. //Video::set_clear_color(vec4(0.0f, 0.0f, 0.12f, 1.0f));
  599. }
  600. //-----------------------------------------------------------------------------
  601. // CShaderData
  602. //-----------------------------------------------------------------------------
  603. CatShaderData::CatShaderData(uint32_t vert_decl_flags, std::shared_ptr<Shader> shader)
  604. : GpuShaderData(vert_decl_flags, shader, DebugRenderMode::Default)
  605. {
  606. m_sprite_orientation = .0f;
  607. m_sprite_flip = .0f;
  608. SetupDefaultData();
  609. }
  610. //-----------------------------------------------------------------------------
  611. void CatShaderData::SetupDefaultData()
  612. {
  613. AddUniform("u_model_view");
  614. AddUniform("u_normal_mat");
  615. AddUniform("u_proj");
  616. AddUniform("u_texture");
  617. AddUniform("u_sprite_orientation");
  618. AddUniform("u_sprite_flip");
  619. }
  620. //-----------------------------------------------------------------------------
  621. void CatShaderData::SetupShaderDatas(mat4 const &model)
  622. {
  623. Scene& scene = Scene::GetScene();
  624. mat4 proj = scene.GetCamera()->GetProjection();
  625. mat4 view = scene.GetCamera()->GetView();
  626. mat4 modelview = view * model;
  627. mat3 normalmat = transpose(inverse(mat3(view)));
  628. m_shader->SetUniform(*GetUniform("u_model_view"), modelview);
  629. m_shader->SetUniform(*GetUniform("u_normal_mat"), normalmat);
  630. m_shader->SetUniform(*GetUniform("u_proj"), proj);
  631. m_shader->SetUniform(*GetUniform("u_texture"), m_tex_uniform, 0);
  632. m_shader->SetUniform(*GetUniform("u_sprite_orientation"), m_sprite_orientation);
  633. m_shader->SetUniform(*GetUniform("u_sprite_flip"), m_sprite_flip);
  634. }
  635. int main(int argc, char **argv)
  636. {
  637. sys::init(argc, argv);
  638. Application app("BtPhysTest", ivec2(1280, 960), 60.0f);
  639. new BtPhysTest(argc > 1);
  640. app.ShowPointer(false);
  641. app.Run();
  642. return EXIT_SUCCESS;
  643. }