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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(render): a turned sprite is culled by a box that turned with it
yhh9d0690edd19b
diff --git a/src/esengine/renderer/RenderTypePlugin.hpp b/src/esengine/renderer/RenderTypePlugin.hppindex c581012d7..8ce60bb83 100644--- a/src/esengine/renderer/RenderTypePlugin.hpp+++ b/src/esengine/renderer/RenderTypePlugin.hpp@@ -16,6 +16,8 @@#include <glm/glm.hpp>+#include <cmath>+namespace esengine {struct Frustum;@@ -103,6 +105,27 @@ inline glm::vec3 parallaxedWorldPosition(ecs::Transform& transform, const glm::vreturn position;}+/**+ * @brief The cos/sin of the turn a plane renderable draws with: its entity's rotation+ * about Z, which is the only part a flat quad uses.+ */+inline glm::vec2 flatTurnZ(const glm::quat& rotation) {+ const f32 n = rotation.w * rotation.w + rotation.z * rotation.z;+ if (n <= 1e-12f) return {1.0f, 0.0f};+ return {(rotation.w * rotation.w - rotation.z * rotation.z) / n,+ 2.0f * rotation.w * rotation.z / n};+}++/**+ * @brief World AABB half-extents of a flat box turned by @p turn (from flatTurnZ).+ * |R| * half, exact for a box. An unturned entity gets @p half back, so the+ * result is never narrower than the box itself.+ */+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};+}+class RenderTypePlugin {public:virtual ~RenderTypePlugin() = default;diff --git a/src/esengine/renderer/plugins/ShapePlugin.cpp b/src/esengine/renderer/plugins/ShapePlugin.cppindex 81e3a3504..603d0ce61 100644--- a/src/esengine/renderer/plugins/ShapePlugin.cpp+++ b/src/esengine/renderer/plugins/ShapePlugin.cpp@@ -54,7 +54,10 @@ void ShapePlugin::collect(RenderCollectContext& collect_ctx) {const auto& rotation = transform.worldRotation;const auto& scale = transform.worldScale;- glm::vec3 halfExtents = glm::vec3(shape.size.x * scale.x, shape.size.y * scale.y, 0.0f) * 0.5f;+ // Turned about `position`, so only the extents move (see flatHalfExtents).+ glm::vec3 halfExtents = flatHalfExtents(+ flatTurnZ(rotation),+ glm::vec3(shape.size.x * scale.x, shape.size.y * scale.y, 0.0f) * 0.5f);if (!frustum.intersectsAABB(position, halfExtents)) {continue;}diff --git a/src/esengine/renderer/plugins/SpritePlugin.cpp b/src/esengine/renderer/plugins/SpritePlugin.cppindex d746b843e..002530768 100644--- a/src/esengine/renderer/plugins/SpritePlugin.cpp+++ b/src/esengine/renderer/plugins/SpritePlugin.cpp@@ -37,8 +37,15 @@ void SpritePlugin::collect(RenderCollectContext& collect_ctx) {glm::vec2 finalSize = sprite.size * glm::vec2(scale);glm::vec2 pivotOffset((0.5f - sprite.pivot.x) * finalSize.x,(0.5f - sprite.pivot.y) * finalSize.y);- glm::vec3 aabbCenter = position + glm::vec3(pivotOffset, 0.0f);- glm::vec3 halfExtents = glm::vec3(std::abs(finalSize.x), std::abs(finalSize.y), 0.0f) * 0.5f;+ // The quad turns about `position`, so its pivot offset turns with it and its+ // extents grow. Bounding the unturned box instead loses a turned sprite while+ // part of it is still on screen — at 45 degrees the box is 41% short.+ const glm::vec2 turn = flatTurnZ(rotation);+ glm::vec3 aabbCenter = position + glm::vec3(pivotOffset.x * turn.x - pivotOffset.y * turn.y,+ pivotOffset.x * turn.y + pivotOffset.y * turn.x,+ 0.0f);+ glm::vec3 halfExtents = flatHalfExtents(+ turn, glm::vec3(std::abs(finalSize.x), std::abs(finalSize.y), 0.0f) * 0.5f);if (!frustum.intersectsAABB(aabbCenter, halfExtents)) {continue;}diff --git a/src/esengine/renderer/plugins/UIElementPlugin.cpp b/src/esengine/renderer/plugins/UIElementPlugin.cppindex 694e2adb5..68484d965 100644--- a/src/esengine/renderer/plugins/UIElementPlugin.cpp+++ b/src/esengine/renderer/plugins/UIElementPlugin.cpp@@ -87,7 +87,9 @@ void UIElementPlugin::collect(RenderCollectContext& collect_ctx) {position.x += dx;position.y += dy;- glm::vec3 halfExtents = glm::vec3(w * scale.x, h * scale.y, 0.0f) * 0.5f;+ // `position` above already carries the turned offset, so only the extents do.+ glm::vec3 halfExtents = flatHalfExtents(+ flatTurnZ(rotation), glm::vec3(w * scale.x, h * scale.y, 0.0f) * 0.5f);if (!frustum.intersectsAABB(position, halfExtents)) continue;f32 angle = 2.0f * std::atan2(rotation.z, rotation.w);