SFML community forums

Help => Graphics => Topic started by: etixpp on October 06, 2013, 02:31:23 pm

Title: Collision with png map
Post by: etixpp on October 06, 2013, 02:31:23 pm
Hello, i got a .png image as a map, what´s the best and easiest way to check collisions on it?
That´s my map http://s14.directupload.net/images/131006/cru95zfo.png
Title: Re: Collision with png map
Post by: zsbzsb on October 06, 2013, 03:31:05 pm
How about you forget the idea of having one giant map contained within one file. Instead break your map into individual textures and then use a text file (XML or other format) for storing where the textures will get drawn. Then you will have each thing that gets placed on your map easily defined for collision.

But if you insist on having one big image file, you could create a secondary image, make the collision areas black and the open areas white and then check the position to the color. Similar to a height map.
Title: Re: Collision with png map
Post by: etixpp on October 21, 2013, 08:37:20 pm
Is it possible to get the pixel at x and y of a sprite? Or does this method only work for sf::Images?

And is it somehow possible to set a rect for a texture? I made the collision layer as sf::image and check

                                if (pImage.getPixel(x + 4+(2*i) ,y + 30 -(j*2)).a > 0)
                                {
                                answer = true;
                                return answer;
                                }

when x and y are the player coordinates but this is a bit cpu intensiv, i would like to make the image smaller with a Rect so its only as big as needed to check if a player is on a non transparent place. This would work with a Sprite but i cant get the PixelColor if i use Sprite  :(
Title: Re: Collision with png map
Post by: Ixrec on October 21, 2013, 09:40:54 pm
I think https://github.com/SFML/SFML/wiki/Source%3A-Simple-Collision-Detection-for-SFML-2 covers the solutions to all these questions.