Skip to content

Commit bf403b9

Browse files
sapiernerzhul
authored andcommittedSep 1, 2017
Fix animation frame_speed and blend loosing precision due to incorrec… (#6357)
* Fix animation frame_speed and blend loosing precision due to incorrect data type Add lua function set_animation_frame_speed to update the frame speed without resetting the animation to start
1 parent b8f06ad commit bf403b9

10 files changed

+74
-5
lines changed
 

‎doc/lua_api.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -3304,8 +3304,9 @@ This is basically a reference to a C++ `ServerActiveObject`
33043304
* `set_wielded_item(item)`: replaces the wielded item, returns `true` if successful
33053305
* `set_armor_groups({group1=rating, group2=rating, ...})`
33063306
* `get_armor_groups()`: returns a table with the armor group ratings
3307-
* `set_animation({x=1,y=1}, frame_speed=15, frame_blend=0, frame_loop=true)`
3307+
* `set_animation({x=1,y=1}, frame_speed=15.0, frame_blend=0, frame_loop=true)`
33083308
* `get_animation()`: returns `range`, `frame_speed`, `frame_blend` and `frame_loop`
3309+
* `set_animation_frame_speed(frame_speed=15.0)`
33093310
* `set_attach(parent, bone, position, rotation)`
33103311
* `bone`: string
33113312
* `position`: `{x=num, y=num, z=num}` (relative)

‎src/content_cao.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -1150,9 +1150,17 @@ void GenericCAO::updateAnimation()
11501150
#endif
11511151
}
11521152

1153+
void GenericCAO::updateAnimationSpeed()
1154+
{
1155+
if (!m_animated_meshnode)
1156+
return;
1157+
1158+
m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1159+
}
1160+
11531161
void GenericCAO::updateBonePosition()
11541162
{
1155-
if(m_bone_position.empty() || !m_animated_meshnode)
1163+
if (m_bone_position.empty() || !m_animated_meshnode)
11561164
return;
11571165

11581166
m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
@@ -1351,6 +1359,9 @@ void GenericCAO::processMessage(const std::string &data)
13511359
updateAnimation();
13521360
}
13531361
}
1362+
} else if (cmd == GENERIC_CMD_SET_ANIMATION_SPEED) {
1363+
m_animation_speed = readF1000(is);
1364+
updateAnimationSpeed();
13541365
} else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
13551366
std::string bone = deSerializeString(is);
13561367
v3f position = readV3F1000(is);

‎src/content_cao.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ class GenericCAO : public ClientActiveObject
8383
bool m_initial_tx_basepos_set = false;
8484
bool m_tx_select_horiz_by_yawpitch = false;
8585
v2s32 m_animation_range;
86-
int m_animation_speed = 15;
87-
int m_animation_blend = 0;
86+
float m_animation_speed = 15.0f;
87+
float m_animation_blend = 0.0f;
8888
bool m_animation_loop = true;
8989
// stores position and rotation for each bone name
9090
std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
@@ -197,6 +197,8 @@ class GenericCAO : public ClientActiveObject
197197

198198
void updateAnimation();
199199

200+
void updateAnimationSpeed();
201+
200202
void updateBonePosition();
201203

202204
void updateAttachments();

‎src/content_sao.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ void UnitSAO::getAnimation(v2f *frame_range, float *frame_speed, float *frame_bl
155155
*frame_loop = m_animation_loop;
156156
}
157157

158+
void UnitSAO::setAnimationSpeed(float frame_speed)
159+
{
160+
m_animation_speed = frame_speed;
161+
m_animation_speed_sent = false;
162+
}
163+
158164
void UnitSAO::setBonePosition(const std::string &bone, v3f position, v3f rotation)
159165
{
160166
// store these so they can be updated to clients
@@ -440,6 +446,14 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
440446
m_messages_out.push(aom);
441447
}
442448

449+
if (!m_animation_speed_sent) {
450+
m_animation_speed_sent = true;
451+
std::string str = gob_cmd_update_animation_speed(m_animation_speed);
452+
// create message and add to list
453+
ActiveObjectMessage aom(getId(), true, str);
454+
m_messages_out.push(aom);
455+
}
456+
443457
if (!m_bone_position_sent) {
444458
m_bone_position_sent = true;
445459
for (std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator

‎src/content_sao.h

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class UnitSAO: public ServerActiveObject
4646
const ItemGroupList &getArmorGroups();
4747
void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
4848
void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
49+
void setAnimationSpeed(float frame_speed);
4950
void setBonePosition(const std::string &bone, v3f position, v3f rotation);
5051
void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
5152
void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
@@ -70,6 +71,7 @@ class UnitSAO: public ServerActiveObject
7071
float m_animation_blend = 0.0f;
7172
bool m_animation_loop = true;
7273
bool m_animation_sent = false;
74+
bool m_animation_speed_sent = false;
7375

7476
// Stores position and rotation for each bone name
7577
std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;

‎src/genericobject.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ std::string gob_cmd_update_animation(v2f frames, float frame_speed, float frame_
147147
return os.str();
148148
}
149149

150+
std::string gob_cmd_update_animation_speed(float frame_speed)
151+
{
152+
std::ostringstream os(std::ios::binary);
153+
// command
154+
writeU8(os, GENERIC_CMD_SET_ANIMATION_SPEED);
155+
// parameters
156+
writeF1000(os, frame_speed);
157+
return os.str();
158+
}
159+
150160
std::string gob_cmd_update_bone_position(const std::string &bone, v3f position,
151161
v3f rotation)
152162
{

‎src/genericobject.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ enum GenericCMD {
3636
GENERIC_CMD_ATTACH_TO,
3737
GENERIC_CMD_SET_PHYSICS_OVERRIDE,
3838
GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES,
39-
GENERIC_CMD_SPAWN_INFANT
39+
GENERIC_CMD_SPAWN_INFANT,
40+
GENERIC_CMD_SET_ANIMATION_SPEED
4041
};
4142

4243
#include "object_properties.h"
@@ -72,6 +73,8 @@ std::string gob_cmd_update_physics_override(float physics_override_speed,
7273

7374
std::string gob_cmd_update_animation(v2f frames, float frame_speed, float frame_blend, bool frame_loop);
7475

76+
std::string gob_cmd_update_animation_speed(float frame_speed);
77+
7578
std::string gob_cmd_update_bone_position(const std::string &bone, v3f position,
7679
v3f rotation);
7780

‎src/script/lua_api/l_object.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,26 @@ int ObjectRef::l_get_eye_offset(lua_State *L)
604604
return 2;
605605
}
606606

607+
// set_animation_frame_speed(self, frame_speed)
608+
int ObjectRef::l_set_animation_frame_speed(lua_State *L)
609+
{
610+
NO_MAP_LOCK_REQUIRED;
611+
ObjectRef *ref = checkobject(L, 1);
612+
ServerActiveObject *co = getobject(ref);
613+
if (co == NULL)
614+
return 0;
615+
616+
// Do it
617+
if (!lua_isnil(L, 2)) {
618+
float frame_speed = lua_tonumber(L, 2);
619+
co->setAnimationSpeed(frame_speed);
620+
lua_pushboolean(L, true);
621+
} else {
622+
lua_pushboolean(L, false);
623+
}
624+
return 1;
625+
}
626+
607627
// set_bone_position(self, std::string bone, v3f position, v3f rotation)
608628
int ObjectRef::l_set_bone_position(lua_State *L)
609629
{
@@ -1937,6 +1957,7 @@ const luaL_Reg ObjectRef::methods[] = {
19371957
luamethod(ObjectRef, get_armor_groups),
19381958
luamethod(ObjectRef, set_animation),
19391959
luamethod(ObjectRef, get_animation),
1960+
luamethod(ObjectRef, set_animation_frame_speed),
19401961
luamethod(ObjectRef, set_bone_position),
19411962
luamethod(ObjectRef, get_bone_position),
19421963
luamethod(ObjectRef, set_attach),

‎src/script/lua_api/l_object.h

+3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ class ObjectRef : public ModApiBase {
126126
// set_animation(self, frame_range, frame_speed, frame_blend, frame_loop)
127127
static int l_set_animation(lua_State *L);
128128

129+
// set_animation_frame_speed(self, frame_speed)
130+
static int l_set_animation_frame_speed(lua_State *L);
131+
129132
// get_animation(self)
130133
static int l_get_animation(lua_State *L);
131134

‎src/serverobject.h

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ class ServerActiveObject : public ActiveObject
154154
{}
155155
virtual void getAnimation(v2f *frames, float *frame_speed, float *frame_blend, bool *frame_loop)
156156
{}
157+
virtual void setAnimationSpeed(float frame_speed)
158+
{}
157159
virtual void setBonePosition(const std::string &bone, v3f position, v3f rotation)
158160
{}
159161
virtual void getBonePosition(const std::string &bone, v3f *position, v3f *lotation)

0 commit comments

Comments
 (0)
Please sign in to comment.