关于C#:我在SFML中的\\’body \\’类:如何自动正确旋转成员形状?

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

嘿,所以我做了一个'body'类,它继承自SFML中的sf :: drawable,该类将形状保持在一起,因此我可以形成更复杂的形状和图形。我发现我必须在渲染时根据自上次渲染以来整个身体发生的情况对每个单个成员形状进行一些更新。

其中包括:

  • 回转
  • 笔译
  • 缩放比例

我认为我将不得不手动编写代码来考虑可能发生在整个身体上的这些变化,因此形状位置正确了。但是,在对Rotation部分进行编码时,我在编写了手动代码后发现,即使我使Bodies Render()函数仅迭代并渲染每个形状而不更改它们,它们还是以正确的方向出现了。怎么会这样?

我注释掉了我编写的代码,显然不需要它,并且我使用Draw()函数进行渲染。请给我解释我自己的代码! (上帝,我感到迟钝)。

这是课程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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函数,并设置了旋转环境。这意味着您的身体部位会旋转。

亲自查看源代码,以更好地理解。 (;