0

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...

#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. (see image).

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

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) (see image).

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

Rogério Dec
  • 801
  • 8
  • 31
  • you are drawing the wall to a different view is that correct? Than you will probably need [this](https://www.sfml-dev.org/tutorials/2.4/graphics-view.php#coordinates-conversions) – gotocoffee Apr 25 '18 at 07:56
  • I did not understand. In my code, the problem is when the car is inside the view and the wall is out of view. In this format, Pixel Perfect misses when it detects the collision, it understands as if the wall is inside the view, in incorrect coordinates. – Rogério Dec Apr 25 '18 at 17:48
  • How does your `Collision` work? I think is key part of this question – alseether May 11 '18 at 07:46
  • In fact, I see a misconception about SFML views. When you draw things, you're drawing them **on the window**, notice that you're using `window.draw(...)`. Once things are drawn in the window, the view is applied to effectively show things on the screen. At the end, a `sf::View` is just a couple of floats determining the edges of your view, zoom, etc – alseether May 11 '18 at 07:55

0 Answers0