diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index d432a2fb..6c8c4a1a 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -22,7 +22,8 @@ end function on_update() if inair then - tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 1, 0}, math.random()*4)) - tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 0, 1}, math.random()*4)) + 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)) end end diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index 78027e7f..8245a22a 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -9,9 +9,9 @@ function on_hud_open() local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0}) local throw_force = vec3.mul(vec3.add(player.get_dir(pid), { - (math.random() - 0.5) * 5, - (math.random() - 0.5) * 5, - (math.random() - 0.5) * 5 + (math.random() - 0.5) * 1, + (math.random() - 0.5) * 1, + (math.random() - 0.5) * 1 }), DROP_FORCE) local drop = entity.spawn("base:drop", ppos) diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index 326e4da7..11b16e2c 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -51,7 +51,7 @@ static int l_get_pos(lua::State* L) { static int l_set_pos(lua::State* L) { if (auto entity = get_entity(L, 1)) { - entity->getTransform().pos = lua::tovec3(L, 2); + entity->getTransform().setPos(lua::tovec3(L, 2)); } return 0; } @@ -79,7 +79,7 @@ static int l_get_rot(lua::State* L) { static int l_set_rot(lua::State* L) { if (auto entity = get_entity(L, 1)) { - entity->getTransform().rot = lua::tomat4(L, 2); + entity->getTransform().setRot(lua::tomat4(L, 2)); } return 0; } diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index f43c25a4..c730a128 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -18,6 +18,7 @@ void Transform::refresh() { combined = glm::translate(combined, pos); combined = glm::scale(combined, size); combined = combined * glm::mat4(rot); + dirty = false; } void Entity::destroy() { @@ -136,7 +137,7 @@ void Entities::updatePhysics(float delta) { eid.uid ); hitbox.linearDamping = hitbox.grounded * 24; - transform.pos = hitbox.position; + transform.setPos(hitbox.position); if (hitbox.grounded && !grounded) { scripting::on_entity_grounded(*get(eid.uid), glm::length(prevVel-hitbox.velocity)); } @@ -150,7 +151,9 @@ void Entities::update() { scripting::on_entities_update(); auto view = registry.view(); for (auto [entity, transform] : view.each()) { - transform.refresh(); + if (transform.dirty) { + transform.refresh(); + } } } diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 0860e6a4..289849fe 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -7,6 +7,8 @@ #include #include #include +#define GLM_ENABLE_EXPERIMENTAL +#include #include #include @@ -32,8 +34,21 @@ struct Transform { glm::vec3 size; glm::mat3 rot; glm::mat4 combined; + bool dirty = true; void refresh(); + + inline void setRot(glm::mat3 m) { + rot = m; + dirty = true; + } + + inline void setPos(glm::vec3 v) { + if (glm::distance2(pos, v) >= 0.00001f) { + dirty = true; + } + pos = v; + } }; struct Rigidbody { @@ -104,7 +119,7 @@ class Entities { Level* level; std::unordered_map entities; entityid_t nextID = 1; - + void preparePhysics(); public: Entities(Level* level);