我在 SFML 中的"body"类:它如何自行正确旋转成员形状?

My 'body' class in SFML: How does it rotate member shapes correctly on its own?

本文关键字:旋转 成员 何自行 中的 SFML body 我在      更新时间:2023-10-16

嘿,所以我做了一个'body'类,它继承了sf::drawable在SFML中,将形状放在一起,所以我可以形成更复杂的形状和数字。我发现我必须在渲染时对每个成员的形状做一些更新,这是基于上次渲染以来身体整体发生的变化。

这个包括:

    旋转
  • 翻译

我想我必须手动编写代码来考虑这些可能发生在整个主体上的变化,所以形状位置是正确的。然而,当编码旋转部分时,我发现在编写手动代码后,即使我使Bodies Render()函数只迭代并渲染每个形状而不改变它们,它们无论如何都会以正确的方向出现。这怎么可能?

我注释掉了我写的代码,显然不需要,我使用Draw()函数来渲染。请解释给我我的自己的代码!(天哪,我觉得自己很迟钝)。

这是类:

class Body : public sf::Drawable{ 
    public:
        Body(const sf::Vector2f& Position = sf::Vector2f(0, 0), const sf::Vector2f& Scale = sf::Vector2f(1, 1), float Rotation = 0.f, const sf::Color& Col = sf::Color(255, 255, 255, 255)){
            SetPosition(Position);
            SetScale(Scale);
            SetRotation(Rotation);
            SetColor(Col);
            RotVal=0;};
////////////////// Drawable Functions   ////////////////////
        void SetX(float X){
            MoveVal.x += X - GetPosition().x;
            Drawable::SetX(X);};
        void SetY(float Y){
            MoveVal.y += Y - GetPosition().y;
            Drawable::SetY(Y);};
        void SetRotation(float Rotation){
            RotVal+= Rotation-GetRotation();
            Drawable::SetRotation(Rotation);};
////////////////////////////////////////////////////////////
        bool AddShape(sf::Shape& S){
            Shapes.push_back(S); return true;};
        bool AddSprite(sf::Sprite& S){
            Sprites.push_back(S); return true;};
        void Draw(sf::RenderTarget& target){
            for(unsigned short I=0; I<Shapes.size(); I++){
                //Body offset
                Shapes[I].SetPosition( 
                    Shapes[I].GetPosition().x + MoveVal.x,
                    Shapes[I].GetPosition().y + MoveVal.y);
                // Aparrently Body shapes rotate on their own...
                //WWTFFFF>>>>??????????
                //Body Rotation
                //float px= GetPosition().x,
                //    py= GetPosition().y,
                //    x=  Shapes[I].GetPosition().x,
                //    y=  Shapes[I].GetPosition().y,
                //   rot= ConvToRad(RotVal);
                /*Shapes[I].SetPosition( 
                    px + ((x-px)*cos(rot)) - ((y-py)*sin(rot)),
                    py - ((x-px)*sin(rot)) + ((y-py)*cos(rot)));*/ //TODO: put this in a math header
                //Shapes[I].Rotate(RotVal);
            }
            target.Draw(*this); // draws each individual shape
            //Reset all the Change Values
            RotVal=0;
            MoveVal.x=0;
            MoveVal.y=0;
        };
    private:
        sf::Vector2f MoveVal;
        float         RotVal;
        std::vector<sf::Shape> Shapes;
        std::vector<sf::Sprite> Sprites;
        virtual void Render(sf::RenderTarget& target) const{                
            for(unsigned short I=0; I<Shapes.size(); I++){
                target.Draw(Shapes[I]);}
            for(unsigned short I=0; I<Sprites.size(); I++){
                target.Draw(Sprites[I]);}
        };
};

我的猜测得到了SFML源代码的证实。

当你调用target.Draw(*this)时,它最终会调用Drawable::Draw(RenderTarget& Target) const,它会建立一个渲染矩阵,该矩阵具有你在Drawable::SetRotation调用时给它的旋转。然后调用virtual Render函数,并设置旋转环境。这意味着你的身体部位会被旋转。

自己看看源代码,以获得更好的理解。(;