SFML community forums
Help => Graphics => Topic started 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.
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:
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)
-
Can you show us your 'GetRect' functions, for both paddle and ball please ?
-
It would also help to have the coordinates of both rectangles when the function returns a wrong result.
-
This is basically how it's done:
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/)
-
sprite.GetSubRect will give you coordinates relative to the image, you have to offset it by the position of the sprite.
-
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:
sfIntRect GetRect()
{
return sfIntRect(sprite.GetLeft(),sprite.GetTop(),sprite.GetWidth(),sprite.GetHeight());
}
________
vaporizer wiki (http://vaporizerwiki.com)
-
The two last parameters are right and bottom, not width and height :
return sfIntRect(sprite.GetLeft(),sprite.GetTop(),sprite.GetLeft()+sprite.GetWidth(),sprite.GetTop()+sprite.GetHeight());
-
The two last parameters are right and bottom, not width and height :
return sfIntRect(sprite.GetLeft(),sprite.GetTop(),sprite.GetLeft()+sprite.GetWidth(),sprite.GetTop()+sprite.GetHeight());
Thanks a lot! Now it works properly.
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)