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

Author Topic: Collision with png map  (Read 2766 times)

0 Members and 1 Guest are viewing this topic.

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Collision with png map
« 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

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Collision with png map
« Reply #1 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.
« Last Edit: October 06, 2013, 03:37:06 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Re: Collision with png map
« Reply #2 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  :(
« Last Edit: October 21, 2013, 08:41:41 pm by etixpp »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Collision with png map
« Reply #3 on: October 21, 2013, 09:40:54 pm »