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

时间:2011-07-11 08:17:30

标签: c++ rotation drawable sfml

嘿所以我做了一个'body'类,它继承了SFML中的sf :: drawable,它将形状放在一起,这样我就可以形成更复杂的形状和图形。我发现我必须在渲染时对每个成员形状做一些更新,这取决于自上次渲染以来身体发生了什么。

这包括:

  • 轮换
  • 翻译
  • 缩放

我以为我必须手动编写代码来考虑这些可能发生在整个身体上的变化,因此形状位置向右转。然而,当编写旋转部分时,我在编写手动代码后发现即使我使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]);}
        };
};

1 个答案:

答案 0 :(得分:3)

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

当您致电target.Draw(*this)时,它最终会调用Drawable::Draw(RenderTarget& Target) const,这会设置一个渲染矩阵,该矩阵具有您通过Drawable::SetRotation调用所提供的旋转。然后调用您的virtual Render函数,并设置该旋转环境。这意味着您的身体部位会旋转。

自己查看来源以获得更好的理解。 (;