Collision detection c++ and boundry detection

I am making a game where the player uses the arrow keys to control a submarine to avoid obstacles. However I am unsure of to detect collisions between the 2 objects...

Using c++ and SFML I have declared the Submarine sprite like this

      void Submarine::Init(const std::string& ImageFile)
      cout<<"Sub loaded";
      cout<<"Sub stored"<<endl;

and the obstacle sprites are declared like this

class ObstacleSprite : public Sprite
      ObstacleSprite(const Image &Img, const Vector2f &Position=Vector2f(0, 0), const Vector2f &Scale=Vector2f(1, 1), float Rotation=0.f, const Color &Col=Color(255, 255, 255, 255),float scrollPos=0.0);
      void setScrollPos(float scrollPos);
      void setVertPos(float vertPos);
      float getVertPos(void);
      float getScrollPos(void);

My best guess is a bounding box, but as i've never written one before I am unsure how to go about it. In the SFML documentation there may be somthing like 'get sub rect' but again I am unsure how to implement these...

This is a link to sf::sprite documentation
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


doing so is simple, use both the position of the Sprite ( using  GetPosition() ) and size ( using GetSize() ) to do the bounding-box check.

in the game-loop create a function to do your collision checks, like so:

bool CheckCollision( Sprite *o1, Sprite *o2 )
   Vector2f pos1 = o1->GetPosition() ;
   Vector2f size1 = o1->GetSize() ;

   Vector2f pos2 = o2->GetPosition() ;
   Vector2f size2 = o2->GetSize() ;

    // . Check if box A is completely to the left of box B  If so, then quit the process: they don't intersect.

    if( pos1.x() > ( pos2.x() + size2.x() ) )
	return false ;

    // Otherwise, check if it's completely to the right  If so, quit the process.

    if( ( pos1.x() + size1.x() ) < pos2.x() )
	return false ;

    // 3. If not, check if it's completely above box B . If so, quit.

    if( ( pos1.y() + size1.y() ) < pos2.y() )
	return false ;

    // 4. If not, check if it's completely beneath box B . If so, quit.

    if( pos1.y() > ( pos2.y() + size2.y() ) )
	return false ;

   return true ;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Computer Games

From novice to tech pro — start learning today.