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.

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