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

Author Topic: Perfect Pixel Collision Detection  (Read 2386 times)

0 Members and 1 Guest are viewing this topic.

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Perfect Pixel Collision Detection
« on: January 14, 2011, 08:29:19 pm »
I am new in SFML and I am tying to do a game. It is about the plane that flyes through the cave but my Perfect Pixel Collision Detection doesnt work. It also giving me meassages "Cannot get pixel numbers for image numbers". Please check my code, because I cannot find the bug.
Here is all with spriteshttp://www.megaupload.com/?d=00BJEWMA
Code: [Select]

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

bool col(int camera, int planex, int planey, sf::Sprite obr1, sf::Sprite obr2)
{
    for(int i=0;i<8;i++)
        for(int m=0;m<30;m++)
            if(obr1.GetPixel(camera+85+m,planey+i)==sf::Color::Blue && obr2.GetPixel(m,i)==sf::Color::Black)
                    return true;

    return false;
}

int main()
{
    // Create the main rendering window
    int camera = 0, plane_y = 50, plane_x = 85 ;

    sf::RenderWindow App(sf::VideoMode(200, 100, 32), "SFML Plane");

    // Load the sprite image from a file
    sf::Image Image;
    sf::Image Image2;
    if (!Image.LoadFromFile("sprite.png"))
        return EXIT_FAILURE;

    if (!Image2.LoadFromFile("plane.png"))
        return EXIT_FAILURE;
    // Create the sprite
    sf::Sprite Map(Image);
    sf::Sprite Plane(Image2);
    Plane.SetPosition(plane_x,plane_y);

    // Start game loop

    App.Draw(Plane);
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        App.Clear();

        Map.SetSubRect(sf::IntRect(camera, 0, camera+200, 100));
        App.Draw(Map);
        App.Draw(Plane);

        if (App.GetInput().IsKeyDown(sf::Key::Up)) {
        plane_y-=2;
        Plane.SetPosition(plane_x,plane_y);
        }

        if (App.GetInput().IsKeyDown(sf::Key::Down)) {
        plane_y+=2;
        Plane.SetPosition(plane_x,plane_y);
        }

        if(col(camera,plane_x,plane_y,Map,Plane)==true)
        break;

        App.Display();

        if(camera < 800)
        camera+=2;

        sf::Sleep(0.02f);
    }


    return EXIT_SUCCESS;
}

Gibgezr

  • Newbie
  • *
  • Posts: 33
    • View Profile
Perfect Pixel Collision Detection
« Reply #1 on: February 18, 2011, 03:20:31 am »
I dl'd and tested your app with SFML 2, and it worked fine. Didn't try it with 1.6.

Just a note: change your collision function like so:
Code: [Select]

bool col(int camera, int planex, int planey, const sf::Sprite& obr1, const sf::Sprite& obr2)


Instead of creating copies of the sprites every time you call the collision func, it will now pass constant references to the sprites i.e. a single pointer for each sprite. Much more efficient.

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Perfect Pixel Collision Detection
« Reply #2 on: February 18, 2011, 07:42:50 am »
I solved it different way but thanks . :D

 

anything