Just stumbled upon this implementation for separating axis theorem, and I'm wondering why the author is using sf::transform. From my understanding sf::Transform has a built 3x3 matrix (Which is matrices for 2D). But why is the matrix needed?
OrientedBoundingBox( const Rect & rect)
{
sf::Transform tran = rect.getTransform();
Points[0] = tran.transformPoint(0.0f, 0.0f);
Points[1] = tran.transformPoint(rect.getSize().x, 0.0f);
Points[2] = tran.transformPoint(rect.getSize().x, rect.getSize().y);
Points[3] = tran.transformPoint(0.0f, rect.getSize().y);
}
sf::Vector2f Points[4];
void ProjectOntoAxis(const sf::Vector2f & Axis, float &Min, float &Max)
{
Min = (Points[0].x * Axis.x + Points[0].y * Axis.y);
Max = Min;
for(int j = 1; j < 4; j++)
{
float Projection = (Points[j].x * Axis.x + Points[j].y * Axis.y);
if(Projection < Min)
Min = Projection;
if(Projection > Max)
Max = Projection;
}
}
Why couldn't the author use the following code, assuming the default object origin is at the default top left corner.
OrientedBoundingBox(const sf::Sprite & Object)
{
sf::Transform trans = Object.getTransform();
sf::IntRect local = Object.getTextureRect();
Points[0] = sf::Vector2f(Object.getPosition().x, Object.getPosition().y);
Points[1] = sf::Vector2f(Object.getPosition().x + Object.getSize().x, Object.getPosition().y);
Points[2] = sf::Vector2f(Object.getPosition().x + Object.getSize().x, Object.getPosition().y + Object.getSize().y)
Points[3] = sf::Vector2f(Object.getPosition().x, Object.getPosition().y + Object.getSize().y)
}
What makes the first implementation better than the second? They both give you the same thing which is the objects vertices.