调整大小时碰撞不起作用

时间:2018-01-17 21:19:45

标签: c++ resize collision sfml scaling

似乎当我调整碰撞对象的大小时,左,右,底部,顶部是错误的并被推或者什么的。但是绘制的对象距离&#34;边界框<#34;

有点远

我已经研究了几天的碰撞和实施,我已经改进了,但仍然遇到了那些烦人的问题。

我怎样才能解决这个问题?

详细解释会对我有所帮助,如果可以,请告诉我它为什么会这样:

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

class Player{
    public:
        sf::RectangleShape rect;
        unsigned int bottom, left, right, top;

        Player(sf::Vector2f position, sf::Color color){
            rect.setPosition(position);
            rect.setFillColor(color);
        }

        void update(){
            bottom = rect.getPosition().y + rect.getSize().y;
            left = rect.getPosition().x;
            right = rect.getPosition().x + rect.getSize().x;
            top = rect.getPosition().y;    
        }

        bool collision(Player p){
            if(right < p.left || left > p.right || top > p.bottom || bottom < p.top)
                return false;

            return true;
        }
};

int main()
{
    sf::RenderWindow window(sf::VideoMode(800.0f, 1000.0f), "Collision", sf::Style::Default);
    window.setFramerateLimit(60);

    Player p1(sf::Vector2f(400.0f, 500.0f), sf::Color::Green),
           p2(sf::Vector2f(400.0f, 380.0f), sf::Color::Red);

    while(window.isOpen()){
        system("clear");
        sf::Event evnt;
        while(window.pollEvent(evnt)){
            if(evnt.type == sf::Event::Closed)
                window.close();
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
            p1.rect.move(sf::Vector2f(4.5f, 0.0f));

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
            p1.rect.move(sf::Vector2f(-4.5f, 0.0f));

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
            p1.rect.move(sf::Vector2f(0.0f, -4.5f));

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            p1.rect.move(sf::Vector2f(0.0f, 4.5f));

        p1.update();
        p2.update();

        std::cout << -(p2.right % p1.left - p2.right) << std::endl;

        p1.rect.setSize(sf::Vector2f(100.0f, 100.0f));
        p2.rect.setSize(sf::Vector2f(100.0f, 100.0f));

        p1.rect.setOrigin(sf::Vector2f(p1.rect.getSize().x / 2.0f, p1.rect.getSize().y / 2.0f));
        p2.rect.setOrigin(sf::Vector2f(p2.rect.getSize().x / 2.0f, p2.rect.getSize().y / 2.0f));

        sf::Vector2f T = p2.rect.getPosition() - p1.rect.getPosition();

        float penx = (p1.rect.getSize().x / 2.0f) + (p2.rect.getSize().x / 2.0f) - abs(T.x);

        float peny = (p1.rect.getSize().y / 2.0f) + (p2.rect.getSize().y / 2.0f) - abs(T.y);

        std::cout << "penetration is " << penx << std::endl;
        std::cout << "penetration is " << peny << std::endl;

        //if(abs(T.x) > (p1.rect.getSize().x / 2) + (p2.rect.getSize().x / 2) && abs(T.y) > (p1.rect.getSize().y / 2) + (p2.rect.getSize().y / 2)){
        if(p1.collision(p2)){
            std::cout << "collided" << std::endl;

            if(peny <= penx){
                if(T.y < 0.0f)
                    p1.rect.setPosition(sf::Vector2f(p1.rect.getPosition().x, p1.top += peny));
                if(T.y > 0.0f)
                    p1.rect.setPosition(sf::Vector2f(p1.rect.getPosition().x, p1.top -= peny));
            }
            else{
                if(T.x < 0.0f){
                    p1.rect.setPosition(sf::Vector2f(p1.left += penx, p1.rect.getPosition().y));
                }
                if(T.x > 0.0f)
                    p1.rect.setPosition(sf::Vector2f(p1.left -= penx, p1.rect.getPosition().y));
                }
            }

        std::cout << p2.rect.getSize().x << std::endl;
        std::cout << p2.rect.getSize().y << std::endl;
        std::cout << "botot, " << -(p2.bottom % p1.top - p2.bottom) << std::endl;

        window.clear(sf::Color::Black);
        window.draw(p1.rect);
        window.draw(p2.rect);
        window.display();

    }
    return 0;
}

0 个答案:

没有答案