diff --git a/data/scripts/actors/actor_player.lua b/data/scripts/actors/actor_player.lua index 612c089..662f111 100644 --- a/data/scripts/actors/actor_player.lua +++ b/data/scripts/actors/actor_player.lua @@ -67,23 +67,6 @@ function actor_player:action_update() end end --- TODO: remove -function vec3_magnitude(_x, _y, _z) - return math.sqrt(_x * _x + _y * _y + _z * _z) -end - -function vec3_normalize(_x, _y, _z) - local mag = vec3_magnitude(_x, _y, _z) - if mag == 0 then return 0, 0, 0 end - return _x / mag, _y / mag, _z / mag -end - -function vec3_cross(_x1, _y1, _z1, _x2, _y2, _z2) - return _y1 * _z2 - _y2 * _z1, - _z1 * _x2 - _z2 * _x1, - _x1 * _y2 - _x2 * _y1 -end - function actor_player:update_player_movement(dt) local movement = self:get_movement() local speed = 4.0 diff --git a/data/scripts/game_init.lua b/data/scripts/game_init.lua index 84c5e88..adfbd54 100644 --- a/data/scripts/game_init.lua +++ b/data/scripts/game_init.lua @@ -1,6 +1,7 @@ -- Game initialization script -- загружаем скрипты +load_script("mathlib.lua") load_script("game_utils.lua") load_script("game_hud.lua") load_script("game_object.lua") diff --git a/data/scripts/mathlib.lua b/data/scripts/mathlib.lua new file mode 100644 index 0000000..5166a78 --- /dev/null +++ b/data/scripts/mathlib.lua @@ -0,0 +1,17 @@ +-- math library + +function vec3_magnitude(_x, _y, _z) + return math.sqrt(_x * _x + _y * _y + _z * _z) +end + +function vec3_normalize(_x, _y, _z) + local mag = vec3_magnitude(_x, _y, _z) + if mag == 0 then return 0, 0, 0 end + return _x / mag, _y / mag, _z / mag +end + +function vec3_cross(_x1, _y1, _z1, _x2, _y2, _z2) + return _y1 * _z2 - _y2 * _z1, + _z1 * _x2 - _z2 * _x1, + _x1 * _y2 - _x2 * _y1 +end diff --git a/docs/lua help.txt b/docs/lua help.txt index 3752117..9923bca 100644 --- a/docs/lua help.txt +++ b/docs/lua help.txt @@ -87,6 +87,7 @@ get_velocity() -- return x, y, z has_rigid_body() -- return boolean create_box_body(float sizex, float sizey, float sizez, float mass, float friction, float damping, bool is_trigger, bool use_parameters) create_sphere_body(float radius, float mass, float friction, float damping, bool is_trigger, bool use_parameters) +ForceBodyTransformUpdate() -- hack for forcing entity world transform to the rigid body find_animation(string name) -- find an animation in the model diff --git a/src/game/game_object.cpp b/src/game/game_object.cpp index 138b730..85a7375 100644 --- a/src/game/game_object.cpp +++ b/src/game/game_object.cpp @@ -222,6 +222,7 @@ void Entity::RegisterBaseFunctions() m_luaObject.Register("has_rigid_body", *this, &Entity::Lua_HasRigidBody); m_luaObject.Register("create_box_body", *this, &Entity::Lua_CreateBoxBody); m_luaObject.Register("create_sphere_body", *this, &Entity::Lua_CreateSphereBody); + m_luaObject.Register("ForceBodyTransformUpdate", *this, &Entity::Lua_ForceBodyTransformUpdate); // animation m_luaObject.Register("find_animation", *this, &Entity::Lua_FindAnimation); @@ -487,6 +488,13 @@ int Entity::Lua_CreateSphereBody(LuaPlus::LuaState* state) return 0; } +int Entity::Lua_ForceBodyTransformUpdate(LuaPlus::LuaState* state) +{ + ForceUpdateBodyTranslation(); + + return 0; +} + int Entity::Lua_FindAnimation(LuaPlus::LuaState* state) { LuaPlus::LuaStack stack(state); @@ -692,6 +700,18 @@ void Entity::UpdateBodyDirty() } } +void Entity::ForceUpdateBodyTranslation() +{ + if (!m_rigidBody) + return; + + // I'm sure that position is valid + btTransform xform; + xform.setIdentity(); + xform.setOrigin(glmVectorToBt(m_position)); + m_rigidBody->setWorldTransform(xform); +} + REGISTER_ENTITY(WeaponBase); WeaponBase::WeaponBase() diff --git a/src/game/game_object.h b/src/game/game_object.h index 6998c8e..99229cb 100644 --- a/src/game/game_object.h +++ b/src/game/game_object.h @@ -91,6 +91,7 @@ public: void UpdateBody(); void UpdateBodyDirty(); + void ForceUpdateBodyTranslation(); // Game entity lua bindings @@ -125,6 +126,7 @@ public: int Lua_HasRigidBody(LuaPlus::LuaState* state); int Lua_CreateBoxBody(LuaPlus::LuaState* state); int Lua_CreateSphereBody(LuaPlus::LuaState* state); + int Lua_ForceBodyTransformUpdate(LuaPlus::LuaState* state); int Lua_FindAnimation(LuaPlus::LuaState* state); int Lua_PlayAnimation(LuaPlus::LuaState* state);