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