SFML community forums

Help => Graphics => Topic started by: OrientatedCookie on April 17, 2015, 03:53:44 pm

Title: Sprite movement help
Post by: OrientatedCookie on April 17, 2015, 03:53:44 pm
I'm currently making a classic ping pong game, but I don't know what the code/syntax is for if a sprite contacts another sprite, if you know what I mean. I thought I could make an if statement that checks if the ball is touching the rectangle or something like that, and then make it go towards somewhere.  Can anyone help me with this, or what other way could I do it?  :D
Title: Re: Sprite movement help
Post by: zsbzsb on April 17, 2015, 04:50:56 pm
http://lmgtfy.com/?q=basic+collision+detection
Title: Re: Sprite movement help
Post by: OrientatedCookie on April 17, 2015, 05:09:08 pm
You could of just written it in text, not with the slightly overdone derision.
Title: Re: Sprite movement help
Post by: Rosme on April 17, 2015, 06:02:45 pm
Or you could just have searched for it. It's not like it's the most complicated things and you're the first to ask this. There is thousands of post on the web about this. Follow the link from zsbzsb, and you'll find an answer for sure.
Title: Re: Sprite movement help
Post by: zsbzsb on April 17, 2015, 06:24:08 pm
Quote from: http://en.sfml-dev.org/forums/index.php?topic=5559.msg36364#msg36364
Chances are that your problem was already solved somewhere on the internet, so after checking the documentation, don't hesitate to:
  • search the SFML forum
  • ask Google

Collision detection is a huge topic, hence the link. When you ask "how to do collision detection" its not something that we can just explain. So unless you are more specific on what you need help with pretty much all you will get is very generalized answers.

On a side note, collision detection and response (physics in general) is really more of a math question than a graphics question. Graphics are displayed on top of what your physics code does when it actually updates your objects positions and whatnot.

sf::Rect<T>::intersects(...) (http://www.sfml-dev.org/documentation/2.2/classsf_1_1Rect.php#a566740c8f58e01bb052266f47e7e1011)
Title: Re: Sprite movement help
Post by: Jesper Juhl on April 17, 2015, 06:32:03 pm
I've always found the following article very useful as a basic starting point: Pool Hall Lessons: Fast, Accurate Collision Detection Between Circles or Spheres (http://www.gamasutra.com/view/feature/131424/pool_hall_lessons_fast_accurate_.php?print=1).
 Maybe you'll enjoy it as well.

Also, this is a good book that's relevant to physics simulation and collision detection that I'd recommend reading : Physics for Game Developers, 2nd Edition (http://shop.oreilly.com/product/mobile/0636920012221.do).

As has already been pointed out, collision detection is a huge topic and it's really not within the scope of SFML. SFML just lets you draw stuff and test basic things like if rectangles intersect, but if you want to use bounding boxes (axis alligned or not), bounding spheres, pixel perfect collision detection or something else, then it's really into the math/physics domain and you have to do your own calculations. SFML will draw the results of your calculations nicely for you (but it won't do the detection work).