Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12 роки тому
12 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. //
  2. // BtPhysTest
  3. //
  4. // Copyright: (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  5. // (c) 2012-2013 Sam Hocevar <sam@hocevar.net>
  6. //
  7. #if defined HAVE_CONFIG_H
  8. # include "config.h"
  9. #endif
  10. #include "core.h"
  11. #include "loldebug.h"
  12. using namespace lol;
  13. #include "physics/lolphysics.h"
  14. #include "physics/easyphysics.h"
  15. #define CAT_MODE 1
  16. #define OBJ_SIZE 2.f
  17. #define NB_SPRITE 4
  18. #define PARTICLE_SIZE 4
  19. #include "physicobject.h"
  20. #include "btphystest.h"
  21. using namespace lol::phys;
  22. #define CUBE_HALF_EXTENTS .5f
  23. #define EXTRA_HEIGHT 1.f
  24. #define BASE_TIME 2.f
  25. #define ZERO_TIME (BASE_TIME + rand(-BASE_TIME * .4f, BASE_TIME * .4f))
  26. #define ZERO_SPEED 3.5f
  27. #define JUMP_HEIGHT 30.f
  28. #define JUMP_STRAFE .5f
  29. #define TARGET_TIMER 10.f + (rand(4.f) - 2.f)
  30. int gNumObjects = 64;
  31. #if CAT_MODE
  32. #define USE_WALL 1
  33. #define USE_BODIES 1
  34. #else
  35. #define USE_WALL 1
  36. #define USE_PLATFORM 1
  37. #define USE_ROPE 0
  38. #define USE_BODIES 1
  39. #define USE_ROTATION 0
  40. #define USE_CHARACTER 0
  41. #define USE_STAIRS 0
  42. #endif
  43. LOLFX_RESOURCE_DECLARE(front_camera_sprite);
  44. BtPhysTest::BtPhysTest(bool editor)
  45. {
  46. m_init_status = 0;
  47. }
  48. void BtPhysTest::InitApp()
  49. {
  50. m_init_status = 1;
  51. m_loop_value = .0f;
  52. #if CAT_MODE
  53. /* cat datas setup */
  54. m_cat_texture = Tiler::Register("data/CatsSheet.png", ivec2::zero, ivec2(0,1));
  55. m_fov_dp = .0f;
  56. m_loc_dp = .0f;
  57. #endif //CAT_MODE
  58. /* Register an input controller for the keyboard */
  59. m_controller = new Controller("Default", KEY_MAX, 0);
  60. m_controller->GetKey(KEY_MOVE_FORWARD).Bind("Keyboard", "Up");
  61. m_controller->GetKey(KEY_MOVE_BACK).Bind("Keyboard", "Down");
  62. m_controller->GetKey(KEY_MOVE_LEFT).Bind("Keyboard", "Left");
  63. m_controller->GetKey(KEY_MOVE_RIGHT).Bind("Keyboard", "Right");
  64. m_controller->GetKey(KEY_MOVE_JUMP).Bind("Keyboard", "Space");
  65. m_controller->GetKey(KEY_MOVE_UP).Bind("Keyboard", "PageUp");
  66. m_controller->GetKey(KEY_MOVE_DOWN).Bind("Keyboard", "PageDown");
  67. m_controller->GetKey(KEY_QUIT).Bind("Keyboard", "Escape");
  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(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(45.f, .1f, 1000.f, (float)Video::GetSize().x, (float)Video::GetSize().y / (float)Video::GetSize().x);
  82. #endif
  83. g_scene->PushCamera(m_camera);
  84. m_ready = false;
  85. m_simulation = new Simulation();
  86. m_simulation->SetWorldLimit(vec3(-1000.0f, -1000.0f, -1000.0f), vec3(1000.0f, 1000.0f, 1000.0f));
  87. m_simulation->Init();
  88. vec3 NewGravity = vec3(.0f, -10.0f, .0f);
  89. m_simulation->SetGravity(NewGravity);
  90. m_simulation->SetContinuousDetection(true);
  91. m_simulation->SetTimestep(1.f / 120.f);
  92. Ticker::Ref(m_simulation);
  93. /* Add a white directional light */
  94. m_light1 = new Light();
  95. m_light1->SetPosition(vec4(0.2f, 0.2f, 0.f, 0.f));
  96. m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
  97. Ticker::Ref(m_light1);
  98. /* Add an orangeish point light */
  99. m_light2 = new Light();
  100. m_light2->SetPosition(vec4(-15.f, 15.f, 15.f, 1.f));
  101. m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
  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, 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, 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(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 = NULL;
  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, 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(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().v3.xyz -
  236. RopeElements[i - 1]->GetPhysic()->GetTransform().v3.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. void BtPhysTest::TickGame(float seconds)
  249. {
  250. WorldEntity::TickGame(seconds);
  251. if (!m_init_status)
  252. {
  253. if (g_renderer)
  254. InitApp();
  255. return;
  256. }
  257. else if (m_init_status == 1)
  258. {
  259. m_init_status++;
  260. return;
  261. }
  262. if (m_controller->GetKey(KEY_QUIT).IsReleased())
  263. Ticker::Shutdown();
  264. m_loop_value += seconds;
  265. if (m_loop_value > F_PI * 2.0f)
  266. m_loop_value -= F_PI * 2.0f;
  267. vec3 GroundBarycenter = vec3(.0f);
  268. vec3 PhysObjBarycenter = vec3(.0f);
  269. float factor = .0f;
  270. #if CAT_MODE
  271. #if USE_BODIES
  272. vec3 cam_center(0.f);
  273. float cam_factor = .0f;
  274. vec2 screen_min_max[2] = { vec2(FLT_MAX), vec2(-FLT_MAX) };
  275. mat4 world_cam = g_scene->GetCamera()->GetView();
  276. mat4 cam_screen = g_scene->GetCamera()->GetProjection();
  277. m_target_timer -= seconds;
  278. if (m_target_timer < .0f)
  279. {
  280. m_target_timer = TARGET_TIMER;
  281. if (m_cam_target == -1)
  282. m_cam_target = rand(m_physobj_list.Count());
  283. else
  284. m_cam_target = -1;
  285. }
  286. for (int i = 0; i < m_physobj_list.Count(); i++)
  287. {
  288. PhysicsObject* PhysObj = m_physobj_list[i].m1;
  289. float &Timer = m_physobj_list[i].m2;
  290. vec3 obj_loc = PhysObj->GetPhysic()->GetTransform().v3.xyz;
  291. if (m_cam_target == -1 || m_cam_target == i)
  292. {
  293. cam_center += obj_loc;
  294. cam_factor += 1.f;
  295. mat4 LocalPos = mat4::translate(obj_loc);
  296. vec3 vpos;
  297. LocalPos = world_cam * LocalPos;
  298. mat4 LocalPos0 = LocalPos;
  299. int j = 2;
  300. while (j-- > 0)
  301. {
  302. if (j == 1)
  303. LocalPos = mat4::translate(vec3(-4.f)) * LocalPos0;
  304. else
  305. LocalPos = mat4::translate(vec3(4.f)) * LocalPos0;
  306. LocalPos = cam_screen * LocalPos;
  307. vpos = (LocalPos.v3 / LocalPos.v3.w).xyz;
  308. screen_min_max[0] = min(vpos.xy, screen_min_max[0]);
  309. screen_min_max[1] = max(vpos.xy, screen_min_max[1]);
  310. }
  311. }
  312. //Jump handling
  313. //if (length(PhysObj->GetPhysic()->GetLinearVelocity()) < ZERO_SPEED)
  314. if (lol::abs(PhysObj->GetPhysic()->GetLinearVelocity().y) < ZERO_SPEED)
  315. Timer -= seconds;
  316. if (Timer < .0f)
  317. {
  318. PhysObj->GetPhysic()->AddImpulse(JUMP_HEIGHT *
  319. vec3(JUMP_STRAFE, 1.f, JUMP_STRAFE) *
  320. vec3(rand(-1.f, 1.f), 1.0f, rand(-1.f, 1.f)) *
  321. PhysObj->GetPhysic()->GetMass());
  322. Timer = ZERO_TIME;
  323. }
  324. }
  325. float fov_ratio = max(max(lol::abs(screen_min_max[0].x), lol::abs(screen_min_max[0].y)),
  326. max(lol::abs(screen_min_max[1].x), lol::abs(screen_min_max[1].y)));
  327. vec3 new_target = cam_center / cam_factor;
  328. float fov_dp = .0f;
  329. float loc_dp = .0f;
  330. //ideally fov is on the target
  331. if (lol::abs(fov_ratio - 1.f) < .2f)
  332. fov_dp = ((m_cam_target == -1)?(.7f):(.2f));
  333. else
  334. fov_dp = ((m_cam_target == -1)?(1.7f):(.9f));
  335. //ideally loc is on the target
  336. if (length(new_target - m_camera->GetTarget()) < 6.f)
  337. loc_dp = ((m_cam_target == -1)?(.5f):(.03f));
  338. else
  339. loc_dp = ((m_cam_target == -1)?(.9f):(.5f));
  340. m_fov_dp = damp(m_fov_dp, fov_dp, 0.08f, seconds);
  341. m_loc_dp = damp(m_loc_dp, loc_dp, 0.08f, seconds);
  342. m_camera->SetFov(damp(m_camera->GetFov(), m_camera->GetFov() * fov_ratio * 1.1f, m_fov_dp, seconds));
  343. vec3 tmp = damp(m_camera->GetTarget(), new_target, m_loc_dp, seconds);
  344. m_camera->SetView((mat4::rotate(10.f * seconds, vec3(.0f, 1.f, .0f)) * vec4(m_camera->GetPosition(), 1.0f)).xyz,
  345. tmp, vec3(0, 1, 0));
  346. #endif //USE_BODIES
  347. #endif //CAT_MODE
  348. #if USE_WALL
  349. {
  350. for (int i = 0; i < m_ground_list.Count(); i++)
  351. {
  352. PhysicsObject* PhysObj = m_ground_list[i];
  353. mat4 GroundMat = PhysObj->GetTransform();
  354. GroundBarycenter += GroundMat.v3.xyz;
  355. factor += 1.f;
  356. }
  357. GroundBarycenter /= factor;
  358. for (int i = 0; i < m_ground_list.Count(); i++)
  359. {
  360. PhysicsObject* PhysObj = m_ground_list[i];
  361. mat4 GroundMat = PhysObj->GetTransform();
  362. vec3 CenterToGround = GroundMat.v3.xyz - GroundBarycenter;
  363. vec3 CenterToCam = m_camera->GetPosition() - GroundBarycenter;
  364. if (dot(normalize(CenterToCam - CenterToGround),
  365. normalize(CenterToGround)) > 0.f)
  366. PhysObj->SetRender(false);
  367. else
  368. PhysObj->SetRender(true);
  369. }
  370. }
  371. #endif //USE_WALL
  372. #if USE_ROTATION
  373. {
  374. for (int i = 0; i < m_ground_list.Count(); i++)
  375. {
  376. PhysicsObject* PhysObj = m_ground_list[i];
  377. mat4 GroundMat = PhysObj->GetTransform();
  378. mat4 CenterMx = mat4::translate(GroundBarycenter);
  379. GroundMat = inverse(CenterMx) * GroundMat;
  380. GroundMat = CenterMx *
  381. mat4(quat::fromeuler_xyz(vec3(.0f, 20.f, 20.0f) * seconds))
  382. * GroundMat;
  383. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  384. }
  385. }
  386. #endif //USE_ROTATION
  387. #if USE_PLATFORM
  388. {
  389. for (int i = 0; i < m_platform_list.Count(); i++)
  390. {
  391. PhysicsObject* PhysObj = m_platform_list[i];
  392. mat4 GroundMat = PhysObj->GetTransform();
  393. if (i == 0)
  394. {
  395. GroundMat = GroundMat * mat4(quat::fromeuler_xyz(vec3(20.f, .0f, .0f) * seconds));
  396. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  397. }
  398. else if (i == 1)
  399. {
  400. GroundMat =
  401. mat4::translate(vec3(-15.0f, 5.0f, lol::cos(m_loop_value) * 8.f)) *
  402. mat4(quat::fromeuler_xyz(vec3(.0f, lol::cos(m_loop_value) * 20.f, .0f)));
  403. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  404. }
  405. }
  406. }
  407. #endif //USE_PLATFORM
  408. #if USE_CHARACTER
  409. {
  410. for (int i = 0; i < m_character_list.Count(); i++)
  411. {
  412. PhysicsObject* PhysObj = m_character_list[i];
  413. EasyCharacterController* Character = (EasyCharacterController*)PhysObj->GetCharacter();
  414. mat4 CtlrMx = Character->GetTransform();
  415. vec3 movement(0.f);
  416. movement.z = (m_controller->GetKey(KEY_MOVE_RIGHT).IsDown() ? 1.f : 0.f)
  417. - (m_controller->GetKey(KEY_MOVE_LEFT).IsDown() ? 1.f : 0.f);
  418. movement.x = (m_controller->GetKey(KEY_MOVE_FORWARD).IsDown() ? 1.f : 0.f)
  419. - (m_controller->GetKey(KEY_MOVE_BACK).IsDown() ? 1.f : 0.f);
  420. movement.y = (m_controller->GetKey(KEY_MOVE_UP).IsDown() ? 1.f : 0.f)
  421. - (m_controller->GetKey(KEY_MOVE_DOWN).IsDown() ? 1.f : 0.f);
  422. vec3 CharMove = movement * seconds * vec3(4.f, 10.f, 4.f);
  423. if (m_controller->GetKey(KEY_MOVE_JUMP).IsReleased())
  424. Character->Jump();
  425. Character->SetMovementForFrame(CharMove);
  426. RayCastResult HitResult;
  427. if (m_simulation->RayHits(HitResult, ERT_Closest, Character->GetTransform().v3.xyz, (Character->GetTransform().v3.xyz + vec3(.0f, -1.f, .0f)), Character))
  428. Character->AttachTo(HitResult.m_collider_list[0], true, true);
  429. else
  430. Character->AttachTo(NULL);
  431. }
  432. }
  433. #endif //USE_CHARACTER
  434. #if USE_CHARACTER
  435. {
  436. PhysObjBarycenter = vec3(.0f);
  437. factor = .0f;
  438. for (int i = 0; i < m_character_list.Count(); i++)
  439. {
  440. PhysicsObject* PhysObj = m_character_list[i];
  441. mat4 GroundMat = PhysObj->GetTransform();
  442. PhysObjBarycenter += GroundMat.v3.xyz;
  443. factor += 1.f;
  444. }
  445. PhysObjBarycenter /= factor;
  446. #if 0
  447. m_camera->SetTarget(m_camera->GetTarget() + (seconds / (seconds + 0.18f)) * (PhysObjBarycenter - m_camera->GetTarget()));
  448. vec3 CamPosCenter = m_camera->GetTarget() + vec3(.0f, 5.0f, .0f);
  449. m_camera->SetPosition(CamPosCenter + normalize(m_camera->GetPosition() - CamPosCenter) * 20.0f);
  450. #endif
  451. }
  452. #else
  453. {
  454. PhysObjBarycenter = vec3(.0f);
  455. for (int i = 0; i < m_physobj_list.Count(); i++)
  456. {
  457. PhysicsObject* PhysObj = m_physobj_list[i].m1;
  458. mat4 GroundMat = PhysObj->GetTransform();
  459. PhysObjBarycenter += GroundMat.v3.xyz;
  460. factor += 1.f;
  461. }
  462. PhysObjBarycenter /= factor;
  463. #if 0
  464. m_camera->SetTarget(PhysObjBarycenter);
  465. m_camera->SetPosition(GroundBarycenter + normalize(GroundBarycenter - PhysObjBarycenter) * 60.0f);
  466. #endif
  467. }
  468. #endif //USE_CHARACTER
  469. }
  470. void BtPhysTest::TickDraw(float seconds)
  471. {
  472. WorldEntity::TickDraw(seconds);
  473. if (m_init_status != 2)
  474. return;
  475. if (!m_ready)
  476. {
  477. #if CAT_MODE
  478. /* cat datas setup */
  479. m_cat_shader = Shader::Create(LOLFX_RESOURCE_NAME(front_camera_sprite));
  480. #if USE_BODIES
  481. for (int i = 0; i < m_physobj_list.Count(); i++)
  482. {
  483. PhysicsObject* PhysObj = m_physobj_list[i].m1;
  484. m_cat_sdata = new CatShaderData(((1 << VertexUsage::Position) |
  485. (1 << VertexUsage::Color) |
  486. (1 << VertexUsage::TexCoord) |
  487. (1 << VertexUsage::TexCoordExt)),
  488. m_cat_shader);
  489. m_cat_sdata->m_shader_texture = m_cat_texture->GetTexture();
  490. m_cat_sdata->m_sprite_flip = ((rand(2) == 1)?(1.f):(0.f)) / (float)(NB_SPRITE * PARTICLE_SIZE);
  491. PhysObj->SetCustomShaderData(m_cat_sdata);
  492. m_cat_sdata = NULL;
  493. }
  494. #endif //USE_BODIES
  495. #endif //CAT_MODE
  496. /* FIXME: this object never cleans up */
  497. m_ready = true;
  498. }
  499. else
  500. {
  501. #if CAT_MODE
  502. for (int i = 0; i < m_physobj_list.Count(); i++)
  503. {
  504. PhysicsObject* PhysObj = m_physobj_list[i].m1;
  505. CatShaderData* ShaderData = (CatShaderData*)PhysObj->GetCustomShaderData();
  506. ShaderData->m_sprite_orientation = damp(ShaderData->m_sprite_orientation,
  507. F_PI_4 * ((ShaderData->m_sprite_flip * 2.f * (float)(NB_SPRITE * PARTICLE_SIZE)) - 1.f) *
  508. clamp(PhysObj->GetPhysic()->GetLinearVelocity().y / 20.0f, -1.f, 1.f),
  509. 0.1f, seconds);
  510. }
  511. #endif //CAT_MODE
  512. }
  513. //Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
  514. }
  515. BtPhysTest::~BtPhysTest()
  516. {
  517. g_scene->PopCamera(m_camera);
  518. Ticker::Unref(m_light1);
  519. Ticker::Unref(m_light2);
  520. #if CAT_MODE
  521. /* cat datas setup */
  522. Shader::Destroy(m_cat_shader);
  523. Tiler::Deregister(m_cat_texture);
  524. #endif //CAT_MODE
  525. while (m_constraint_list.Count())
  526. {
  527. EasyConstraint* CurPop = m_constraint_list.Last();
  528. m_constraint_list.Pop();
  529. CurPop->RemoveFromSimulation(m_simulation);
  530. delete CurPop;
  531. }
  532. while (m_ground_list.Count())
  533. {
  534. PhysicsObject* CurPop = m_ground_list.Last();
  535. m_ground_list.Pop();
  536. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  537. Ticker::Unref(CurPop);
  538. }
  539. while (m_stairs_list.Count())
  540. {
  541. PhysicsObject* CurPop = m_stairs_list.Last();
  542. m_stairs_list.Pop();
  543. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  544. Ticker::Unref(CurPop);
  545. }
  546. while (m_character_list.Count())
  547. {
  548. PhysicsObject* CurPop = m_character_list.Last();
  549. m_character_list.Pop();
  550. CurPop->GetCharacter()->RemoveFromSimulation(m_simulation);
  551. Ticker::Unref(CurPop);
  552. }
  553. while (m_platform_list.Count())
  554. {
  555. PhysicsObject* CurPop = m_platform_list.Last();
  556. m_platform_list.Pop();
  557. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  558. Ticker::Unref(CurPop);
  559. }
  560. while (m_physobj_list.Count())
  561. {
  562. PhysicsObject* CurPop = m_physobj_list.Last().m1;
  563. m_physobj_list.Pop();
  564. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  565. Ticker::Unref(CurPop);
  566. }
  567. Ticker::Unref(m_simulation);
  568. }
  569. //-----------------------------------------------------------------------------
  570. // CShaderData
  571. //-----------------------------------------------------------------------------
  572. CatShaderData::CatShaderData(uint32_t vert_decl_flags, Shader* shader)
  573. : GpuShaderData(vert_decl_flags, shader, DebugRenderMode::Default)
  574. {
  575. m_sprite_orientation = .0f;
  576. m_sprite_flip = .0f;
  577. SetupDefaultData();
  578. }
  579. //-----------------------------------------------------------------------------
  580. void CatShaderData::SetupDefaultData()
  581. {
  582. AddUniform("in_model_view");
  583. AddUniform("in_normal_mat");
  584. AddUniform("in_proj");
  585. AddUniform("in_texture");
  586. AddUniform("in_sprite_orientation");
  587. AddUniform("in_sprite_flip");
  588. }
  589. //-----------------------------------------------------------------------------
  590. void CatShaderData::SetupShaderDatas(mat4 const &model)
  591. {
  592. mat4 proj = g_scene->GetCamera()->GetProjection();
  593. mat4 view = g_scene->GetCamera()->GetView();
  594. mat4 modelview = view * model;
  595. mat3 normalmat = transpose(inverse(mat3(view)));
  596. m_shader->SetUniform(*GetUniform("in_model_view"), modelview);
  597. m_shader->SetUniform(*GetUniform("in_normal_mat"), normalmat);
  598. m_shader->SetUniform(*GetUniform("in_proj"), proj);
  599. m_shader->SetUniform(*GetUniform("in_texture"), m_shader_texture, 0);
  600. m_shader->SetUniform(*GetUniform("in_sprite_orientation"), m_sprite_orientation);
  601. m_shader->SetUniform(*GetUniform("in_sprite_flip"), m_sprite_flip);
  602. }
  603. int main(int argc, char **argv)
  604. {
  605. System::Init(argc, argv);
  606. Application app("BtPhysTest", ivec2(1280, 960), 60.0f);
  607. new BtPhysTest(argc > 1);
  608. app.ShowPointer(false);
  609. app.Run();
  610. return EXIT_SUCCESS;
  611. }