Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(transform): preserve reflected world transforms and UI bounds

yhhdf7c68a7bc3e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bcd2460b7..c81ef3ae8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,8 @@ published separately; it ships inside the editor.
### Fixed
+- **World transforms preserve mirrored axes.** Quaternion decomposition retains signed scale instead of converting reflected bases into invalid rotations. Planar transforms keep a Z rotation, including collapsed axes; mirrored UI pointer bounds and flat render bounds follow their displayed geometry.
+
- **Nine-slice texture flips preserve asymmetric borders.** Flipped UV spans sample inside their original region and exchange opposing border widths, producing the complete mirrored texture. The shared geometry builder retains all cells for signed destination extents, including small boxes and rotated draws.
- **Small nine-slice visuals keep borders inside their box.** Sprite and UI nine-slice geometry proportionally shrinks opposing borders when their total exceeds the destination size, retaining the texture's original border UVs. Zero-width or zero-height nine-slices emit no geometry; ordinary sizes retain their existing border widths.
diff --git a/src/esengine/math/Math.hpp b/src/esengine/math/Math.hpp
index d5a6a599a..65a07f030 100644
--- a/src/esengine/math/Math.hpp
+++ b/src/esengine/math/Math.hpp
@@ -294,8 +294,29 @@ inline void decompose(const glm::mat4& matrix, glm::vec3& position, glm::quat& r
scale.y = glm::length(glm::vec3(matrix[1]));
scale.z = glm::length(glm::vec3(matrix[2]));
+ // A planar reflection stays a Z rotation plus signed XY scale. This also
+ // keeps a collapsed axis from producing a non-unit rotation quaternion.
+ if (matrix[0].z == 0.0f && matrix[1].z == 0.0f && matrix[2].x == 0.0f && matrix[2].y == 0.0f) {
+ glm::vec2 direction{1.0f, 0.0f};
+ if (scale.x > EPSILON) {
+ direction = glm::vec2(matrix[0]) / scale.x;
+ if (matrix[0].x * matrix[1].y - matrix[0].y * matrix[1].x < 0.0f) scale.y = -scale.y;
+ } else if (scale.y > EPSILON) {
+ direction = glm::vec2(matrix[1].y, -matrix[1].x) / scale.y;
+ }
+ scale.z = matrix[2].z;
+ rotation = glm::quat_cast(glm::mat3(glm::vec3(direction, 0.0f),
+ glm::vec3(-direction.y, direction.x, 0.0f),
+ glm::vec3(0.0f, 0.0f, 1.0f)));
+ return;
+ }
+
+ // Quaternion conversion requires a proper rotation, not a reflection.
+ // Put the spatial reflection on one scale axis before converting the basis.
+ if (glm::determinant(glm::mat3(matrix)) < 0.0f) scale.x = -scale.x;
+
// Guard zero-scale axes to avoid NaN/Inf in the quaternion.
- const float sx = scale.x > EPSILON ? scale.x : EPSILON;
+ const float sx = std::abs(scale.x) > EPSILON ? scale.x : std::copysign(EPSILON, scale.x);
const float sy = scale.y > EPSILON ? scale.y : EPSILON;
const float sz = scale.z > EPSILON ? scale.z : EPSILON;
diff --git a/src/esengine/renderer/RenderTypePlugin.hpp b/src/esengine/renderer/RenderTypePlugin.hpp
index da39a0ee3..5eed7df18 100644
--- a/src/esengine/renderer/RenderTypePlugin.hpp
+++ b/src/esengine/renderer/RenderTypePlugin.hpp
@@ -397,7 +397,8 @@ inline void orientedWorldAabb(const glm::vec3& position, const glm::quat& rotati
*/
inline glm::vec3 flatHalfExtents(const glm::vec2& turn, const glm::vec3& half) {
const f32 c = std::abs(turn.x), s = std::abs(turn.y);
- return {c * half.x + s * half.y, s * half.x + c * half.y, half.z};
+ const glm::vec3 extent = glm::abs(half);
+ return {c * extent.x + s * extent.y, s * extent.x + c * extent.y, extent.z};
}
class RenderTypePlugin {
diff --git a/src/esengine/renderer/plugins/TextPlugin.cpp b/src/esengine/renderer/plugins/TextPlugin.cpp
index ebbb025a1..91be0331e 100644
--- a/src/esengine/renderer/plugins/TextPlugin.cpp
+++ b/src/esengine/renderer/plugins/TextPlugin.cpp
@@ -157,11 +157,11 @@ void TextPlugin::collect(RenderCollectContext& collect_ctx) {
glm::vec3 position = parallaxedWorldPosition(transform, bt.parallax, collect_ctx.camera);
const f32 s = transform.worldScale.x;
- glm::vec3 halfExtents = glm::vec3(
+ glm::vec3 halfExtents = glm::abs(glm::vec3(
cache.width * s * 0.5f,
cache.height * transform.worldScale.y * 0.5f,
0.0f
- );
+ ));
if (!frustum.intersectsAABB(position, halfExtents)) {
++collect_ctx.culled;
continue;
diff --git a/src/esengine/ui/UIHitTestSystem.hpp b/src/esengine/ui/UIHitTestSystem.hpp
index faa8dcd47..a091efd4e 100644
--- a/src/esengine/ui/UIHitTestSystem.hpp
+++ b/src/esengine/ui/UIHitTestSystem.hpp
@@ -45,7 +45,8 @@ inline bool pointInOBB(
f32 bottom = worldY - worldH * pivotY;
f32 top = worldY + worldH * (1.0f - pivotY);
- return localX >= left && localX <= right && localY >= bottom && localY <= top;
+ return localX >= std::min(left, right) && localX <= std::max(left, right)
+ && localY >= std::min(bottom, top) && localY <= std::max(bottom, top);
}
/**
diff --git a/tests/ecs/test_transform_traversal.cpp b/tests/ecs/test_transform_traversal.cpp
index cc390b43d..0794e5008 100644
--- a/tests/ecs/test_transform_traversal.cpp
+++ b/tests/ecs/test_transform_traversal.cpp
@@ -4,6 +4,59 @@
using namespace esengine;
using namespace esengine::ecs;
+static void checkReconstructedTransform(const glm::mat4& matrix, bool planar) {
+ Transform transform;
+ transform.cachedMatrix_ = matrix;
+ transform.decomposed_ = false;
+ transform.ensureDecomposed();
+ const auto rebuilt = glm::translate(glm::mat4(1), transform.worldPosition)
+ * glm::mat4_cast(transform.worldRotation)
+ * glm::scale(glm::mat4(1), transform.worldScale);
+ CHECK(glm::length(transform.worldRotation) == doctest::Approx(1));
+ if (planar) {
+ CHECK(transform.worldRotation.x == doctest::Approx(0));
+ CHECK(transform.worldRotation.y == doctest::Approx(0));
+ }
+ for (int col = 0; col < 4; ++col) for (int row = 0; row < 4; ++row) {
+ CHECK(std::isfinite(rebuilt[col][row]));
+ CHECK(rebuilt[col][row] == doctest::Approx(matrix[col][row]).epsilon(.0001));
+ }
+}
+
+TEST_CASE("signed transforms reconstruct planar and spatial mirrors") {
+ for (float x : {1.0f, -1.0f}) for (float y : {1.0f, -1.0f}) for (float z : {1.0f, -1.0f}) {
+ for (bool planar : {false, true}) {
+ const auto rotation = planar ? glm::angleAxis(.7f, glm::vec3(0, 0, 1))
+ : glm::quat(glm::vec3(.3f, -.5f, .9f));
+ const auto matrix = glm::translate(glm::mat4(1), glm::vec3(4, -7, 3))
+ * glm::mat4_cast(rotation) * glm::scale(glm::mat4(1), glm::vec3(2*x, 3*y, 4*z));
+ checkReconstructedTransform(matrix, planar);
+ }
+ }
+}
+
+TEST_CASE("planar mirrors compose through parents and collapsed axes stay finite") {
+ for (float x : {1.0f, -1.0f}) for (float y : {1.0f, -1.0f}) {
+ Registry registry; TransformSystem system;
+ const auto parent = registry.create(), child = registry.create();
+ auto& p = registry.emplace<Transform>(parent);
+ p.rotation = glm::angleAxis(.7f, glm::vec3(0, 0, 1));
+ p.scale = {x, y, -1};
+ auto& c = registry.emplace<Transform>(child);
+ c.position = {3, 4, 0};
+ c.rotation = glm::angleAxis(-.2f, glm::vec3(0, 0, 1));
+ c.scale = {-2, 3, 1};
+ setParent(registry, child, parent);
+ system.ensureComposed(registry);
+ checkReconstructedTransform(c.cachedMatrix_, true);
+ }
+ for (const glm::vec3 scale : {glm::vec3{0, 3, 4}, {2, 0, 4}, {0, -3, -4}, {0, 0, 0}}) {
+ const auto matrix = glm::mat4_cast(glm::angleAxis(.7f, glm::vec3(0, 0, 1)))
+ * glm::scale(glm::mat4(1), scale);
+ checkReconstructedTransform(matrix, true);
+ }
+}
+
TEST_CASE("transformless grouping nodes preserve descendant composition") {
Registry r; TransformSystem system;
const auto root = r.create(), group = r.create(), child = r.create();
diff --git a/tests/ecs/test_ui_tree.cpp b/tests/ecs/test_ui_tree.cpp
index 6cbbf0a72..c870f7516 100644
--- a/tests/ecs/test_ui_tree.cpp
+++ b/tests/ecs/test_ui_tree.cpp
@@ -2,10 +2,32 @@
#include <doctest.h>
#include "esengine/ui/UITree.hpp"
#include "esengine/ui/UIRenderOrderSystem.hpp"
+#include "esengine/ui/UIHitTestSystem.hpp"
using namespace esengine;
using namespace esengine::ecs;
+TEST_CASE("mirrored UI boxes keep their pivot and rotated pointer bounds") {
+ for (float x : {1.0f, -1.0f}) for (float y : {1.0f, -1.0f}) {
+ for (float angle : {0.0f, .7f}) {
+ const auto rotation = glm::angleAxis(angle, glm::vec3(0, 0, 1));
+ const auto world = [&](glm::vec2 local) {
+ local *= glm::vec2(x, y);
+ return glm::vec2(13, 17) + glm::vec2(local.x * std::cos(angle) - local.y * std::sin(angle),
+ local.x * std::sin(angle) + local.y * std::cos(angle));
+ };
+ for (const glm::vec2 local : {glm::vec2{-1, -3}, {5, 1}}) {
+ const auto point = world(local);
+ CHECK(pointInOBB(point.x, point.y, 13, 17, 10*x, 8*y, .2f, .75f, rotation.z, rotation.w));
+ }
+ for (const glm::vec2 local : {glm::vec2{-3, 0}, {9, 0}, {0, -7}, {0, 3}}) {
+ const auto point = world(local);
+ CHECK_FALSE(pointInOBB(point.x, point.y, 13, 17, 10*x, 8*y, .2f, .75f, rotation.z, rotation.w));
+ }
+ }
+ }
+}
+
TEST_CASE("nested canvas roots respect hierarchy instead of component insertion order") {
for (bool nestedFirst : {false, true}) {
Registry registry;
diff --git a/tests/renderer/test_batch_builder.cpp b/tests/renderer/test_batch_builder.cpp
index 470246541..89104d7ba 100644
--- a/tests/renderer/test_batch_builder.cpp
+++ b/tests/renderer/test_batch_builder.cpp
@@ -278,6 +278,17 @@ TEST_CASE("camera mask: hidden text and skeletal submissions upload no geometry"
CHECK(frame.stats().triangles == 4);
}
+TEST_CASE("flat bounds: reflected extents enclose the same rotated box") {
+ for (f32 x : {1.0f, -1.0f}) for (f32 y : {1.0f, -1.0f}) {
+ for (f32 angle : {0.0f, .7f}) {
+ const auto extent = flatHalfExtents({std::cos(angle), std::sin(angle)}, {3*x, 5*y, -2});
+ CHECK(extent.x == doctest::Approx(std::abs(std::cos(angle))*3 + std::abs(std::sin(angle))*5));
+ CHECK(extent.y == doctest::Approx(std::abs(std::sin(angle))*3 + std::abs(std::cos(angle))*5));
+ CHECK(extent.z == 2);
+ }
+ }
+}
+
TEST_CASE("nine-slice: small boxes contain their borders and preserve source UVs") {
for (const glm::vec2 size : {glm::vec2{40, 30}, {10, 6}, {10, 30}, {40, 6}, {0, 6}, {10, 0}}) {
Harness h;
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 7f2911c43..116e166b6 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'fix(transform): preserve reflected world transforms and UI bounds':
+ { note: 'World transforms preserve mirrored axes.' },
'fix(renderer): mirror nine-slice cells and texture borders':
{ note: 'Nine-slice texture flips preserve asymmetric borders.' },
'fix(renderer): fit nine-slice borders inside small boxes':