SFML community forums

Help => General => Topic started by: Student555 on November 03, 2016, 04:42:48 am

Title: Question of SAT implementation
Post by: Student555 on November 03, 2016, 04:42:48 am
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?
Code: [Select]
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.
Code: [Select]
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.
Title: Re: Question of SAT implementation
Post by: Laurent on November 03, 2016, 07:44:19 am
Quote
What makes the first implementation better than the second?
It doesn't ignore rotation and scale.
Title: Re: Question of SAT implementation
Post by: Student555 on November 04, 2016, 08:46:42 am
Oh ok, so

Code: [Select]
sf::Transform trans = Object.getTransform();

Returns a matrix that has all the possible transformations of the object (translation, rotation, and scale).

Not quite sure what the following code does.
Code: [Select]
Point[0] = tran.transformPoint(0.0f, 0.0f);

Looking at the documentation the the above function transforms 2D point (Guessing the point passed in the function?). However, "transforms 2D point", sounds kinda vague to me. So what is it doing exactly (translation, rotation, or scale?). From what I can get from debugging, the above code returns the origin of the object. So is the purpose of the function to give it object coordinates then it spits out the objects world coordinates?
Title: Re: Question of SAT implementation
Post by: Laurent on November 04, 2016, 10:26:41 am
transformPoint transforms the given point with the content of the transform. Which is, when you get it from object.getTransform(), the combined transformations (position, origin, rotation, scale) of the object, of course.