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

Author Topic: SFML Pixel Perfect Collision limited to a view?  (Read 3369 times)

0 Members and 1 Guest are viewing this topic.

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
SFML Pixel Perfect Collision limited to a view?
« on: April 25, 2018, 07:58:47 pm »
In this code I have one view (white, zoom 2) and the original window, also one sprite for a car and one sprite for a wall...

Code: [Select]
#include <SFML/Graphics.hpp>
#include "collision.hpp"

#define windowWidth  1000
#define windowHeight 600

int main()
{
    sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "SFML Views");

    sf::View view(sf::FloatRect(0,0, windowWidth, windowHeight));
    view.zoom(2);

// car
    sf::Texture imgCar;
    Collision::CreateTextureAndBitmask(imgCar, "carro.png" );
    sf::Sprite car;
    car.setTexture(imgCar);
    car.setPosition(0, (windowHeight - imgCar.getSize().y) / 2);

// wall
    sf::Texture imgWall;
    Collision::CreateTextureAndBitmask( imgWall, "barreira.png" );
    sf::Sprite wall;
    wall.setTexture(imgWall);
    wall.setPosition(windowWidth - imgWall.getSize().x, 0);

    sf::RectangleShape background (sf::Vector2f(windowWidth, windowHeight));
    background.setFillColor(sf::Color::White);


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        if (!Collision::PixelPerfectTest(car, wall))
            car.move(0.1, 0);

        window.clear();
        window.setView(view);
        window.draw(background);

        window.setView(window.getDefaultView());
        window.draw(car);
        window.draw(wall);
        window.display();
    }

    return 0;
}

... if both car and wall are related with the main window,  I get a perfect car collision with the wall.





Now if I put the car inside the view ...

Code: [Select]
window.clear();
window.setView(view);
window.draw(background);
window.draw(car);

window.setView(window.getDefaultView());
window.draw(wall);
window.display();

... the collision is detected as if the wall were within the boundaries of the view (which is zoomed in).



How can I make the collision detection independent of the view?

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: SFML Pixel Perfect Collision limited to a view?
« Reply #1 on: April 27, 2018, 03:00:16 am »
Could anyone tell me how to detect collisions between different views or from a view and a global window, as in the case of the above example?

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: SFML Pixel Perfect Collision limited to a view?
« Reply #2 on: April 27, 2018, 08:30:31 am »
How can I make the collision detection independent of the view?
Collision detection is independent of the view, because the transformations of your objects don't change, you just view them from different perspectives. (So you are still technically colliding in your second example, but you don't see it)

To quote the tutorials: (which you should read and understand)
Quote
The world itself remains unchanged, what changes is just the way it is seen.

What are you trying to achieve anyways? If you just want the wall to be twice as big, why don't you scale it by that factor then?
Failing to succeed does not mean failing to progress!

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: SFML Pixel Perfect Collision limited to a view?
« Reply #3 on: April 27, 2018, 04:13:03 pm »
Collision detection is independent of the view, because the transformations of your objects don't change, you just view them from different perspectives. (So you are still technically colliding in your second example, but you don't see it)

"Technically" is not intuitive.
"View" means "to see".
What I'm seeing is a car stopping without any barrier.
If you look at the code, you will see that only the car is in the view and the wall is not.
Anyone who sees this will find it strange.
"Technically" anything can be done (remedied), but it would be much better if all of this were more intuitive.
But I guess this does not happen with SFML.

What are you trying to achieve anyways? If you just want the wall to be twice as big, why don't you scale it by that factor then?

Indeed, I am already doing this to remedy the situation. It is not just a simple resize, because if the wall has to be relative to the view for the collision to work and I want the wall to be relative to the original window, I have to consider the view offset and redo a series of calculations.

SFML is a great tool, but could be more intuitive.
In any case I'm already reading the book "SFML Essentials" and I think from there I will understand these puzzles.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: SFML Pixel Perfect Collision limited to a view?
« Reply #4 on: April 27, 2018, 07:28:59 pm »
In my opinion, you'd have a hard time finding a multimedia library that was more intuitive than SFML  ;)

Can you describe at a high level what you are trying to achieve in your game? It seems so far that you may be trying to use a sf::View in a way where other techniques could be better. It seems curious that you want your car and wall to be in different views, yet interact in this manner.

If you do want to continue using views, you need to be aware of the 2 different coordinate systems. There is the world's coordinate system and the screen's coordinate system. Moving the view changes the object's screen coordinates, but does not change where it exists in the world. This is why your car is stopping. Even though they are not colliding in the screen's coordinate system (which is what you see), they ARE colliding in the world coordinate system. SFML provides functions to map between these 2 coordinate systems.

This separation of world and screen coordinates enables things like split-screen multiplayer, or to easily layer a HUD interface on top of your game without impacting collisions and other things. You may need to step back and investigate if using a sf::View is really the right tool to solve whatever it is your game needs to do.

 

anything