diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index 3ce24f95..287312d2 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -5,8 +5,14 @@ local rig = entity.modeltree inair = true ready = false + +local rotation = mat4.rotate({0, 1, 0}, math.random() * 360) +mat4.rotate(rotation, {1, 0, 0}, math.random() * 360, rotation) +mat4.rotate(rotation, {0, 0, 1}, math.random() * 360, rotation) +rig:set_matrix(0, rotation) + function on_grounded(force) - tsf:set_rot(mat4.rotate({0, 1, 0}, math.random()*360)) + rig:set_matrix(0, mat4.rotate({0, 1, 0}, math.random()*360)) inair = false ready = true end @@ -25,7 +31,9 @@ end function on_update() if inair then local dt = time.delta(); - tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 1, 0}, 240*dt)) - tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 0, 1}, 240*dt)) + local matrix = rig:get_matrix(0) + mat4.rotate(matrix, {0, 1, 0}, 240*dt, matrix) + mat4.rotate(matrix, {0, 0, 1}, 240*dt, matrix) + rig:set_matrix(0, matrix) end end diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index 22d03672..4ad429b5 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -16,8 +16,6 @@ function on_hud_open() local drop = entities.spawn("base:drop", ppos) drop.rigidbody:set_vel(vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))) - drop.transform:set_rot(mat4.rotate(mat4.rotate(mat4.rotate({0, 1, 0}, math.random() * 360), - {1, 0, 0}, math.random() * 360), {0, 0, 1}, math.random() * 360)) end end) end diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index 0719d932..b6e6f600 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -24,7 +24,9 @@ function new_Rigidbody(eid) end local Modeltree = {__index={ - get_model=function(self) return __modeltree.get_model(self.eid) end, + get_model=function(self, i) return __modeltree.get_model(self.eid, i) end, + get_matrix=function(self, i) return __modeltree.get_matrix(self.eid, i) end, + set_matrix=function(self, i, m) return __modeltree.set_matrix(self.eid, i, m) end, }} function new_Modeltree(eid) diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index db858742..8f1dbf6a 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -111,16 +111,43 @@ static int l_get_size(lua::State* L) { return 0; } +static int index_range_check(const rigging::Rig& rig, lua::Integer index) { + if (static_cast(index) >= rig.pose.matrices.size()) { + throw std::runtime_error("index out of range [0, " + + std::to_string(rig.pose.matrices.size()) + + "]"); + } + return static_cast(index); +} + static int l_modeltree_get_model(lua::State* L) { if (auto entity = get_entity(L, 1)) { auto& rig = entity->getModeltree(); auto* rigConfig = rig.config; - auto index = lua::tointeger(L, 2); + auto index = index_range_check(rig, lua::tointeger(L, 2)); return lua::pushstring(L, rigConfig->getNodes()[index]->getModelName()); } return 0; } +static int l_modeltree_get_matrix(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + auto& rig = entity->getModeltree(); + auto index = index_range_check(rig, lua::tointeger(L, 2)); + return lua::pushmat4(L, rig.pose.matrices[index]); + } + return 0; +} + +static int l_modeltree_set_matrix(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + auto& rig = entity->getModeltree(); + auto index = index_range_check(rig, lua::tointeger(L, 2)); + rig.pose.matrices[index] = lua::tomat4(L, 3); + } + return 0; +} + const luaL_Reg entitylib [] = { {"exists", lua::wrap}, {"spawn", lua::wrap}, @@ -130,6 +157,8 @@ const luaL_Reg entitylib [] = { const luaL_Reg modeltreelib [] = { {"get_model", lua::wrap}, + {"get_matrix", lua::wrap}, + {"set_matrix", lua::wrap}, {NULL, NULL} }; diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 00385120..6e024427 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -78,9 +78,9 @@ entityid_t Entities::spawn( auto& scripting = registry.emplace( entity, entity_funcs_set {}, nullptr); entities[id] = entity; + registry.emplace(entity, rig->instance()); scripting.env = scripting::on_entity_spawn( def, id, scripting.funcsset, std::move(args)); - registry.emplace(entity, rig->instance()); return id; } diff --git a/src/objects/rigging.cpp b/src/objects/rigging.cpp index cb96fe71..04a90177 100644 --- a/src/objects/rigging.cpp +++ b/src/objects/rigging.cpp @@ -42,10 +42,11 @@ size_t RigConfig::update( glm::mat4 matrix) const { rig.calculated.matrices[index] = matrix * rig.pose.matrices[index]; + size_t count = 1; for (auto& subnode : node->getSubnodes()) { - index = update(index+1, rig, subnode.get(), rig.calculated.matrices[index]); + count += update(index+count, rig, subnode.get(), rig.calculated.matrices[index]); } - return index; + return count; } void RigConfig::update(Rig& rig, glm::mat4 matrix) const { diff --git a/src/objects/rigging.hpp b/src/objects/rigging.hpp index 6da4bf97..e79bcf01 100644 --- a/src/objects/rigging.hpp +++ b/src/objects/rigging.hpp @@ -1,5 +1,5 @@ -#ifndef OBJECTS_SKELETON_HPP_ -#define OBJECTS_SKELETON_HPP_ +#ifndef OBJECTS_RIGGING_HPP_ +#define OBJECTS_RIGGING_HPP_ #include "../typedefs.hpp" @@ -23,11 +23,8 @@ namespace rigging { struct Pose { std::vector matrices; - Pose(size_t size) : matrices(size) { - matrices.resize(size); - for (size_t i = 0; i < size; i++) { - matrices[i] = glm::mat4(1.0f); - } + Pose(size_t size) { + matrices.resize(size, glm::mat4(1.0f)); } }; @@ -115,4 +112,4 @@ namespace rigging { }; }; -#endif // OBJECTS_SKELETON_HPP_ +#endif // OBJECTS_RIGGING_HPP_