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

Author Topic: How to use sfRect Intersect()?  (Read 7094 times)

0 Members and 1 Guest are viewing this topic.

cpl

  • Newbie
  • *
  • Posts: 19
    • View Profile
How to use sfRect Intersect()?
« on: September 06, 2007, 11:29:11 pm »
Okey, I'm making a simple Arkanoid-game and having trouble using this sfRect public member function.

Code: [Select]

bool Intersects(const sfRect<T>& Rect, sfRect<T>* OverlappingRect = NULL) const;


I'm trying to use this function to tell when the paddle and the ball has collided however this doesn't seem to work. This is currently how I'm doing it:

Code: [Select]

void HandleBallToPlayerCollision()
{
      if( paddle->GetRect().Intersects(ball->GetRect()))
      {
             // Do stuff here.....          
      }
}


This method doesn't work. The function returns true even if there is no collision.
How should I do to make this work?
________
F-550

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
How to use sfRect Intersect()?
« Reply #1 on: September 07, 2007, 07:04:02 am »
Can you show us your 'GetRect' functions, for both paddle and ball please ?
Mindiell
----

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to use sfRect Intersect()?
« Reply #2 on: September 07, 2007, 08:39:46 am »
It would also help to have the coordinates of both rectangles when the function returns a wrong result.
Laurent Gomila - SFML developer

cpl

  • Newbie
  • *
  • Posts: 19
    • View Profile
How to use sfRect Intersect()?
« Reply #3 on: September 07, 2007, 10:18:19 am »
This is basically how it's done:

Code: [Select]

class Sprite
{
public:
     sfIntRect GetRect(){ return sprite.GetSubRect(); }
     //.....
private:
     sfSprite sprite;
     //.....
};

class Paddle : public Sprite
{
//.....
}
class Ball : public Sprite
{
//.....
}
________
buy cannabis seeds

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to use sfRect Intersect()?
« Reply #4 on: September 07, 2007, 10:19:56 am »
sprite.GetSubRect will give you coordinates relative to the image, you have to offset it by the position of the sprite.
Laurent Gomila - SFML developer

cpl

  • Newbie
  • *
  • Posts: 19
    • View Profile
How to use sfRect Intersect()?
« Reply #5 on: September 07, 2007, 11:07:29 am »
Quote from: "Laurent"
sprite.GetSubRect will give you coordinates relative to the image, you have to offset it by the position of the sprite.


I'm not really sure how you mean. Is it something like this:
Code: [Select]

sfIntRect GetRect()
{
return sfIntRect(sprite.GetLeft(),sprite.GetTop(),sprite.GetWidth(),sprite.GetHeight());
}
________
vaporizer wiki

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to use sfRect Intersect()?
« Reply #6 on: September 07, 2007, 11:18:07 am »
The two last parameters are right and bottom, not width and height :
Code: [Select]
return sfIntRect(sprite.GetLeft(),sprite.GetTop(),sprite.GetLeft()+sprite.GetWidth(),sprite.GetTop()+sprite.GetHeight());
Laurent Gomila - SFML developer

cpl

  • Newbie
  • *
  • Posts: 19
    • View Profile
How to use sfRect Intersect()?
« Reply #7 on: September 07, 2007, 11:28:01 am »
Quote from: "Laurent"
The two last parameters are right and bottom, not width and height :
Code: [Select]
return sfIntRect(sprite.GetLeft(),sprite.GetTop(),sprite.GetLeft()+sprite.GetWidth(),sprite.GetTop()+sprite.GetHeight());


Thanks a lot! Now it works properly.

Code: [Select]


class Sprite
{
public:
     sfIntRect GetRect()
     {  
         return sfIntRect((sprite.GetLeft(),
                                  sprite.GetTop(),
                                  sprite.GetLeft()+sprite.GetWidth(),
                                  sprite.GetTop()+sprite.GetHeight());
     }
};

class Ball : public Sprite {}
class Paddle : public Sprite {}

void HandleBallToPlayerCollision()
{
     if( paddle->GetRect().Intersects(ball->GetRect()))
     {
          //Do stuff if collided      
     }
}

________
box vaporizer