add Transform.dirty flag

This commit is contained in:
MihailRis 2024-06-30 19:19:43 +03:00
parent df39a9a8f3
commit d4460928e5
5 changed files with 29 additions and 10 deletions

View File

@ -22,7 +22,8 @@ end
function on_update() function on_update()
if inair then if inair then
tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 1, 0}, math.random()*4)) local dt = time.delta();
tsf:set_rot(mat4.rotate(tsf:get_rot(), {0, 0, 1}, math.random()*4)) 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
end end

View File

@ -9,9 +9,9 @@ function on_hud_open()
local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0}) local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0})
local throw_force = vec3.mul(vec3.add(player.get_dir(pid), local throw_force = vec3.mul(vec3.add(player.get_dir(pid),
{ {
(math.random() - 0.5) * 5, (math.random() - 0.5) * 1,
(math.random() - 0.5) * 5, (math.random() - 0.5) * 1,
(math.random() - 0.5) * 5 (math.random() - 0.5) * 1
}), DROP_FORCE) }), DROP_FORCE)
local drop = entity.spawn("base:drop", ppos) local drop = entity.spawn("base:drop", ppos)

View File

@ -51,7 +51,7 @@ static int l_get_pos(lua::State* L) {
static int l_set_pos(lua::State* L) { static int l_set_pos(lua::State* L) {
if (auto entity = get_entity(L, 1)) { if (auto entity = get_entity(L, 1)) {
entity->getTransform().pos = lua::tovec3(L, 2); entity->getTransform().setPos(lua::tovec3(L, 2));
} }
return 0; return 0;
} }
@ -79,7 +79,7 @@ static int l_get_rot(lua::State* L) {
static int l_set_rot(lua::State* L) { static int l_set_rot(lua::State* L) {
if (auto entity = get_entity(L, 1)) { if (auto entity = get_entity(L, 1)) {
entity->getTransform().rot = lua::tomat4(L, 2); entity->getTransform().setRot(lua::tomat4(L, 2));
} }
return 0; return 0;
} }

View File

@ -18,6 +18,7 @@ void Transform::refresh() {
combined = glm::translate(combined, pos); combined = glm::translate(combined, pos);
combined = glm::scale(combined, size); combined = glm::scale(combined, size);
combined = combined * glm::mat4(rot); combined = combined * glm::mat4(rot);
dirty = false;
} }
void Entity::destroy() { void Entity::destroy() {
@ -136,7 +137,7 @@ void Entities::updatePhysics(float delta) {
eid.uid eid.uid
); );
hitbox.linearDamping = hitbox.grounded * 24; hitbox.linearDamping = hitbox.grounded * 24;
transform.pos = hitbox.position; transform.setPos(hitbox.position);
if (hitbox.grounded && !grounded) { if (hitbox.grounded && !grounded) {
scripting::on_entity_grounded(*get(eid.uid), glm::length(prevVel-hitbox.velocity)); scripting::on_entity_grounded(*get(eid.uid), glm::length(prevVel-hitbox.velocity));
} }
@ -150,8 +151,10 @@ void Entities::update() {
scripting::on_entities_update(); scripting::on_entities_update();
auto view = registry.view<Transform>(); auto view = registry.view<Transform>();
for (auto [entity, transform] : view.each()) { for (auto [entity, transform] : view.each()) {
if (transform.dirty) {
transform.refresh(); transform.refresh();
} }
}
} }
void Entities::renderDebug(LineBatch& batch, const Frustum& frustum) { void Entities::renderDebug(LineBatch& batch, const Frustum& frustum) {

View File

@ -7,6 +7,8 @@
#include <vector> #include <vector>
#include <optional> #include <optional>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/norm.hpp>
#include <unordered_map> #include <unordered_map>
#include <entt/entity/registry.hpp> #include <entt/entity/registry.hpp>
@ -32,8 +34,21 @@ struct Transform {
glm::vec3 size; glm::vec3 size;
glm::mat3 rot; glm::mat3 rot;
glm::mat4 combined; glm::mat4 combined;
bool dirty = true;
void refresh(); 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 { struct Rigidbody {