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

Author Topic: Issues with intersects  (Read 1438 times)

0 Members and 1 Guest are viewing this topic.

CG13

  • Newbie
  • *
  • Posts: 2
    • View Profile
Issues with intersects
« on: May 11, 2013, 01:41:27 pm »
Hello there.
I just have a little problem I can't wrap my head around.
The function "intersects" returns true even if the rects should not have space in common.

#include "stdafx.h"
#include "SFML\Graphics.hpp"


int _tmain(int argc, _TCHAR* argv[])
{

        sf::Rect<float> rect1 (4.0f, 3.0f, 4.5f, 3.5f);        // Hint: Nexus = sf::Rect<float> (4.0f, 3.0f, 4.5f, 3.5f);
        sf::Rect<float> rect2 (4.0f, 3.5f, 4.5f, 4.0f);        // Hint: Nexus = sf::Rect<float> (4.0f, 3.5f, 4.5f, 4.0f);

        sf::Rect<float> rect3 (6.0f, 7.0f, 7.0f, 8.0f);        // Hint: Nexus  = sf::Rect<float> (6.0f, 7.0f, 7.0f, 8.0f);
       
        if (rect1.intersects (rect3)) std::cout << "Rect 1 intersects Rect 3" << std::endl;
        if (rect2.intersects (rect3)) std::cout << "Rect 2 intersects Rect 3" << std::endl;

        system("PAUSE");

        return 0;
}

On my console the program returns: Rect 2 intersects Rect 3.

From what I understand it should look like that:
(Rect 1 isn't shown)


So, where am I wrong, or why do they intersect?

Thanks in advance.
« Last Edit: May 11, 2013, 01:53:44 pm by CG13 »

gostron

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Issues with intersects
« Reply #1 on: May 11, 2013, 01:44:27 pm »
You seem to work with SFML 2.0, so the constructor for Rect takes 4 arguments which are not the same as for SFML 1.6 :

left, top, width, height and NOT
left, top, right, bottom

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Issues with intersects
« Reply #2 on: May 11, 2013, 01:47:31 pm »
sf::Rect<float> rect1 = sf::Rect<float> (4.0f, 3.0f, 4.5f, 3.5f);
Why not simply
sf::FloatRect rect1(4.0f, 3.0f, 4.5f, 3.5f);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

CG13

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Issues with intersects
« Reply #3 on: May 11, 2013, 01:51:14 pm »
Ha, that's it!
I compared the changes from 1.6 to 2.0 and I don't know why I was thinking, that width and height are still koordinates instead of scalars relativ to the top and left..

And yes Nexus, for obvious reasons that would be.. prettier.. and easier.. better overall.

Thanks!