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

Author Topic: Collision Detection.  (Read 3548 times)

0 Members and 1 Guest are viewing this topic.

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
Collision Detection.
« on: February 20, 2014, 12:03:58 pm »
I finally decided to get back into coding, and after seeing that not only SFML 2.0 has finally been released, but SFML 2.1 has been too, I decided to start converting my old SFML 1.6 code to the new format.

And I've hit my first brick wall with Collision Detection.

my old code relies on TransformToGlobal to look up screen positions of 2 sprites, to see if they intersect, and TransformToLocal to then find the pixel position within that sprite.

Okay, I think I figured that part out.
I used getTransform in place of TransformToGlobal, and getInverseTransform in place of TransformToLocal, with a transformPoint(vector) on the end to get the actual positions of the sprites, and positions of the pixels within the sprite.

the next step was then to compare the pixels with GetPixel to see if they both had an alpha > 127.

Problem: sf::Sprite::GetPixel() no longer exists, nor has a .getPixel() alternative.

I thought... no problem, let's just look at the image pixel. wait... we're using Textures in Sprites now, instead of Images... no getPixel there either. (Also, I would have needed to do even more maths on the sprite and texture's Rects to figure out what pixel I actually wanted to look at, but with all the extra maths everywhere accounting for the massive changes in structure (EG, changing rects from left, top, right, buttom, to left, top, width, heiht), the maths is kinda moot).


So... how do I examine the alpha of a specific pixel in a sprite in SFML 2.1?

Alternate question: How would I do a "Pixel Perfect" collision Detection of 2  sprites in SFML 2.1?
« Last Edit: February 20, 2014, 12:48:01 pm by bobingabout »

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Collision Detection.
« Reply #1 on: February 20, 2014, 02:27:40 pm »
I've been thinking about it, and I think I might have an answer for myself... 2 actually.

Code: [Select]
sf::Image image1 = sprite1.getTexture()->copyToImage();
sf::Image image2 = sprite2.getTexture()->copyToImage();

Code: [Select]
if ((image1.getPixel((int)(sprite1pos.x), (int)(sprite1pos.y)).a > 127) &&
(image2.getPixel((int)(sprite2pos.x), (int)(sprite2pos.y)).a > 127))
return true;

I hate this processor heavy solution, but it looks like it might work.

if there was an easy way to access the image I loaded from file originally from the sprite/texture, I'd do that, which brings me to option 2.

Since I had to load it into an image first to be able to use "createMaskFromColor(transparancy,0);", the files are there in memory, just no way to link there from a sprite, or texture. so, the solution would be writing my own texture class with the single addition of a pointer to the image, and costom sprite class to the new texture class.


I'll use option 1 for now, simply get it working, and come back later to fix these performance holes I'm creating for myself...


unless someone else has an idea.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Collision Detection.
« Reply #2 on: February 20, 2014, 03:16:27 pm »
TransformToGlobal and TransformToLocal have been renamed to getGlobalBounds and getLocalBounds respectively.

As for the pixel perfect collision, just load a sf::Image and then load your texture from that. And keep the image in memory if you really want pixel perfect collision (which I highly doubt you need).
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Collision Detection.
« Reply #3 on: February 20, 2014, 04:18:24 pm »
TransformToGlobal and TransformToLocal have been renamed to getGlobalBounds and getLocalBounds respectively.

As for the pixel perfect collision, just load a sf::Image and then load your texture from that. And keep the image in memory if you really want pixel perfect collision (which I highly doubt you need).

TransformToGlobal and TransformToLocal didn't return an IntRect like your sugested getGlobalBounds and getLocalBounds do(Well, they return a FloatRect), they returned a Vector2f, for a single point, aka, pixel.

Thanks for sugesting them though, they might make one of my earlier calculations easier.

And your sugestion with the image is basically my option 2, that I already thought of, the hard part is keeping track of it.
« Last Edit: February 20, 2014, 04:19:55 pm by bobingabout »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Collision Detection.
« Reply #4 on: February 20, 2014, 10:54:33 pm »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Collision Detection.
« Reply #5 on: February 21, 2014, 09:49:17 am »
Don't hesitate to search before asking ;)
http://en.sfml-dev.org/forums/index.php?topic=13810
http://en.sfml-dev.org/forums/index.php?topic=14174

I did, it told me "Database Error"... so I looked to see if there were any on the latest page, and didn't see any, so I asked anyway.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Collision Detection.
« Reply #6 on: February 21, 2014, 10:17:49 am »
The forum search has some bugs, it helps if you limit the number of results by selecting only some subcategories. Alternatively, use an external search engine.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: