From 1d1d922a7aafc464ec4483a2f8caa8c49ddbbdee Mon Sep 17 00:00:00 2001 From: kilbith Date: Mon, 26 Jun 2017 20:03:48 +0200 Subject: [PATCH] Add Arm Inertia (#6050) --- builtin/settingtypes.txt | 4 ++ minetest.conf.example | 5 +++ src/camera.cpp | 83 ++++++++++++++++++++++++++++++++++++++-- src/camera.h | 9 +++++ src/defaultsettings.cpp | 1 + 5 files changed, 99 insertions(+), 3 deletions(-) diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index f15fc679b..192ce5b66 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -469,6 +469,10 @@ enable_waving_plants (Waving plants) bool false [***Advanced] +# Arm inertia, gives a more realistic movement of +# the arm when the camera moves. +arm_inertia (Arm inertia) bool true + # If FPS would go higher than this, limit it by sleeping # to not waste CPU power for no benefit. fps_max (Maximum FPS) int 60 diff --git a/minetest.conf.example b/minetest.conf.example index d0ed146e0..f5f3f5e8c 100644 --- a/minetest.conf.example +++ b/minetest.conf.example @@ -534,6 +534,11 @@ #### Advanced +# Arm inertia, gives a more realistic movement of +# the arm when the camera moves. +# type: bool +# arm_inertia = true + # If FPS would go higher than this, limit it by sleeping # to not waste CPU power for no benefit. # type: int diff --git a/src/camera.cpp b/src/camera.cpp index d5e32bf5a..f75d6018d 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -75,6 +75,7 @@ Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control, m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount"); m_cache_fov = g_settings->getFloat("fov"); m_cache_zoom_fov = g_settings->getFloat("zoom_fov"); + m_arm_inertia = g_settings->getBool("arm_inertia"); m_nametags.clear(); } @@ -193,6 +194,82 @@ void Camera::step(f32 dtime) } } +void Camera::add_arm_inertia(f32 player_yaw, f32 frametime) +{ + m_cam_vel.X = std::fabs((m_last_cam_pos.X - player_yaw) / m_timer.X) * 0.01f; + m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / m_timer.Y); + + if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) { + /* + the arm moves when the camera moves fast enough. + */ + + if (m_cam_vel.X > 1.0f) { + m_timer.X = 0.0f; + m_timer.X += frametime; + + if (m_cam_vel.X > m_cam_vel_old.X) + m_cam_vel_old.X = m_cam_vel.X; + + if (m_last_cam_pos.X > player_yaw) + // right + m_wieldmesh_offset.X -= (0.1f + frametime) * m_cam_vel.X; + else + // left + m_wieldmesh_offset.X += (0.1f + frametime) * m_cam_vel.X; + + if (m_last_cam_pos.X != player_yaw) + m_last_cam_pos.X = player_yaw; + + m_wieldmesh_offset.X = rangelim(m_wieldmesh_offset.X, 48.0f, 62.0f); + } + + if (m_cam_vel.Y > 1.0f) { + m_timer.Y = 0.0f; + m_timer.Y += frametime; + + if (m_cam_vel.Y > m_cam_vel_old.Y) + m_cam_vel_old.Y = m_cam_vel.Y; + + if (m_last_cam_pos.Y > m_camera_direction.Y) + // down + m_wieldmesh_offset.Y += (0.1f + frametime) * m_cam_vel.Y; + else + // up + m_wieldmesh_offset.Y -= (0.1f + frametime) * m_cam_vel.Y; + + if (m_last_cam_pos.Y != m_camera_direction.Y) + m_last_cam_pos.Y = m_camera_direction.Y; + + m_wieldmesh_offset.Y = rangelim(m_wieldmesh_offset.Y, -45.0f, -30.0f); + } + } else { + /* + the arm now gets back to its default position when the camera stops. + */ + + if (floor(m_wieldmesh_offset.X) == 55.0f) { + m_cam_vel_old.X = 0.0f; + m_wieldmesh_offset.X = 55.0f; + } + + if (m_wieldmesh_offset.X > 55.0f) + m_wieldmesh_offset.X -= (0.05f + frametime) * m_cam_vel_old.X; + if (m_wieldmesh_offset.X < 55.0f) + m_wieldmesh_offset.X += (0.05f + frametime) * m_cam_vel_old.X; + + if (floor(m_wieldmesh_offset.Y) == -35.0f) { + m_cam_vel_old.Y = 0.0f; + m_wieldmesh_offset.Y = -35.0f; + } + + if (m_wieldmesh_offset.Y > -35.0f) + m_wieldmesh_offset.Y -= (0.05f + frametime) * m_cam_vel_old.Y; + if (m_wieldmesh_offset.Y < -35.0f) + m_wieldmesh_offset.Y += (0.05f + frametime) * m_cam_vel_old.Y; + } +} + void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, f32 tool_reload_ratio, ClientEnvironment &c_env) { @@ -380,12 +457,12 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime, m_cameranode->setAspectRatio(m_aspect); m_cameranode->setFOV(m_fov_y); - float wieldmesh_offset_Y = -35 + player->getPitch() * 0.05; - wieldmesh_offset_Y = rangelim(wieldmesh_offset_Y, -52, -32); + if (m_arm_inertia) + add_arm_inertia(player->getYaw(), frametime); // Position the wielded item //v3f wield_position = v3f(45, -35, 65); - v3f wield_position = v3f(55, wieldmesh_offset_Y, 65); + v3f wield_position = v3f(m_wieldmesh_offset.X, m_wieldmesh_offset.Y, 65); //v3f wield_rotation = v3f(-100, 120, -100); v3f wield_rotation = v3f(-100, 120, -100); wield_position.Y += fabs(m_wield_change_timer)*320 - 40; diff --git a/src/camera.h b/src/camera.h index 8d213ede7..9ed4b7277 100644 --- a/src/camera.h +++ b/src/camera.h @@ -166,6 +166,8 @@ public: void drawNametags(); + inline void add_arm_inertia(f32 player_yaw, f32 frametime); + private: // Nodes scene::ISceneNode *m_playernode = nullptr; @@ -188,6 +190,12 @@ private: // Camera offset v3s16 m_camera_offset; + v2f m_wieldmesh_offset = v2f(55.0f, -35.0f); + v2f m_timer; + v2f m_cam_vel; + v2f m_cam_vel_old; + v2f m_last_cam_pos; + // Field of view and aspect ratio stuff f32 m_aspect = 1.0f; f32 m_fov_x = 1.0f; @@ -221,6 +229,7 @@ private: f32 m_cache_view_bobbing_amount; f32 m_cache_fov; f32 m_cache_zoom_fov; + bool m_arm_inertia; std::list m_nametags; }; diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index c10d2cb02..42e369f60 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -171,6 +171,7 @@ void set_default_settings(Settings *settings) settings->setDefault("ambient_occlusion_gamma", "2.2"); settings->setDefault("enable_shaders", "true"); settings->setDefault("enable_particles", "true"); + settings->setDefault("arm_inertia", "true"); settings->setDefault("enable_minimap", "true"); settings->setDefault("minimap_shape_round", "true"); -- 2.25.1