Add Arm Inertia (#6050)
authorkilbith <kilbith@users.noreply.github.com>
Mon, 26 Jun 2017 18:03:48 +0000 (20:03 +0200)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Mon, 26 Jun 2017 18:03:48 +0000 (20:03 +0200)
builtin/settingtypes.txt
minetest.conf.example
src/camera.cpp
src/camera.h
src/defaultsettings.cpp

index f15fc679bb8615df632a78f45f87ac1b5b2e49f7..192ce5b66fd3a0fb67bf8ed176e296227ca02887 100644 (file)
@@ -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
index d0ed146e0405424c4919c37d2d483538f5db4c57..f5f3f5e8c4748d723a21056630b22cba4d3572da 100644 (file)
 
 #### 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
index d5e32bf5ac44281a6a5fadc59a669fc7b2763db2..f75d6018daddf51a67c9635ce275358435ebcfc1 100644 (file)
@@ -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;
index 8d213ede7dbe238e90368063eaf56146868542a4..9ed4b72779c95f0eaa2f2413c1adf6832abeb566 100644 (file)
@@ -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<Nametag *> m_nametags;
 };
index c10d2cb02fe2e7cb0e0978693d9e748652481a0b..42e369f6051ba57e79a15352087570a405391663 100644 (file)
@@ -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");