Skip to content

Commit

Permalink
Fix animation frame_speed and blend loosing precision due to incorrec… (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
sapier authored and nerzhul committed Sep 1, 2017
1 parent b8f06ad commit bf403b9
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 5 deletions.
3 changes: 2 additions & 1 deletion doc/lua_api.txt
Expand Up @@ -3304,8 +3304,9 @@ This is basically a reference to a C++ `ServerActiveObject`
* `set_wielded_item(item)`: replaces the wielded item, returns `true` if successful
* `set_armor_groups({group1=rating, group2=rating, ...})`
* `get_armor_groups()`: returns a table with the armor group ratings
* `set_animation({x=1,y=1}, frame_speed=15, frame_blend=0, frame_loop=true)`
* `set_animation({x=1,y=1}, frame_speed=15.0, frame_blend=0, frame_loop=true)`
* `get_animation()`: returns `range`, `frame_speed`, `frame_blend` and `frame_loop`
* `set_animation_frame_speed(frame_speed=15.0)`
* `set_attach(parent, bone, position, rotation)`
* `bone`: string
* `position`: `{x=num, y=num, z=num}` (relative)
Expand Down
13 changes: 12 additions & 1 deletion src/content_cao.cpp
Expand Up @@ -1150,9 +1150,17 @@ void GenericCAO::updateAnimation()
#endif
}

void GenericCAO::updateAnimationSpeed()
{
if (!m_animated_meshnode)
return;

m_animated_meshnode->setAnimationSpeed(m_animation_speed);
}

void GenericCAO::updateBonePosition()
{
if(m_bone_position.empty() || !m_animated_meshnode)
if (m_bone_position.empty() || !m_animated_meshnode)
return;

m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
Expand Down Expand Up @@ -1351,6 +1359,9 @@ void GenericCAO::processMessage(const std::string &data)
updateAnimation();
}
}
} else if (cmd == GENERIC_CMD_SET_ANIMATION_SPEED) {
m_animation_speed = readF1000(is);
updateAnimationSpeed();
} else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
std::string bone = deSerializeString(is);
v3f position = readV3F1000(is);
Expand Down
6 changes: 4 additions & 2 deletions src/content_cao.h
Expand Up @@ -83,8 +83,8 @@ class GenericCAO : public ClientActiveObject
bool m_initial_tx_basepos_set = false;
bool m_tx_select_horiz_by_yawpitch = false;
v2s32 m_animation_range;
int m_animation_speed = 15;
int m_animation_blend = 0;
float m_animation_speed = 15.0f;
float m_animation_blend = 0.0f;
bool m_animation_loop = true;
// stores position and rotation for each bone name
std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
Expand Down Expand Up @@ -197,6 +197,8 @@ class GenericCAO : public ClientActiveObject

void updateAnimation();

void updateAnimationSpeed();

void updateBonePosition();

void updateAttachments();
Expand Down
14 changes: 14 additions & 0 deletions src/content_sao.cpp
Expand Up @@ -155,6 +155,12 @@ void UnitSAO::getAnimation(v2f *frame_range, float *frame_speed, float *frame_bl
*frame_loop = m_animation_loop;
}

void UnitSAO::setAnimationSpeed(float frame_speed)
{
m_animation_speed = frame_speed;
m_animation_speed_sent = false;
}

void UnitSAO::setBonePosition(const std::string &bone, v3f position, v3f rotation)
{
// store these so they can be updated to clients
Expand Down Expand Up @@ -440,6 +446,14 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
m_messages_out.push(aom);
}

if (!m_animation_speed_sent) {
m_animation_speed_sent = true;
std::string str = gob_cmd_update_animation_speed(m_animation_speed);
// create message and add to list
ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push(aom);
}

if (!m_bone_position_sent) {
m_bone_position_sent = true;
for (std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
Expand Down
2 changes: 2 additions & 0 deletions src/content_sao.h
Expand Up @@ -46,6 +46,7 @@ class UnitSAO: public ServerActiveObject
const ItemGroupList &getArmorGroups();
void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
void setAnimationSpeed(float frame_speed);
void setBonePosition(const std::string &bone, v3f position, v3f rotation);
void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
Expand All @@ -70,6 +71,7 @@ class UnitSAO: public ServerActiveObject
float m_animation_blend = 0.0f;
bool m_animation_loop = true;
bool m_animation_sent = false;
bool m_animation_speed_sent = false;

// Stores position and rotation for each bone name
std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
Expand Down
10 changes: 10 additions & 0 deletions src/genericobject.cpp
Expand Up @@ -147,6 +147,16 @@ std::string gob_cmd_update_animation(v2f frames, float frame_speed, float frame_
return os.str();
}

std::string gob_cmd_update_animation_speed(float frame_speed)
{
std::ostringstream os(std::ios::binary);
// command
writeU8(os, GENERIC_CMD_SET_ANIMATION_SPEED);
// parameters
writeF1000(os, frame_speed);
return os.str();
}

std::string gob_cmd_update_bone_position(const std::string &bone, v3f position,
v3f rotation)
{
Expand Down
5 changes: 4 additions & 1 deletion src/genericobject.h
Expand Up @@ -36,7 +36,8 @@ enum GenericCMD {
GENERIC_CMD_ATTACH_TO,
GENERIC_CMD_SET_PHYSICS_OVERRIDE,
GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES,
GENERIC_CMD_SPAWN_INFANT
GENERIC_CMD_SPAWN_INFANT,
GENERIC_CMD_SET_ANIMATION_SPEED
};

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

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

std::string gob_cmd_update_animation_speed(float frame_speed);

std::string gob_cmd_update_bone_position(const std::string &bone, v3f position,
v3f rotation);

Expand Down
21 changes: 21 additions & 0 deletions src/script/lua_api/l_object.cpp
Expand Up @@ -604,6 +604,26 @@ int ObjectRef::l_get_eye_offset(lua_State *L)
return 2;
}

// set_animation_frame_speed(self, frame_speed)
int ObjectRef::l_set_animation_frame_speed(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if (co == NULL)
return 0;

// Do it
if (!lua_isnil(L, 2)) {
float frame_speed = lua_tonumber(L, 2);
co->setAnimationSpeed(frame_speed);
lua_pushboolean(L, true);
} else {
lua_pushboolean(L, false);
}
return 1;
}

// set_bone_position(self, std::string bone, v3f position, v3f rotation)
int ObjectRef::l_set_bone_position(lua_State *L)
{
Expand Down Expand Up @@ -1937,6 +1957,7 @@ const luaL_Reg ObjectRef::methods[] = {
luamethod(ObjectRef, get_armor_groups),
luamethod(ObjectRef, set_animation),
luamethod(ObjectRef, get_animation),
luamethod(ObjectRef, set_animation_frame_speed),
luamethod(ObjectRef, set_bone_position),
luamethod(ObjectRef, get_bone_position),
luamethod(ObjectRef, set_attach),
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_object.h
Expand Up @@ -126,6 +126,9 @@ class ObjectRef : public ModApiBase {
// set_animation(self, frame_range, frame_speed, frame_blend, frame_loop)
static int l_set_animation(lua_State *L);

// set_animation_frame_speed(self, frame_speed)
static int l_set_animation_frame_speed(lua_State *L);

// get_animation(self)
static int l_get_animation(lua_State *L);

Expand Down
2 changes: 2 additions & 0 deletions src/serverobject.h
Expand Up @@ -154,6 +154,8 @@ class ServerActiveObject : public ActiveObject
{}
virtual void getAnimation(v2f *frames, float *frame_speed, float *frame_blend, bool *frame_loop)
{}
virtual void setAnimationSpeed(float frame_speed)
{}
virtual void setBonePosition(const std::string &bone, v3f position, v3f rotation)
{}
virtual void getBonePosition(const std::string &bone, v3f *position, v3f *lotation)
Expand Down

0 comments on commit bf403b9

Please sign in to comment.