Add support for limiting rotation of automatic face movement dir entitys 3457/head
authorSapier <sapier@does.not.have.an.email>
Mon, 14 Dec 2015 23:03:18 +0000 (00:03 +0100)
committerSapier <sapier AT gmx dot net>
Sat, 19 Dec 2015 16:00:36 +0000 (17:00 +0100)
src/content_cao.cpp
src/content_sao.cpp
src/object_properties.cpp
src/object_properties.h
src/script/common/c_content.cpp

index ed4e3b7133180750e7eda70b7dc32dbe4cef88ed..1b8e84c8fb4f8ec61a4c25ff25edb20437561dfa 100644 (file)
@@ -1260,8 +1260,18 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
        if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
                        (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
        {
-               m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
+               float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
                                + m_prop.automatic_face_movement_dir_offset;
+               float max_rotation_delta =
+                               dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
+
+               if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
+                       (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
+
+                       m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
+               } else {
+                       m_yaw = optimal_yaw;
+               }
                updateNodePos();
        }
 }
index 0a1cfe770f4ec39736aa0aed21ca0a354979a176..4d144aa17453c1415f555bb3b564fe381a5ee6d4 100644 (file)
@@ -283,8 +283,20 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
                }
 
                if((m_prop.automatic_face_movement_dir) &&
-                               (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)){
-                       m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI + m_prop.automatic_face_movement_dir_offset;
+                               (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
+               {
+                       float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
+                                       + m_prop.automatic_face_movement_dir_offset;
+                       float max_rotation_delta =
+                                       dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
+
+                       if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
+                               (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
+
+                               m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
+                       } else {
+                               m_yaw = optimal_yaw;
+                       }
                }
        }
 
index 3cec51672a99713e3b31513910b983930414e55e..abd1bbd0929361619ff264ee4ca90aaecd5a017f 100644 (file)
@@ -45,7 +45,8 @@ ObjectProperties::ObjectProperties():
        automatic_face_movement_dir_offset(0.0),
        backface_culling(true),
        nametag(""),
-       nametag_color(255, 255, 255, 255)
+       nametag_color(255, 255, 255, 255),
+       automatic_face_movement_max_rotation_per_sec(-1)
 {
        textures.push_back("unknown_object.png");
        colors.push_back(video::SColor(255,255,255,255));
@@ -116,6 +117,8 @@ void ObjectProperties::serialize(std::ostream &os) const
        writeU8(os, backface_culling);
        os << serializeString(nametag);
        writeARGB8(os, nametag_color);
+       writeF1000(os, automatic_face_movement_max_rotation_per_sec);
+
        // Add stuff only at the bottom.
        // Never remove anything, because we don't want new versions of this
 }
@@ -155,6 +158,7 @@ void ObjectProperties::deSerialize(std::istream &is)
                        backface_culling = readU8(is);
                        nametag = deSerializeString(is);
                        nametag_color = readARGB8(is);
+                       automatic_face_movement_max_rotation_per_sec = readF1000(is);
                }catch(SerializationError &e){}
        }
        else
index 5dd3f57a14c2b4ae4bfe48a541fcc3f099ec6834..47698cff7620eb53cb70c55358ef08b36976a503 100644 (file)
@@ -50,7 +50,7 @@ struct ObjectProperties
        bool backface_culling;
        std::string nametag;
        video::SColor nametag_color;
-
+       f32 automatic_face_movement_max_rotation_per_sec;
 
        ObjectProperties();
        std::string dump();
index 4d277ec592505c82abfd330689e525db92ca0016..f9370c4dc6984ca9e6acacf5172658886912b985 100644 (file)
@@ -201,6 +201,7 @@ void read_object_properties(lua_State *L, int index,
        }
        lua_pop(L, 1);
        getboolfield(L, -1, "backface_culling", prop->backface_culling);
+
        getstringfield(L, -1, "nametag", prop->nametag);
        lua_getfield(L, -1, "nametag_color");
        if (!lua_isnil(L, -1)) {
@@ -208,6 +209,11 @@ void read_object_properties(lua_State *L, int index,
                if (read_color(L, -1, &color))
                        prop->nametag_color = color;
        }
+
+       lua_getfield(L, -1, "automatic_face_movement_max_rotation_per_sec");
+       if (lua_isnumber(L, -1)) {
+               prop->automatic_face_movement_max_rotation_per_sec = luaL_checknumber(L, -1);
+       }
        lua_pop(L, 1);
 }