From 586f6e4993f80f09ec5dc43e50d8fa4236bb9b8a Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Sat, 3 Aug 2024 13:56:16 +0100 Subject: [PATCH] Adds toTransform for WorldBoundingBox --- examples/Sprite/main.cpp | 13 ++--------- include/jGL/primitive.h | 47 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/examples/Sprite/main.cpp b/examples/Sprite/main.cpp index 81343c3..3afcd28 100644 --- a/examples/Sprite/main.cpp +++ b/examples/Sprite/main.cpp @@ -281,17 +281,8 @@ int main(int argv, char ** argc) animationFrame = (animationFrame+1)%animationFrames.size(); } - jGL::WorldBoundingBox wbbPi = sprites->getSprite("sPi").getWorldBoundingBox(); - float x = 0.0; float y = 0.0; - for (auto p : wbbPi.vertices) { x += p.x; y += p.y;} - float s = std::sqrt((wbbPi.vertices[1].x-wbbPi.vertices[0].x)*(wbbPi.vertices[1].x-wbbPi.vertices[0].x) + (wbbPi.vertices[1].y-wbbPi.vertices[0].y)*(wbbPi.vertices[1].y-wbbPi.vertices[0].y)); - shapeTrans[0] = jGL::Transform(x*0.25f, y*0.25f, trans["sPi"].theta ,s); - - jGL::WorldBoundingBox wbbHeart = sprites->getSprite("sHeart").getWorldBoundingBox(); - x = 0.0; y = 0.0; - for (auto p : wbbHeart.vertices) { x += p.x; y += p.y;} - s = std::sqrt((wbbHeart.vertices[1].x-wbbHeart.vertices[0].x)*(wbbHeart.vertices[1].x-wbbHeart.vertices[0].x) + (wbbHeart.vertices[1].y-wbbHeart.vertices[0].y)*(wbbHeart.vertices[1].y-wbbHeart.vertices[0].y)); - shapeTrans[1] = jGL::Transform(x*0.25f, y*0.25f, trans["sHeart"].theta ,s); + shapeTrans[0] = sprites->getSprite("sPi").getWorldBoundingBox().toTransform(); + shapeTrans[1] = sprites->getSprite("sHeart").getWorldBoundingBox().toTransform(); sprites->draw(); shapes->draw(); diff --git a/include/jGL/primitive.h b/include/jGL/primitive.h index 0ee2f5e..d9f94fa 100644 --- a/include/jGL/primitive.h +++ b/include/jGL/primitive.h @@ -136,7 +136,7 @@ namespace jGL /** * @brief Vertices of bounding box. - * @remark Assumed in clockwise order, e.g. Sprite::getWorldBoundingBox + * @remark Assumed in anti-clockwise order, e.g. Sprite::getWorldBoundingBox */ std::array, 4> vertices; @@ -165,11 +165,52 @@ namespace jGL } }; - /** + /** * @brief A world space bounding box. * */ - typedef BoundingBox WorldBoundingBox; + class WorldBoundingBox : public BoundingBox + { + public: + WorldBoundingBox() + : BoundingBox({glm::tvec2(0),glm::tvec2(0),glm::tvec2(0),glm::tvec2(0)}) + {} + + WorldBoundingBox(const std::array, 4> & v) + : BoundingBox(v) + {} + + /** + * @brief Returns a Transform for ShapeRenderer. + * + * @return Transform + */ + Transform toTransform() + { + float lowest = vertices[0].y; uint8_t lowestVertex = 0; + glm::vec2 centre = vertices[0]; + for (uint8_t i = 1; i < vertices.size(); i++) + { + if (vertices[i].y < lowest) { lowestVertex = i; lowest = vertices[i].y; } + centre += vertices[i]; + } + centre *= 0.25; + + uint8_t next = (lowestVertex + 1) % vertices.size(); + glm::vec2 r = vertices[next] - vertices[lowestVertex]; + float d = std::sqrt(r.x*r.x+r.y*r.y); + glm::vec2 n = r / d; + float theta = 0.0; + if (n.y != 0.0) + { + theta = std::atan2(n.y, n.x); + } + + r = vertices[(lowestVertex - 1) % vertices.size()] - vertices[lowestVertex]; + + return Transform(centre.x, centre.y, -theta, d, std::sqrt(r.x*r.x+r.y*r.y)); + } + }; /** * @brief A screen space bounding box.