Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite Collision Detection  (Read 11433 times)

0 Members and 1 Guest are viewing this topic.

lruc

  • Newbie
  • *
  • Posts: 6
    • View Profile
Sprite Collision Detection
« 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?

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Sprite Collision Detection
« Reply #1 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.

lruc

  • Newbie
  • *
  • Posts: 6
    • View Profile
Sprite Collision Detection
« Reply #2 on: August 19, 2008, 11:23:01 pm »
How about something like animation support? ex. like GIF's of bmp sprite sheets.

lruc

  • Newbie
  • *
  • Posts: 6
    • View Profile
Sprite Collision Detection
« Reply #3 on: August 19, 2008, 11:39:32 pm »
Edit: GIF's OR bmp sprite sheets

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Sprite Collision Detection
« Reply #4 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.

dunce

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Sprite Collision Detection
« Reply #5 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.  :)

tgm

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Sprite Collision Detection
« Reply #6 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...

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Sprite Collision Detection
« Reply #7 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