camera.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "camera.h"
  17. #include "debug.h"
  18. #include "client.h"
  19. #include "map.h"
  20. #include "clientmap.h" // MapDrawControl
  21. #include "player.h"
  22. #include <cmath>
  23. #include "client/renderingengine.h"
  24. #include "settings.h"
  25. #include "wieldmesh.h"
  26. #include "noise.h" // easeCurve
  27. #include "sound.h"
  28. #include "event.h"
  29. #include "nodedef.h"
  30. #include "util/numeric.h"
  31. #include "constants.h"
  32. #include "fontengine.h"
  33. #include "script/scripting_client.h"
  34. #define CAMERA_OFFSET_STEP 200
  35. #define WIELDMESH_OFFSET_X 55.0f
  36. #define WIELDMESH_OFFSET_Y -35.0f
  37. Camera::Camera(MapDrawControl &draw_control, Client *client):
  38. m_draw_control(draw_control),
  39. m_client(client)
  40. {
  41. scene::ISceneManager *smgr = RenderingEngine::get_scene_manager();
  42. // note: making the camera node a child of the player node
  43. // would lead to unexpected behaviour, so we don't do that.
  44. m_playernode = smgr->addEmptySceneNode(smgr->getRootSceneNode());
  45. m_headnode = smgr->addEmptySceneNode(m_playernode);
  46. m_cameranode = smgr->addCameraSceneNode(smgr->getRootSceneNode());
  47. m_cameranode->bindTargetAndRotation(true);
  48. // This needs to be in its own scene manager. It is drawn after
  49. // all other 3D scene nodes and before the GUI.
  50. m_wieldmgr = smgr->createNewSceneManager();
  51. m_wieldmgr->addCameraSceneNode();
  52. m_wieldnode = new WieldMeshSceneNode(m_wieldmgr, -1, false);
  53. m_wieldnode->setItem(ItemStack(), m_client);
  54. m_wieldnode->drop(); // m_wieldmgr grabbed it
  55. /* TODO: Add a callback function so these can be updated when a setting
  56. * changes. At this point in time it doesn't matter (e.g. /set
  57. * is documented to change server settings only)
  58. *
  59. * TODO: Local caching of settings is not optimal and should at some stage
  60. * be updated to use a global settings object for getting thse values
  61. * (as opposed to the this local caching). This can be addressed in
  62. * a later release.
  63. */
  64. m_cache_fall_bobbing_amount = g_settings->getFloat("fall_bobbing_amount");
  65. m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount");
  66. // 45 degrees is the lowest FOV that doesn't cause the server to treat this
  67. // as a zoom FOV and load world beyond the set server limits.
  68. m_cache_fov = std::fmax(g_settings->getFloat("fov"), 45.0f);
  69. m_arm_inertia = g_settings->getBool("arm_inertia");
  70. m_nametags.clear();
  71. }
  72. Camera::~Camera()
  73. {
  74. m_wieldmgr->drop();
  75. }
  76. bool Camera::successfullyCreated(std::string &error_message)
  77. {
  78. if (!m_playernode) {
  79. error_message = "Failed to create the player scene node";
  80. } else if (!m_headnode) {
  81. error_message = "Failed to create the head scene node";
  82. } else if (!m_cameranode) {
  83. error_message = "Failed to create the camera scene node";
  84. } else if (!m_wieldmgr) {
  85. error_message = "Failed to create the wielded item scene manager";
  86. } else if (!m_wieldnode) {
  87. error_message = "Failed to create the wielded item scene node";
  88. } else {
  89. error_message.clear();
  90. }
  91. if (g_settings->getBool("enable_client_modding")) {
  92. m_client->getScript()->on_camera_ready(this);
  93. }
  94. return error_message.empty();
  95. }
  96. // Returns the fractional part of x
  97. inline f32 my_modf(f32 x)
  98. {
  99. double dummy;
  100. return modf(x, &dummy);
  101. }
  102. void Camera::step(f32 dtime)
  103. {
  104. if(m_view_bobbing_fall > 0)
  105. {
  106. m_view_bobbing_fall -= 3 * dtime;
  107. if(m_view_bobbing_fall <= 0)
  108. m_view_bobbing_fall = -1; // Mark the effect as finished
  109. }
  110. bool was_under_zero = m_wield_change_timer < 0;
  111. m_wield_change_timer = MYMIN(m_wield_change_timer + dtime, 0.125);
  112. if (m_wield_change_timer >= 0 && was_under_zero)
  113. m_wieldnode->setItem(m_wield_item_next, m_client);
  114. if (m_view_bobbing_state != 0)
  115. {
  116. //f32 offset = dtime * m_view_bobbing_speed * 0.035;
  117. f32 offset = dtime * m_view_bobbing_speed * 0.030;
  118. if (m_view_bobbing_state == 2) {
  119. // Animation is getting turned off
  120. if (m_view_bobbing_anim < 0.25) {
  121. m_view_bobbing_anim -= offset;
  122. } else if (m_view_bobbing_anim > 0.75) {
  123. m_view_bobbing_anim += offset;
  124. }
  125. if (m_view_bobbing_anim < 0.5) {
  126. m_view_bobbing_anim += offset;
  127. if (m_view_bobbing_anim > 0.5)
  128. m_view_bobbing_anim = 0.5;
  129. } else {
  130. m_view_bobbing_anim -= offset;
  131. if (m_view_bobbing_anim < 0.5)
  132. m_view_bobbing_anim = 0.5;
  133. }
  134. if (m_view_bobbing_anim <= 0 || m_view_bobbing_anim >= 1 ||
  135. fabs(m_view_bobbing_anim - 0.5) < 0.01) {
  136. m_view_bobbing_anim = 0;
  137. m_view_bobbing_state = 0;
  138. }
  139. }
  140. else {
  141. float was = m_view_bobbing_anim;
  142. m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
  143. bool step = (was == 0 ||
  144. (was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
  145. (was > 0.5f && m_view_bobbing_anim <= 0.5f));
  146. if(step) {
  147. m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::VIEW_BOBBING_STEP));
  148. }
  149. }
  150. }
  151. if (m_digging_button != -1) {
  152. f32 offset = dtime * 3.5f;
  153. float m_digging_anim_was = m_digging_anim;
  154. m_digging_anim += offset;
  155. if (m_digging_anim >= 1)
  156. {
  157. m_digging_anim = 0;
  158. m_digging_button = -1;
  159. }
  160. float lim = 0.15;
  161. if(m_digging_anim_was < lim && m_digging_anim >= lim)
  162. {
  163. if (m_digging_button == 0) {
  164. m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_LEFT));
  165. } else if(m_digging_button == 1) {
  166. m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::CAMERA_PUNCH_RIGHT));
  167. }
  168. }
  169. }
  170. }
  171. static inline v2f dir(const v2f &pos_dist)
  172. {
  173. f32 x = pos_dist.X - WIELDMESH_OFFSET_X;
  174. f32 y = pos_dist.Y - WIELDMESH_OFFSET_Y;
  175. f32 x_abs = std::fabs(x);
  176. f32 y_abs = std::fabs(y);
  177. if (x_abs >= y_abs) {
  178. y *= (1.0f / x_abs);
  179. x /= x_abs;
  180. }
  181. if (y_abs >= x_abs) {
  182. x *= (1.0f / y_abs);
  183. y /= y_abs;
  184. }
  185. return v2f(std::fabs(x), std::fabs(y));
  186. }
  187. void Camera::addArmInertia(f32 player_yaw)
  188. {
  189. m_cam_vel.X = std::fabs(rangelim(m_last_cam_pos.X - player_yaw,
  190. -100.0f, 100.0f) / 0.016f) * 0.01f;
  191. m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / 0.016f);
  192. f32 gap_X = std::fabs(WIELDMESH_OFFSET_X - m_wieldmesh_offset.X);
  193. f32 gap_Y = std::fabs(WIELDMESH_OFFSET_Y - m_wieldmesh_offset.Y);
  194. if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) {
  195. /*
  196. The arm moves relative to the camera speed,
  197. with an acceleration factor.
  198. */
  199. if (m_cam_vel.X > 1.0f) {
  200. if (m_cam_vel.X > m_cam_vel_old.X)
  201. m_cam_vel_old.X = m_cam_vel.X;
  202. f32 acc_X = 0.12f * (m_cam_vel.X - (gap_X * 0.1f));
  203. m_wieldmesh_offset.X += m_last_cam_pos.X < player_yaw ? acc_X : -acc_X;
  204. if (m_last_cam_pos.X != player_yaw)
  205. m_last_cam_pos.X = player_yaw;
  206. m_wieldmesh_offset.X = rangelim(m_wieldmesh_offset.X,
  207. WIELDMESH_OFFSET_X - 7.0f, WIELDMESH_OFFSET_X + 7.0f);
  208. }
  209. if (m_cam_vel.Y > 1.0f) {
  210. if (m_cam_vel.Y > m_cam_vel_old.Y)
  211. m_cam_vel_old.Y = m_cam_vel.Y;
  212. f32 acc_Y = 0.12f * (m_cam_vel.Y - (gap_Y * 0.1f));
  213. m_wieldmesh_offset.Y +=
  214. m_last_cam_pos.Y > m_camera_direction.Y ? acc_Y : -acc_Y;
  215. if (m_last_cam_pos.Y != m_camera_direction.Y)
  216. m_last_cam_pos.Y = m_camera_direction.Y;
  217. m_wieldmesh_offset.Y = rangelim(m_wieldmesh_offset.Y,
  218. WIELDMESH_OFFSET_Y - 10.0f, WIELDMESH_OFFSET_Y + 5.0f);
  219. }
  220. m_arm_dir = dir(m_wieldmesh_offset);
  221. } else {
  222. /*
  223. Now the arm gets back to its default position when the camera stops,
  224. following a vector, with a smooth deceleration factor.
  225. */
  226. f32 dec_X = 0.12f * (m_cam_vel_old.X * (1.0f +
  227. (1.0f - m_arm_dir.X))) * (gap_X / 20.0f);
  228. f32 dec_Y = 0.06f * (m_cam_vel_old.Y * (1.0f +
  229. (1.0f - m_arm_dir.Y))) * (gap_Y / 15.0f);
  230. if (gap_X < 0.1f)
  231. m_cam_vel_old.X = 0.0f;
  232. m_wieldmesh_offset.X -=
  233. m_wieldmesh_offset.X > WIELDMESH_OFFSET_X ? dec_X : -dec_X;
  234. if (gap_Y < 0.1f)
  235. m_cam_vel_old.Y = 0.0f;
  236. m_wieldmesh_offset.Y -=
  237. m_wieldmesh_offset.Y > WIELDMESH_OFFSET_Y ? dec_Y : -dec_Y;
  238. }
  239. }
  240. void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_reload_ratio)
  241. {
  242. // Get player position
  243. // Smooth the movement when walking up stairs
  244. v3f old_player_position = m_playernode->getPosition();
  245. v3f player_position = player->getPosition();
  246. if (player->isAttached && player->parent)
  247. player_position = player->parent->getPosition();
  248. //if(player->touching_ground && player_position.Y > old_player_position.Y)
  249. if(player->touching_ground &&
  250. player_position.Y > old_player_position.Y)
  251. {
  252. f32 oldy = old_player_position.Y;
  253. f32 newy = player_position.Y;
  254. f32 t = std::exp(-23 * frametime);
  255. player_position.Y = oldy * t + newy * (1-t);
  256. }
  257. // Set player node transformation
  258. m_playernode->setPosition(player_position);
  259. m_playernode->setRotation(v3f(0, -1 * player->getYaw(), 0));
  260. m_playernode->updateAbsolutePosition();
  261. // Get camera tilt timer (hurt animation)
  262. float cameratilt = fabs(fabs(player->hurt_tilt_timer-0.75)-0.75);
  263. // Fall bobbing animation
  264. float fall_bobbing = 0;
  265. if(player->camera_impact >= 1 && m_camera_mode < CAMERA_MODE_THIRD)
  266. {
  267. if(m_view_bobbing_fall == -1) // Effect took place and has finished
  268. player->camera_impact = m_view_bobbing_fall = 0;
  269. else if(m_view_bobbing_fall == 0) // Initialize effect
  270. m_view_bobbing_fall = 1;
  271. // Convert 0 -> 1 to 0 -> 1 -> 0
  272. fall_bobbing = m_view_bobbing_fall < 0.5 ? m_view_bobbing_fall * 2 : -(m_view_bobbing_fall - 0.5) * 2 + 1;
  273. // Smoothen and invert the above
  274. fall_bobbing = sin(fall_bobbing * 0.5 * M_PI) * -1;
  275. // Amplify according to the intensity of the impact
  276. fall_bobbing *= (1 - rangelim(50 / player->camera_impact, 0, 1)) * 5;
  277. fall_bobbing *= m_cache_fall_bobbing_amount;
  278. }
  279. // Calculate players eye offset for different camera modes
  280. v3f PlayerEyeOffset = player->getEyeOffset();
  281. if (m_camera_mode == CAMERA_MODE_FIRST)
  282. PlayerEyeOffset += player->eye_offset_first;
  283. else
  284. PlayerEyeOffset += player->eye_offset_third;
  285. // Set head node transformation
  286. m_headnode->setPosition(PlayerEyeOffset+v3f(0,cameratilt*-player->hurt_tilt_strength+fall_bobbing,0));
  287. m_headnode->setRotation(v3f(player->getPitch(), 0, cameratilt*player->hurt_tilt_strength));
  288. m_headnode->updateAbsolutePosition();
  289. // Compute relative camera position and target
  290. v3f rel_cam_pos = v3f(0,0,0);
  291. v3f rel_cam_target = v3f(0,0,1);
  292. v3f rel_cam_up = v3f(0,1,0);
  293. if (m_cache_view_bobbing_amount != 0.0f && m_view_bobbing_anim != 0.0f &&
  294. m_camera_mode < CAMERA_MODE_THIRD) {
  295. f32 bobfrac = my_modf(m_view_bobbing_anim * 2);
  296. f32 bobdir = (m_view_bobbing_anim < 0.5) ? 1.0 : -1.0;
  297. #if 1
  298. f32 bobknob = 1.2;
  299. f32 bobtmp = sin(pow(bobfrac, bobknob) * M_PI);
  300. //f32 bobtmp2 = cos(pow(bobfrac, bobknob) * M_PI);
  301. v3f bobvec = v3f(
  302. 0.3 * bobdir * sin(bobfrac * M_PI),
  303. -0.28 * bobtmp * bobtmp,
  304. 0.);
  305. //rel_cam_pos += 0.2 * bobvec;
  306. //rel_cam_target += 0.03 * bobvec;
  307. //rel_cam_up.rotateXYBy(0.02 * bobdir * bobtmp * M_PI);
  308. float f = 1.0;
  309. f *= m_cache_view_bobbing_amount;
  310. rel_cam_pos += bobvec * f;
  311. //rel_cam_target += 0.995 * bobvec * f;
  312. rel_cam_target += bobvec * f;
  313. rel_cam_target.Z -= 0.005 * bobvec.Z * f;
  314. //rel_cam_target.X -= 0.005 * bobvec.X * f;
  315. //rel_cam_target.Y -= 0.005 * bobvec.Y * f;
  316. rel_cam_up.rotateXYBy(-0.03 * bobdir * bobtmp * M_PI * f);
  317. #else
  318. f32 angle_deg = 1 * bobdir * sin(bobfrac * M_PI);
  319. f32 angle_rad = angle_deg * M_PI / 180;
  320. f32 r = 0.05;
  321. v3f off = v3f(
  322. r * sin(angle_rad),
  323. r * (cos(angle_rad) - 1),
  324. 0);
  325. rel_cam_pos += off;
  326. //rel_cam_target += off;
  327. rel_cam_up.rotateXYBy(angle_deg);
  328. #endif
  329. }
  330. // Compute absolute camera position and target
  331. m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
  332. m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
  333. v3f abs_cam_up;
  334. m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
  335. // Seperate camera position for calculation
  336. v3f my_cp = m_camera_position;
  337. // Reposition the camera for third person view
  338. if (m_camera_mode > CAMERA_MODE_FIRST)
  339. {
  340. if (m_camera_mode == CAMERA_MODE_THIRD_FRONT)
  341. m_camera_direction *= -1;
  342. my_cp.Y += 2;
  343. // Calculate new position
  344. bool abort = false;
  345. for (int i = BS; i <= BS * 2.75; i++) {
  346. my_cp.X = m_camera_position.X + m_camera_direction.X * -i;
  347. my_cp.Z = m_camera_position.Z + m_camera_direction.Z * -i;
  348. if (i > 12)
  349. my_cp.Y = m_camera_position.Y + (m_camera_direction.Y * -i);
  350. // Prevent camera positioned inside nodes
  351. const NodeDefManager *nodemgr = m_client->ndef();
  352. MapNode n = m_client->getEnv().getClientMap()
  353. .getNodeNoEx(floatToInt(my_cp, BS));
  354. const ContentFeatures& features = nodemgr->get(n);
  355. if (features.walkable) {
  356. my_cp.X += m_camera_direction.X*-1*-BS/2;
  357. my_cp.Z += m_camera_direction.Z*-1*-BS/2;
  358. my_cp.Y += m_camera_direction.Y*-1*-BS/2;
  359. abort = true;
  360. break;
  361. }
  362. }
  363. // If node blocks camera position don't move y to heigh
  364. if (abort && my_cp.Y > player_position.Y+BS*2)
  365. my_cp.Y = player_position.Y+BS*2;
  366. }
  367. // Update offset if too far away from the center of the map
  368. m_camera_offset.X += CAMERA_OFFSET_STEP*
  369. (((s16)(my_cp.X/BS) - m_camera_offset.X)/CAMERA_OFFSET_STEP);
  370. m_camera_offset.Y += CAMERA_OFFSET_STEP*
  371. (((s16)(my_cp.Y/BS) - m_camera_offset.Y)/CAMERA_OFFSET_STEP);
  372. m_camera_offset.Z += CAMERA_OFFSET_STEP*
  373. (((s16)(my_cp.Z/BS) - m_camera_offset.Z)/CAMERA_OFFSET_STEP);
  374. // Set camera node transformation
  375. m_cameranode->setPosition(my_cp-intToFloat(m_camera_offset, BS));
  376. m_cameranode->setUpVector(abs_cam_up);
  377. // *100.0 helps in large map coordinates
  378. m_cameranode->setTarget(my_cp-intToFloat(m_camera_offset, BS) + 100 * m_camera_direction);
  379. // update the camera position in third-person mode to render blocks behind player
  380. // and correctly apply liquid post FX.
  381. if (m_camera_mode != CAMERA_MODE_FIRST)
  382. m_camera_position = my_cp;
  383. // Get FOV
  384. f32 fov_degrees;
  385. // Disable zoom with zoom FOV = 0
  386. if (player->getPlayerControl().zoom && player->getZoomFOV() > 0.001f) {
  387. fov_degrees = player->getZoomFOV();
  388. } else {
  389. fov_degrees = m_cache_fov;
  390. }
  391. fov_degrees = rangelim(fov_degrees, 1.0f, 160.0f);
  392. // FOV and aspect ratio
  393. const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
  394. m_aspect = (f32) window_size.X / (f32) window_size.Y;
  395. m_fov_y = fov_degrees * M_PI / 180.0;
  396. // Increase vertical FOV on lower aspect ratios (<16:10)
  397. m_fov_y *= MYMAX(1.0, MYMIN(1.4, sqrt(16./10. / m_aspect)));
  398. m_fov_x = 2 * atan(m_aspect * tan(0.5 * m_fov_y));
  399. m_cameranode->setAspectRatio(m_aspect);
  400. m_cameranode->setFOV(m_fov_y);
  401. if (m_arm_inertia)
  402. addArmInertia(player->getYaw());
  403. // Position the wielded item
  404. //v3f wield_position = v3f(45, -35, 65);
  405. v3f wield_position = v3f(m_wieldmesh_offset.X, m_wieldmesh_offset.Y, 65);
  406. //v3f wield_rotation = v3f(-100, 120, -100);
  407. v3f wield_rotation = v3f(-100, 120, -100);
  408. wield_position.Y += fabs(m_wield_change_timer)*320 - 40;
  409. if(m_digging_anim < 0.05 || m_digging_anim > 0.5)
  410. {
  411. f32 frac = 1.0;
  412. if(m_digging_anim > 0.5)
  413. frac = 2.0 * (m_digging_anim - 0.5);
  414. // This value starts from 1 and settles to 0
  415. f32 ratiothing = std::pow((1.0f - tool_reload_ratio), 0.5f);
  416. //f32 ratiothing2 = pow(ratiothing, 0.5f);
  417. f32 ratiothing2 = (easeCurve(ratiothing*0.5))*2.0;
  418. wield_position.Y -= frac * 25.0 * pow(ratiothing2, 1.7f);
  419. //wield_position.Z += frac * 5.0 * ratiothing2;
  420. wield_position.X -= frac * 35.0 * pow(ratiothing2, 1.1f);
  421. wield_rotation.Y += frac * 70.0 * pow(ratiothing2, 1.4f);
  422. //wield_rotation.X -= frac * 15.0 * pow(ratiothing2, 1.4f);
  423. //wield_rotation.Z += frac * 15.0 * pow(ratiothing2, 1.0f);
  424. }
  425. if (m_digging_button != -1)
  426. {
  427. f32 digfrac = m_digging_anim;
  428. wield_position.X -= 50 * sin(pow(digfrac, 0.8f) * M_PI);
  429. wield_position.Y += 24 * sin(digfrac * 1.8 * M_PI);
  430. wield_position.Z += 25 * 0.5;
  431. // Euler angles are PURE EVIL, so why not use quaternions?
  432. core::quaternion quat_begin(wield_rotation * core::DEGTORAD);
  433. core::quaternion quat_end(v3f(80, 30, 100) * core::DEGTORAD);
  434. core::quaternion quat_slerp;
  435. quat_slerp.slerp(quat_begin, quat_end, sin(digfrac * M_PI));
  436. quat_slerp.toEuler(wield_rotation);
  437. wield_rotation *= core::RADTODEG;
  438. } else {
  439. f32 bobfrac = my_modf(m_view_bobbing_anim);
  440. wield_position.X -= sin(bobfrac*M_PI*2.0) * 3.0;
  441. wield_position.Y += sin(my_modf(bobfrac*2.0)*M_PI) * 3.0;
  442. }
  443. m_wieldnode->setPosition(wield_position);
  444. m_wieldnode->setRotation(wield_rotation);
  445. m_wieldnode->setColor(player->light_color);
  446. // Set render distance
  447. updateViewingRange();
  448. // If the player is walking, swimming, or climbing,
  449. // view bobbing is enabled and free_move is off,
  450. // start (or continue) the view bobbing animation.
  451. const v3f &speed = player->getSpeed();
  452. const bool movement_XZ = hypot(speed.X, speed.Z) > BS;
  453. const bool movement_Y = fabs(speed.Y) > BS;
  454. const bool walking = movement_XZ && player->touching_ground;
  455. const bool swimming = (movement_XZ || player->swimming_vertical) && player->in_liquid;
  456. const bool climbing = movement_Y && player->is_climbing;
  457. if ((walking || swimming || climbing) &&
  458. (!g_settings->getBool("free_move") || !m_client->checkLocalPrivilege("fly"))) {
  459. // Start animation
  460. m_view_bobbing_state = 1;
  461. m_view_bobbing_speed = MYMIN(speed.getLength(), 70);
  462. }
  463. else if (m_view_bobbing_state == 1)
  464. {
  465. // Stop animation
  466. m_view_bobbing_state = 2;
  467. m_view_bobbing_speed = 60;
  468. }
  469. }
  470. void Camera::updateViewingRange()
  471. {
  472. f32 viewing_range = g_settings->getFloat("viewing_range");
  473. f32 near_plane = g_settings->getFloat("near_plane");
  474. m_draw_control.wanted_range = std::fmin(adjustDist(viewing_range, getFovMax()), 4000);
  475. m_cameranode->setNearValue(rangelim(near_plane, 0.0f, 0.5f) * BS);
  476. if (m_draw_control.range_all) {
  477. m_cameranode->setFarValue(100000.0);
  478. return;
  479. }
  480. m_cameranode->setFarValue((viewing_range < 2000) ? 2000 * BS : viewing_range * BS);
  481. }
  482. void Camera::setDigging(s32 button)
  483. {
  484. if (m_digging_button == -1)
  485. m_digging_button = button;
  486. }
  487. void Camera::wield(const ItemStack &item)
  488. {
  489. if (item.name != m_wield_item_next.name ||
  490. item.metadata != m_wield_item_next.metadata) {
  491. m_wield_item_next = item;
  492. if (m_wield_change_timer > 0)
  493. m_wield_change_timer = -m_wield_change_timer;
  494. else if (m_wield_change_timer == 0)
  495. m_wield_change_timer = -0.001;
  496. }
  497. }
  498. void Camera::drawWieldedTool(irr::core::matrix4* translation)
  499. {
  500. // Clear Z buffer so that the wielded tool stay in front of world geometry
  501. m_wieldmgr->getVideoDriver()->clearZBuffer();
  502. // Draw the wielded node (in a separate scene manager)
  503. scene::ICameraSceneNode* cam = m_wieldmgr->getActiveCamera();
  504. cam->setAspectRatio(m_cameranode->getAspectRatio());
  505. cam->setFOV(72.0*M_PI/180.0);
  506. cam->setNearValue(10);
  507. cam->setFarValue(1000);
  508. if (translation != NULL)
  509. {
  510. irr::core::matrix4 startMatrix = cam->getAbsoluteTransformation();
  511. irr::core::vector3df focusPoint = (cam->getTarget()
  512. - cam->getAbsolutePosition()).setLength(1)
  513. + cam->getAbsolutePosition();
  514. irr::core::vector3df camera_pos =
  515. (startMatrix * *translation).getTranslation();
  516. cam->setPosition(camera_pos);
  517. cam->setTarget(focusPoint);
  518. }
  519. m_wieldmgr->drawAll();
  520. }
  521. void Camera::drawNametags()
  522. {
  523. core::matrix4 trans = m_cameranode->getProjectionMatrix();
  524. trans *= m_cameranode->getViewMatrix();
  525. for (std::list<Nametag *>::const_iterator
  526. i = m_nametags.begin();
  527. i != m_nametags.end(); ++i) {
  528. Nametag *nametag = *i;
  529. if (nametag->nametag_color.getAlpha() == 0) {
  530. // Enforce hiding nametag,
  531. // because if freetype is enabled, a grey
  532. // shadow can remain.
  533. continue;
  534. }
  535. v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->nametag_pos * BS;
  536. f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
  537. trans.multiplyWith1x4Matrix(transformed_pos);
  538. if (transformed_pos[3] > 0) {
  539. std::wstring nametag_colorless =
  540. unescape_translate(utf8_to_wide(nametag->nametag_text));
  541. core::dimension2d<u32> textsize =
  542. g_fontengine->getFont()->getDimension(
  543. nametag_colorless.c_str());
  544. f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
  545. core::reciprocal(transformed_pos[3]);
  546. v2u32 screensize = RenderingEngine::get_video_driver()->getScreenSize();
  547. v2s32 screen_pos;
  548. screen_pos.X = screensize.X *
  549. (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
  550. screen_pos.Y = screensize.Y *
  551. (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
  552. core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
  553. g_fontengine->getFont()->draw(
  554. translate_string(utf8_to_wide(nametag->nametag_text)).c_str(),
  555. size + screen_pos, nametag->nametag_color);
  556. }
  557. }
  558. }
  559. Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
  560. const std::string &nametag_text, video::SColor nametag_color,
  561. const v3f &pos)
  562. {
  563. Nametag *nametag = new Nametag(parent_node, nametag_text, nametag_color, pos);
  564. m_nametags.push_back(nametag);
  565. return nametag;
  566. }
  567. void Camera::removeNametag(Nametag *nametag)
  568. {
  569. m_nametags.remove(nametag);
  570. delete nametag;
  571. }