SFML Pixel Perfect Collision仅限于视图?

时间:2018-04-25 04:01:15

标签: c++ collision sfml pixel-perfect

在这段代码中,我有一个视图(白色,缩放2)和原始窗口,还有一个用于汽车的精灵和一个用于墙壁的精灵......

#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;
}

......如果汽车和墙壁都与主窗口相关,我会与墙壁完全碰撞。 (see image)

现在,如果我把车放在视野内......

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

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

...检测到碰撞,就好像墙在视图的边界内(放大了)(see image)

如何使碰撞检测独立于视图?

0 个答案:

没有答案