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