Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Question of SAT implementation  (Read 1270 times)

0 Members and 1 Guest are viewing this topic.

Student555

  • Guest
Question of SAT implementation
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question of SAT implementation
« Reply #1 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.
Laurent Gomila - SFML developer

Student555

  • Guest
Re: Question of SAT implementation
« Reply #2 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question of SAT implementation
« Reply #3 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.
Laurent Gomila - SFML developer

 

anything