Skip to content

Commit

Permalink
Adds toTransform for WorldBoundingBox
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Aug 3, 2024
1 parent 5412b73 commit 586f6e4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
13 changes: 2 additions & 11 deletions examples/Sprite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
47 changes: 44 additions & 3 deletions include/jGL/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<glm::tvec2<T>, 4> vertices;

Expand Down Expand Up @@ -165,11 +165,52 @@ namespace jGL
}
};

/**
/**
* @brief A world space bounding box.
*
*/
typedef BoundingBox<float> WorldBoundingBox;
class WorldBoundingBox : public BoundingBox<float>
{
public:
WorldBoundingBox()
: BoundingBox<float>({glm::tvec2<float>(0),glm::tvec2<float>(0),glm::tvec2<float>(0),glm::tvec2<float>(0)})
{}

WorldBoundingBox(const std::array<glm::tvec2<float>, 4> & v)
: BoundingBox<float>(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.
Expand Down

0 comments on commit 586f6e4

Please sign in to comment.