Skip to content

Commit

Permalink
feat(transform): propagate LocalToWorld
Browse files Browse the repository at this point in the history
  • Loading branch information
luishfonseca committed Feb 1, 2024
1 parent f4078a8 commit 3e4423d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
4 changes: 3 additions & 1 deletion engine/include/cubos/engine/transform/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ namespace cubos::engine
/// Position, @ref Rotation or @ref Scale components.
/// - `cubos.transform.missing` - the @ref Position, @ref Rotation, @ref Scale and possibly @ref LocalToParent
/// components are added to entities with @ref LocalToWorld components.
/// - `cubos.transform.update.relative` - the @ref LocalToWorld or @ref LocalToParent components are updated with
/// the information from the @ref Position, @ref Rotation and @ref Scale components.
/// - `cubos.transform.update` - the @ref LocalToWorld components are updated with the information from the @ref
/// Position, @ref Rotation and @ref Scale components.
/// LocalToParent component and the @ref LocalToWorld components of the parent.

/// @brief Plugin entry function.
/// @param cubos @b CUBOS. main class
Expand Down
43 changes: 28 additions & 15 deletions engine/src/cubos/engine/transform/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <cubos/engine/transform/plugin.hpp>

using cubos::core::ecs::Traversal;

void cubos::engine::transformPlugin(Cubos& cubos)
{
cubos.addComponent<Position>();
Expand Down Expand Up @@ -91,28 +93,39 @@ void cubos::engine::transformPlugin(Cubos& cubos)
}
});

cubos.tag("cubos.transform.missing").before("cubos.transform.update");
cubos.tag("cubos.transform.missing").before("cubos.transform.update.relative");

cubos.system("update LocalToWorld")
.tagged("cubos.transform.update")
.call([](Query<Entity, LocalToWorld&, Opt<const Position&>, Opt<const Rotation&>, Opt<const Scale&>> query) {
for (auto [entity, localToWorld, position, rotation, scale] : query)
cubos.system("update relative transform matrix")
.tagged("cubos.transform.update.relative")
.before("cubos.transform.update")
.call([](Query<LocalToWorld&, Opt<LocalToParent&>, const Position&, const Rotation&, const Scale&> query) {
for (auto [localToWorld, localToParent, position, rotation, scale] : query)
{
localToWorld.mat = glm::mat4(1.0F);
if (position)
{
localToWorld.mat = glm::translate(localToWorld.mat, position->vec);
}
auto mat = glm::scale(glm::translate(localToWorld.mat, position.vec) * glm::toMat4(rotation.quat),
glm::vec3(scale.factor));

if (rotation)
if (localToParent)
{
localToWorld.mat *= glm::toMat4(rotation->quat);
localToParent->mat = mat;
}

if (scale)
else
{
localToWorld.mat = glm::scale(localToWorld.mat, glm::vec3(scale->factor));
// No parent, so transform is relative to the world
localToWorld.mat = mat;
}
}
});

cubos.system("update LocalToWorlds of children")
.tagged("cubos.transform.update")
.with<LocalToWorld>()
.with<LocalToParent>()
.related<ChildOf>(Traversal::Down)
.with<LocalToWorld>()
.call([](Query<LocalToWorld&, const LocalToParent&, const LocalToWorld&> query) {
for (auto [localToWorld, localToParent, parentLocalToWorld] : query)
{
localToWorld.mat = parentLocalToWorld.mat * localToParent.mat;
}
});
}

0 comments on commit 3e4423d

Please sign in to comment.