SFML community forums

Help => Graphics => Topic started by: cpl on September 06, 2007, 11:29:11 pm

Title: How to use sfRect Intersect()?
Post by: cpl 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 (http://www.ford-wiki.com/wiki/Ford_F-550)
Title: How to use sfRect Intersect()?
Post by: Mindiell on September 07, 2007, 07:04:02 am
Can you show us your 'GetRect' functions, for both paddle and ball please ?
Title: How to use sfRect Intersect()?
Post by: Laurent 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.
Title: How to use sfRect Intersect()?
Post by: cpl 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 (http://marijuanaseeds.org/)
Title: How to use sfRect Intersect()?
Post by: Laurent 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.
Title: How to use sfRect Intersect()?
Post by: cpl 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 (http://vaporizerwiki.com)
Title: How to use sfRect Intersect()?
Post by: Laurent 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());
Title: How to use sfRect Intersect()?
Post by: cpl 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 (http://boxvaporizers.com)