0 Members and 2 Guests are viewing this topic.
bool Intersects(const sfRect<T>& Rect, sfRect<T>* OverlappingRect = NULL) const;
void HandleBallToPlayerCollision(){ if( paddle->GetRect().Intersects(ball->GetRect())) { // Do stuff here..... }}
class Sprite{public: sfIntRect GetRect(){ return sprite.GetSubRect(); } //.....private: sfSprite sprite; //.....};class Paddle : public Sprite{//.....}class Ball : public Sprite{//.....}
sprite.GetSubRect will give you coordinates relative to the image, you have to offset it by the position of the sprite.
sfIntRect GetRect(){ return sfIntRect(sprite.GetLeft(),sprite.GetTop(),sprite.GetWidth(),sprite.GetHeight()); }
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 :Code: [Select]return sfIntRect(sprite.GetLeft(),sprite.GetTop(),sprite.GetLeft()+sprite.GetWidth(),sprite.GetTop()+sprite.GetHeight());
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 }}