SFML community forums

General => General discussions => Topic started by: lruc on August 19, 2008, 02:57:59 am

Title: Sprite Collision Detection
Post by: lruc on August 19, 2008, 02:57:59 am
Is there anyway to detect sprite collisions in SFML? I wanted to use SFML to make a simple RPG but it doesn't have a lot of things that I thought it would have, mostly sprite collision. Is SFML even meant for games?
Title: Sprite Collision Detection
Post by: Wizzard on August 19, 2008, 03:23:06 am
You can create your own collision detection with SFML.
All you need to detect collisions in any situation is the position and size of the objects.
With SFML's graphic package, all sprites already have a position and a rectangular shape.

To imagine this, here is a little ascii art of a rectangle with the calculations to get all four points of the rectangle:
Code: [Select]
(x, y)-----------------------(x + width, y)
|                                         |
|                                         |
|                                         |
|                                         |
|                                         |
(x, y + height)-----(x + width, y + height)

Using the greater than and less than operators, you can easily see if one sprite's rectangle is overlapping another's.
This should suffice for a simple 2D RPG.
Study into collision detection a bit more though and using the same concepts you can create pixel perfect collision detection in SFML.
Title: Sprite Collision Detection
Post by: lruc on August 19, 2008, 11:23:01 pm
How about something like animation support? ex. like GIF's of bmp sprite sheets.
Title: Sprite Collision Detection
Post by: lruc on August 19, 2008, 11:39:32 pm
Edit: GIF's OR bmp sprite sheets
Title: Sprite Collision Detection
Post by: quasius on August 19, 2008, 11:43:26 pm
Quote from: "lruc"
Edit: GIF's OR bmp sprite sheets


You can do that pretty easily by setting the subrect of the image.
Title: Sprite Collision Detection
Post by: dunce on August 20, 2008, 10:02:59 am
I think that some kind of scene partition management should be used in order to eliminate each-to-each collision testing.  :)
Title: Sprite Collision Detection
Post by: tgm on August 21, 2008, 09:09:27 am
if you're able to sort your boxes along the X axis you could use some line swep algo for doing collision test... might be easyier to maintain a sorting along X-axis in a moving scene than maintaining a komplete Quadtree or something like this....
though quadtree would most likely be faster...
Title: Sprite Collision Detection
Post by: quasius on August 21, 2008, 03:47:36 pm
Quote from: "tgm"
if you're able to sort your boxes along the X axis you could use some line swep algo for doing collision test... might be easyier to maintain a sorting along X-axis in a moving scene than maintaining a komplete Quadtree or something like this....
though quadtree would most likely be faster...


I posted a bit on this before:

http://www.sfml-dev.org/forum/viewtopic.php?t=524&highlight=collision+detection