Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: (Solved) Expanding size of rotated quads vertices - my method  (Read 8179 times)

0 Members and 1 Guest are viewing this topic.

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Hello,
I used a pretty long code to achieve what i wanted, meaning : being able to rotate, move AND expand an item made of vertices in any situation (whichever is value of the rotation angle).
I want first to explain the method i used, then i would like to ask hero members and non hero members, to show me how would they have achieved that with OTHER METHODS? Maybe some advanced methods/functions, or just different methods, i think discovering your methods for doing it might enlarge my understanding and offer a new view in programming, right now i just had to spend some with a paper and a pen to figure out how to achieve the expand item at any rotation angle, but i am pretty sure you guys know way faster methods to do it. So this is the purpose of this topic.
My method :

First of all, i made quad 4-vertices and made a function to rotate it.
sf::VertexArray CenterObj(sf::Quads,4);
    CenterObj[0].position=sf::Vector2f(300,300);
    CenterObj[1].position=sf::Vector2f(400,300);
    CenterObj[2].position=sf::Vector2f(400,400);
    CenterObj[3].position=sf::Vector2f(300,400);
 



Then i made a function that would "expand" it (increase the size of the quad vertices overall), it depends on the angle of rotation, the global rotation angle is saved in a global variables called (save) which grows any time the rotate function is called.
ang=0;
save=ang;
..
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
                {
                    ang=4;
                 pivot(CenterObj,ang,4,CenterObj);
                save=save+ang;
}
The rotation function (pivot) here is not important.

If the global rotation(save) is between 0 and 45, the expand function would behave in a certain manner, if the value of the global rotation (save) is between 45 and 90, it would behave in a different manner, same thing if it is between 90 and 135 and finally  between 135 and 180. if save is above 179 it will go back to 0 (using "%"). So we have many areas.. many "zones"
Last observation, all this regions are reversed if the "save" is between 180 and 360.

float zone=save%180;
So in each case i will calculate 2 variables i call "differenceX" and "differenceY", and these 2 variables are the ones that are used to change the values of vertices. This image show them, the algorithm is just below the picture :

void NewExpand(sf::VertexArray &V,float ExpandValue,char item,int NumberOfVertices)
{
    //save=ang;
    float zone=save%180;
//    cout << "save : " << save << endl;
    cout << " zone(real angle) : " << zone << endl;
  if (V.getVertexCount()==4)
  {
    if(zone==0)
    {
      V[0].position.x=V[0].position.x-ExpandValue; V[0].position.y=V[0].position.y-ExpandValue;
      V[1].position.x=V[1].position.x+ExpandValue; V[1].position.y=V[1].position.y-ExpandValue;
      V[2].position.x=V[2].position.x+ExpandValue; V[2].position.y=V[2].position.y+ExpandValue;
      V[3].position.x=V[3].position.x-ExpandValue; V[3].position.y=V[3].position.y+ExpandValue;
      float test = V[0].position.x;
//      cout << "value of of the quad vertices (case 0): " << test << " " << V[1].position.x << " " << V[2].position.x << " " << V[3].position.x << endl;
    }
   else if(zone>0&&zone<45){ Angle2=45-save; /// /zone A
        float PivotedSideRectangle = sqrt((V[1].position.y-V[0].position.y)*(V[1].position.y-V[0].position.y) + (V[1].position.x-V[0].position.x)*(V[1].position.x-V[0].position.x)) ;
        OldHypotenuse=COS(45)*PivotedSideRectangle;
        float NewHypotenuse=OldHypotenuse+ExpandValue;
//        float ExpandedSideNewRect=NewHypotenuse/COS(45); //Idk if this is useful
       float NewY=NewHypotenuse*COS(Angle2); // It's Y on the window eventhouth cosinus
       float NewX=NewHypotenuse*SIN(Angle2);
       float Ypart1=COS(Angle2)*OldHypotenuse;
       float Xpart1=SIN(Angle2)*OldHypotenuse;
       float differenceY=abs(NewY-Ypart1);
       float differenceX=abs(NewX-Xpart1);
    if(save%360>=180)
       {
       V[0].position.x=V[0].position.x+differenceX;
       V[0].position.y=V[0].position.y+differenceY;
       ///
       V[1].position.x=V[1].position.x-differenceY;
       V[1].position.y=V[1].position.y+differenceX;
       ///
       V[2].position.x=V[2].position.x-differenceX;
       V[2].position.y=V[2].position.y-differenceY;
       ///
       V[3].position.x=V[3].position.x+differenceY;
       V[3].position.y=V[3].position.y-differenceX;
       }
       else{
       V[0].position.x=V[0].position.x-differenceX;
       V[0].position.y=V[0].position.y-differenceY;
       ///
       V[1].position.x=V[1].position.x+differenceY;
       V[1].position.y=V[1].position.y-differenceX;
       ///
       V[2].position.x=V[2].position.x+differenceX;
       V[2].position.y=V[2].position.y+differenceY;
       ///
       V[3].position.x=V[3].position.x-differenceY;
       V[3].position.y=V[3].position.y+differenceX;
       }
//       cout << "value of of the quad vertices (case <45) : " << V[0].position.x << " " << V[1].position.x << " " << V[2].position.x << " " << V[3].position.x << endl;
}
 else if(zone==45)
        {
        if(save%360>=180)
       {
          V[0].position.y=V[0].position.y+ExpandValue;
          V[1].position.x=V[1].position.x-ExpandValue;
          V[2].position.y=V[2].position.y-ExpandValue;
          V[3].position.x=V[3].position.x+ExpandValue;
       }
       else
        {
       V[0].position.y=V[0].position.y-ExpandValue;
       V[1].position.x=V[1].position.x+ExpandValue;
       V[2].position.y=V[2].position.y+ExpandValue;
       V[3].position.x=V[3].position.x-ExpandValue;
    }}

 else if(zone>45&&zone<90) /// Zone A2
    {Angle2=save-45;
        double PivotedSideRectangle = sqrt((V[1].position.y-V[0].position.y)*(V[1].position.y-V[0].position.y) + (V[1].position.x-V[0].position.x)*(V[1].position.x-V[0].position.x)) ;
        OldHypotenuse=COS(45)*PivotedSideRectangle;
        float NewHypotenuse=OldHypotenuse+ExpandValue;
        float NewY=NewHypotenuse*COS(Angle2); // It's Y on the window eventhouth cosinus
        float NewX=NewHypotenuse*SIN(Angle2);
        float Ypart1=COS(Angle2)*OldHypotenuse;
        float Xpart1=SIN(Angle2)*OldHypotenuse;
        double differenceY=abs(NewY-Ypart1);
        double differenceX=abs(NewX-Xpart1);
//            cout << PivotedSideRectangle << endl;
//            cout << OldHypotenuse << endl;
        if(save%360>=180)
       {
       V[0].position.x=V[0].position.x-differenceX;
       V[0].position.y=V[0].position.y+differenceY;
       ///
       V[1].position.x=V[1].position.x-differenceY;
       V[1].position.y=V[1].position.y-differenceX;
       ///
       V[2].position.x=V[2].position.x+differenceX;
       V[2].position.y=V[2].position.y-differenceY;
       ///
       V[3].position.x=V[3].position.x+differenceY;
       V[3].position.y=V[3].position.y+differenceX;
       }
       else
       {
       V[0].position.x=V[0].position.x+differenceX;
       V[0].position.y=V[0].position.y-differenceY;
       ///
       V[1].position.x=V[1].position.x+differenceY;
       V[1].position.y=V[1].position.y+differenceX;
       ///
       V[2].position.x=V[2].position.x-differenceX;
       V[2].position.y=V[2].position.y+differenceY;
       ///
       V[3].position.x=V[3].position.x-differenceY;
       V[3].position.y=V[3].position.y-differenceX;
//       cout << "value of of the quad vertices (Case under 90) : " << V[0].position.x << " " << V[1].position.x << " " << V[2].position.x << " " << V[3].position.x << endl;
//       cout << "value of of the quad vertices.Y (Case under 90) : " << V[0].position.y << " " << V[1].position.y << " " << V[2].position.y << " " << V[3].position.y << endl;
}}
else if(zone==90)
        {
             if(save%360>=180)
       {
       V[0].position.x=V[0].position.x-ExpandValue;
       V[1].position.y=V[1].position.y-ExpandValue;
       V[2].position.x=V[2].position.x+ExpandValue;
       V[3].position.y=V[3].position.y+ExpandValue;
       }
       else
       {
       V[0].position.x=V[0].position.x+ExpandValue;
       V[1].position.y=V[1].position.y+ExpandValue;
       V[2].position.x=V[2].position.x-ExpandValue;
       V[3].position.y=V[3].position.y-ExpandValue;
    }}
else if(zone>90&&zone<135)
    {Angle2=90+45-save; /// //Zone B
            double PivotedSideRectangle = sqrt((V[1].position.y-V[2].position.y)*(V[1].position.y-V[2].position.y) + (V[1].position.x-V[2].position.x)*(V[1].position.x-V[2].position.x)) ;
            OldHypotenuse=COS(45)*PivotedSideRectangle;
        float NewHypotenuse=OldHypotenuse+ExpandValue;
        float ExpandedSideNewRect=NewHypotenuse/COS(45); //Idk if this is useful
       float NewY=NewHypotenuse*COS(Angle2); // It's Y on the window eventhouth cosinus
       float NewX=NewHypotenuse*SIN(Angle2);
       float Ypart1=COS(Angle2)*OldHypotenuse;
       float Xpart1=SIN(Angle2)*OldHypotenuse;
       float differenceY=abs(NewY-Ypart1);
       float differenceX=abs(NewX-Xpart1);
//        cout << "angle2 :" << Angle2 << endl;
     if(save%360>=180)
       {
       V[2].position.x=V[2].position.x+differenceY;
       V[2].position.y=V[2].position.y-differenceX;
       V[3].position.x=V[3].position.x+differenceX;
       V[3].position.y=V[3].position.y+differenceY;
       V[0].position.x=V[0].position.x-differenceY;
       V[0].position.y=V[0].position.y+differenceX;
       V[1].position.x=V[1].position.x-differenceX;
       V[1].position.y=V[1].position.y-differenceY;
       }
       else
       {
       V[2].position.x=V[2].position.x-differenceY;
       V[2].position.y=V[2].position.y+differenceX;
       V[3].position.x=V[3].position.x-differenceX;
       V[3].position.y=V[3].position.y-differenceY;
       V[0].position.x=V[0].position.x+differenceY;
       V[0].position.y=V[0].position.y-differenceX;
       V[1].position.x=V[1].position.x+differenceX;
       V[1].position.y=V[1].position.y+differenceY;
//          cout << "value of of the quad vertices (Case under 135) : " << V[0].position.x << " " << V[1].position.x << " " << V[2].position.x << " " << V[3].position.x << endl;
//          cout << "value of of the quad vertices.Y (Case under 135) : " << V[0].position.y << " " << V[1].position.y << " " << V[2].position.y << " " << V[3].position.y << endl;


}}
else if(zone==135)
        {
            if(save%360>=180)
       {
        V[0].position.y=V[0].position.y-ExpandValue;
       V[1].position.x=V[1].position.x+ExpandValue;
       V[2].position.y=V[2].position.y+ExpandValue;
       V[3].position.x=V[3].position.x-ExpandValue;
       }
       else
       {
       V[0].position.y=V[0].position.y+ExpandValue;
       V[1].position.x=V[1].position.x-ExpandValue;
       V[2].position.y=V[2].position.y-ExpandValue;
       V[3].position.x=V[3].position.x+ExpandValue;
    }}
else if(zone>135&&zone<180){Angle2=save-(45+90);
        float PivotedSideRectangle = sqrt((V[1].position.y-V[0].position.y)*(V[1].position.y-V[0].position.y) + (V[1].position.x-V[0].position.x)*(V[1].position.x-V[0].position.x)) ;
        OldHypotenuse=COS(45)*PivotedSideRectangle;
        float NewHypotenuse=OldHypotenuse+ExpandValue;
        float NewY=NewHypotenuse*COS(Angle2); // It's Y on the window eventhouth cosinus
        float NewX=NewHypotenuse*SIN(Angle2);
        float Ypart1=COS(Angle2)*OldHypotenuse;
        float Xpart1=SIN(Angle2)*OldHypotenuse;
        float differenceY=abs(NewY-Ypart1);
        float differenceX=abs(NewX-Xpart1);

        if(save%360>=180)
       {
       V[2].position.x=V[2].position.x+differenceY;
       V[2].position.y=V[2].position.y+differenceX;
       V[3].position.x=V[3].position.x-differenceX;
       V[3].position.y=V[3].position.y+differenceY;
       V[0].position.x=V[0].position.x-differenceY;
       V[0].position.y=V[0].position.y-differenceX;
       V[1].position.x=V[1].position.x+differenceX;
       V[1].position.y=V[1].position.y-differenceY;}
       else {
       V[2].position.x=V[2].position.x-differenceY;
       V[2].position.y=V[2].position.y-differenceX;
       V[3].position.x=V[3].position.x+differenceX;
       V[3].position.y=V[3].position.y-differenceY;
       V[0].position.x=V[0].position.x+differenceY;
       V[0].position.y=V[0].position.y+differenceX;
       V[1].position.x=V[1].position.x-differenceX;
       V[1].position.y=V[1].position.y+differenceY;
//        cout << "value of of the quad vertices (Case under 180) : " << V[0].position.x << " " << V[1].position.x << " " << V[2].position.x << " " << V[3].position.x << endl;
//        cout << "value of of the quad vertices.Y (Case under 180) : " << V[0].position.y << " " << V[1].position.y << " " << V[2].position.y << " " << V[3].position.y << endl;
           }
                             }
  } /// end of : if (V.getVertexCount()==4)
} /// end of function
 

Inside the main, i will call the rotate function "pivot" and check the value of angle, so at each zone i will test the 'NewExpand" function and see if it works. (as shown in the second code of this post, using the letter "Q", check above). Well it does! :)



I am proud to have achieved it. Now i would be happy to see how you do it guys? I have no doubt there are much better and faster and more powerful methods to associate an expand function with a rotate and a movement one for a groupe of vertices or other, with a result expeceted similar to this one :


https://gfycat.com/poisedthinhartebeest
« Last Edit: April 19, 2020, 03:38:50 pm by Power »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Expanding size of rotated quads vertices - my method
« Reply #1 on: April 11, 2020, 08:06:47 am »
That's certainly a good exercise for learning some important math stuff.

However, in real life, you'd just use sf::Transformable or sf::Transform, to achieve the same things with the translation, rotation and scale functions. SFML does all the maths for you.
Laurent Gomila - SFML developer

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Expanding size of rotated quads vertices - my method
« Reply #2 on: April 11, 2020, 10:19:49 am »
Yes indeed, it was really fun to rewrite everything on paper and try hard to find the relation between all the available angles and finally be able to find how to expand the shape.. the way i wanted.
It was also.. painful, painfully good but painful, because it takes some time.

The irony here, is that this is the only way i know of, that achieves this result, and i was hoping by writing all of this, that someone would be encouraged enough to show me how it is done with  "sf::Transformable or sf::Transform" as you say.

I don't wish to continue like that for future crazy interactions i plan for my entities on my future projects, i must learn  the way you do it.


I would like to see just an example using my quad vertices :
sf::VertexArray CenterObj(sf::Quads,4);
    CenterObj[0].position=sf::Vector2f(300,300);
    CenterObj[1].position=sf::Vector2f(400,300);
    CenterObj[2].position=sf::Vector2f(400,400);
    CenterObj[3].position=sf::Vector2f(300,400);
 
( this is the same one from the OP)
And see :
1) How would you inherit it from transformable class.
2) How would you rotate it ... around its center or around any point of the window (not interested in the classical rotation around upper left point of an "item")
3) how to scale it....


___
While i am writing this, and by thinking about what you wrote yesterday, an idea popped in my head, maybe i understood what you meant :
so you guys make vertices shape, put it on a "sprite" and then only after that, you would apply the transform on the item resulted?
I'd rather see an example, i am pretty sure it's easy as cake for you other hero members. I kinda saturated my mind a bit briefly.


« Last Edit: April 11, 2020, 11:27:33 am by Power »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Expanding size of rotated quads vertices - my method
« Reply #3 on: April 11, 2020, 02:57:18 pm »
Quote
1) How would you inherit it from transformable class.
https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php
With real-life, complete examples at the end.

Quote
2) How would you rotate it ... around its center or around any point of the window (not interested in the classical rotation around upper left point of an "item")
If it's a sf::Transformable entity, play with setRotation and setOrigin.
If using a sf::Transform, see https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Transform.php#ad2a2520ad81724079d109d4a986f9902
(the way it's done is simple: translate to center of rotation, rotate, apply reverse translation).

Quote
3) how to scale it....
sf::Transformable::setScale
sf::Transform::scale
Laurent Gomila - SFML developer

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Expanding size of rotated quads vertices - my method
« Reply #4 on: April 18, 2020, 12:52:03 pm »
Ok so i tried to inherit a class from the "drawable" and "transformable" classes, i named : VertexClass
class VertexClass : public sf::Drawable, public sf::Transformable
{
private:
 virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const    {
 states.transform *= getTransform();
 states.texture = &m_tileset;
 target.draw(m_vertices, states);
}
sf::VertexArray m_vertices; // https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1VertexArray.php
sf::Texture m_tileset;

 public:

    bool construction()
{
 m_vertices.setPrimitiveType(sf::Quads);
 sf::Vertex* quad = &m_vertices[0];
                // define its 4 corners
 quad[0].position = sf::Vector2f(100,100);
 quad[1].position = sf::Vector2f(150,100);
 quad[2].position = sf::Vector2f(150,150);
 quad[3].position = sf::Vector2f(100,150);
 quad[0].color = sf::Color::Red;
 quad[1].color = sf::Color::Yellow;
 quad[2].color = sf::Color::Black;
 quad[3].color = sf::Color::Blue;

return true;
}
};
 

Just for the sake of experimentation, i did not try any rotation or transformation, i just wanted to test the draw function, following the TILEMAP example,
int main()
{
 sf::ContextSettings contextSettings;
 contextSettings.depthBits = 24;
 contextSettings.stencilBits = 8;
 contextSettings.antialiasingLevel = 0;
 contextSettings.majorVersion = 3;
 contextSettings.minorVersion = 3;
sf::RenderWindow window(sf::VideoMode(width, height), "It Works!", sf::Style::Close, contextSettings);
window.setFramerateLimit(30);
VertexClass VC; // class

while (window.isOpen())
{
 sf::Event event;
 while (window.pollEvent(event))
 {
  if (event.type == sf::Event::Closed)
  window.close();
        }
 window.clear(sf::Color::Black);
 window.draw(VC);
 window.display();
    }
    return 0;
}
 

I get a black screen. Why is that?
« Last Edit: April 18, 2020, 12:54:22 pm by Power »

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Expanding size of rotated quads vertices - my method
« Reply #5 on: April 18, 2020, 02:34:23 pm »
I think i know what's the problem,
It's because draw derives from the "states" variable which takes an empty texture (&tileset), the question now is :

Is there a way to enforce the colors of the vertex i made (quad[0].color = sf::Color::Red; ..etc) to the state variable, and thus to the final result?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Expanding size of rotated quads vertices - my method
« Reply #6 on: April 18, 2020, 02:52:04 pm »
You should really indent your code correctly, it is unreadable. I don't even know how you can read and understand your code yourself.

Quote
I get a black screen. Why is that?
Probably bad luck, your code is more likely to trigger a crash ;)
You never allocate those 4 vertices inside the vertex array, unless I missed something.

Quote
Is there a way to enforce the colors of the vertex i made (quad[0].color = sf::Color::Red; ..etc) to the state variable, and thus to the final result?
Vertex color and texture information are modulated (multiplied) together. If your texture is not initialized then don't use it (states.texture = nullptr), and you should see just the vertex color.
Laurent Gomila - SFML developer

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Expanding size of rotated quads vertices - my method
« Reply #7 on: April 19, 2020, 03:35:21 pm »
Thanks for all the help, i have finally done it.
Here is vertex class :
class VertexFun : public sf::Drawable, public sf::Transformable
{
   private:

       sf::VertexArray V;
       sf::Texture Tex;
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const    {
        states.transform *= getTransform();

        // apply the tileset texture
        states.texture = &Tex;
        //states.texture = nullptr;

        // draw the vertex array
        target.draw(V, states);
        }

   public:



        bool VertexShapeContruction3(const std::string& address,sf::Vector2f v0,sf::Vector2f v1,sf::Vector2f v2 )
        {
            if (!Tex.loadFromFile(address)) // load the tileset texture
            {return false;}

              V.setPrimitiveType(sf::Triangles);
              V.resize(3);
              sf::Vertex* quad = &V[0];

                // define its 4 corners
                quad[0].position = v0;
                quad[1].position = v1;
                quad[2].position = v2;
                quad[0].texCoords = v0;
                quad[1].texCoords = v1;
                quad[2].texCoords = v2;
//                 sf::Vertex vertexTab[4] = /// METHODE 3 (Tab)
//                        {
//                            sf::Vertex(sf::Vector2f(400,400),sf::Color::Black),
//                            sf::Vertex(sf::Vector2f(460,450),sf::Color::Yellow),
//                            sf::Vertex(sf::Vector2f(460,500),sf::Color::Blue),
//                            sf::Vertex(sf::Vector2f(400,400),sf::Color::Red)
//                        };
        return true;
        }


        bool VertexShapeContruction4(const std::string& address,sf::Vector2f v0,sf::Vector2f v1,sf::Vector2f v2,sf::Vector2f v3 )
        {
            if (!Tex.loadFromFile(address)) // load the tileset texture
            {return false;}

            V.setPrimitiveType(sf::Quads);
              V.resize(4);
              sf::Vertex* quad = &V[0];

                quad[0].position = v0;
                quad[1].position = v1;
                quad[2].position = v2;
                quad[3].position = v3;
                quad[0].texCoords = v0;
                quad[1].texCoords = v1;
                quad[2].texCoords = v2;
                quad[3].texCoords = v3;
        return true;
        }


};
 
And here is the the use of it :
 VertexFun vertexFun1,vertexFun2,vertexFun3,vertexFun4,vertexFun5;
    vertexFun1.VertexShapeContruction3("data/Thing.png",sf::Vector2f(100,9),sf::Vector2f(123,75),sf::Vector2f(78,75));
    vertexFun2.VertexShapeContruction3("data/Thing.png",sf::Vector2f(190,97),sf::Vector2f(123,122),sf::Vector2f(123,75));
    vertexFun3.VertexShapeContruction3("data/Thing.png",sf::Vector2f(100,186),sf::Vector2f(78,122),sf::Vector2f(123,122));
    vertexFun4.VertexShapeContruction3("data/Thing.png",sf::Vector2f(11,97),sf::Vector2f(78,122),sf::Vector2f(78,75));
    vertexFun5.VertexShapeContruction4("data/Thing.png",sf::Vector2f(123,122),sf::Vector2f(123,75),sf::Vector2f(78,75),sf::Vector2f(78,122));
    ///
    sf::Transform tr;
    vertexFun1.setPosition(100,100); // 100/100 is the center of the original "thing" texture?
    vertexFun2.setPosition(100,100);
    vertexFun3.setPosition(100,100);
    vertexFun4.setPosition(100,100);
    vertexFun5.setPosition(100,100);
    vertexFun1.setOrigin(100,100);
    vertexFun2.setOrigin(100,100);
    vertexFun3.setOrigin(100,100);
    vertexFun4.setOrigin(100,100);
    vertexFun5.setOrigin(100,100);

    float scl=1;
    bool rot=0,lef=0,rig=0,up=0,dow=0,ex=0,con=0;

    while (window.isOpen())
    {


        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed||event.key.code == sf::Keyboard::Escape)
                window.close();

             if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) ///
                {rot=1;}
               if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::R)
                {rot=0;}
               if(event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
              {
                 cout << " The coordinates : " << sf::Mouse::getPosition(window).x << " " << sf::Mouse::getPosition(window).y << endl;

              }
              if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) ///
                {rig=1;}
               if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Right)
                {rig=0;}
              if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) ///
                {lef=1;}
               if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Left)
                {lef=0;}
              if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) ///
                {up=1;}
               if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Up)
                {up=0;}
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) ///
                {dow=1;}
               if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Down)
                {dow=0;}
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) ///
                {ex=1;}
               if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::E)
                {ex=0;}
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::C)) ///
                {con=1;}
               if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::C)
                {con=0;}
        }

        if(rot==1)
        {
                vertexFun5.rotate(15);
                vertexFun4.rotate(15);
                vertexFun3.rotate(15);
                vertexFun2.rotate(15);
                vertexFun1.rotate(15);
        }
        if(rig==1)
        {
                vertexFun5.move(sf::Vector2f(10,0));
                vertexFun4.move(10,0);
                vertexFun3.move(10,0);
                vertexFun2.move(10,0);
                vertexFun1.move(10,0);
        }
        if(rig==1)
        {
                vertexFun5.move(sf::Vector2f(10,0));
                vertexFun4.move(10,0);
                vertexFun3.move(10,0);
                vertexFun2.move(10,0);
                vertexFun1.move(10,0);
        }
        if(lef==1)
        {
                vertexFun5.move(sf::Vector2f(-10,0));
                vertexFun4.move(-10,0);
                vertexFun3.move(-10,0);
                vertexFun2.move(-10,0);
                vertexFun1.move(-10,0);
        }
        if(up==1)
        {
                vertexFun5.move(sf::Vector2f(0,-10));
                vertexFun4.move(0,-10);
                vertexFun3.move(0,-10);
                vertexFun2.move(0,-10);
                vertexFun1.move(0,-10);
        }
        if(dow==1)
        {
                vertexFun5.move(sf::Vector2f(0,10));
                vertexFun4.move(0,10);
                vertexFun3.move(0,10);
                vertexFun2.move(0,10);
                vertexFun1.move(0,10);
        }
        if(ex==1)
        {
                 if(scl<=1.5)
                 {
                     scl=scl+0.1;
                    vertexFun5.setScale(scl,scl);
                    vertexFun4.setScale(scl,scl);
                    vertexFun3.setScale(scl,scl);
                    vertexFun2.setScale(scl,scl);
                    vertexFun1.setScale(scl,scl);
                    cout << " current scale : " << scl << endl;
                  }
        }
        if(con==1)
        {
                if(scl>=0.1)
                  {
                      scl=scl-0.1;


                    vertexFun5.setScale(scl,scl);
                    vertexFun4.setScale(scl,scl);
                    vertexFun3.setScale(scl,scl);
                    vertexFun2.setScale(scl,scl);
                    vertexFun1.setScale(scl,scl);
                    cout << " current scale : " << scl << endl;
                  }
        }


        window.clear(sf::Color::White);
        window.draw(vertexFun1);
        window.draw(vertexFun2);
        window.draw(vertexFun3);
        window.draw(vertexFun4);
        window.draw(vertexFun5);

        window.display();
    }
 

I can finally do what i wanted from the beginning.
Be able to rotate/scale/move a vertex shape any way i want. Here is my feedback :

__________
1) I feel i have been disoriented by 2 things since the beginning :
A) https://en.sfml-dev.org/forums/index.php?topic=26984.0 in this post someone suggested me to use the matrix :
[cos(A)     -sin(A)]
[sin(A)      cos(A)]
And then i continued here https://en.sfml-dev.org/forums/index.php?topic=27070.0
And nobody told to not bother with this kind of matrix to focus on the transformable inhertence possibility, at that moment.

B) Why is the Vertex class, not a transformable from the beginning ??

2) Is it mondatory to have the inheritence from the Drawable class first :
class VertexFun : public sf::Drawable, public sf::Transformable
I am asking because this form
class VertexFun :  public sf::Transformable, public sf::Drawable
Gave me an error message.

3) The gap between the skills required to "ingurgitate" the first first 80% part from the tutorial on transformations is huge between it and and the last examples (Tilemap example). I mean inside that example, there are "resize" functions that a 2 month C++ newcomer would not expect to be part of C++ and not part of the class transformable, also there are useless variables such as the "tv" variable, etc..
Maybe put a simpler example, more graduate.

4) While reading the transformation tutorial : https://www.sfml-dev.org/tutorials/2.5/graphics-transform.php
I read :
Quote
To keep things simple, there's only a single origin for all three transformation components. This means that you can't position an entity relative to its top-left corner while rotating it around its center for example. If you need to do such things, have a look at the next chapters.

I looked inside this chapter and inside the following tutorials, i could find it and still don't. I figured out how it works thogh (i think).

Trying to give a constructive feedback ofcourse, i respect a lot this whole work that has beed done and i,..myself would never be able to bring ou such an awesome website/tool/tutorial(s) to the world (right now).
So thanks again for the great tool that SFML is, and thanks for the help.
« Last Edit: April 19, 2020, 03:37:59 pm by Power »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: (Solved) Expanding size of rotated quads vertices - my method
« Reply #8 on: April 19, 2020, 03:54:50 pm »
Quote
in this post someone suggested me to use the matrix
Yes, that was a very bad advice ;D but maybe that person misunderstood your request (I don't know, I haven't read the topic).

Quote
B) Why is the Vertex class, not a transformable from the beginning ??
Do you mean sf::VertexArray?

Quote
Is it mondatory to have the inheritence from the Drawable class first
Nop. What error did you get?

Quote
I looked inside this chapter and inside the following tutorials, i could find it and still don't. I figured out how it works thogh (i think).
That was probably referring to sf::Transform.

Quote
The gap between the skills required to "ingurgitate" the first first 80% part from the tutorial on transformations is huge between it and and the last examples (Tilemap example)
Although we try to target beginners in both SFML and C++, we can't really make all examples and tutorials available to C++ newcomers. Some pieces of code require some minimal experience with the language. For things that you don't understand with SFML calls, the API documentation and forum are there to complete the knowledge.

Quote
Trying to give a constructive feedback ofcourse
Thanks for taking the time to do so, that's appreciated ;)

And I'm glad you finally found your way through the SFML API. It's true that you started in a wrong direction, things could have been a lot smoother ;D
Laurent Gomila - SFML developer

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: (Solved) Expanding size of rotated quads vertices - my method
« Reply #9 on: April 19, 2020, 04:09:52 pm »
Haha yes it's insane how i went astray, but happy to have found the road.
Quote
Do you mean sf::VertexArray?
Yes! I feel like all classe using Vertices will be inheriting from the transformable class at some point. Maybe there is a purpose having it separated from the other "shape" classes?
Quote
Nop. What error did you get?
I remember having a problem at some time, i definitely had, but i just tried again and no error;).

Quote
That was probably referring to sf::Transform.
Humm really? i don't know i don't think so, it seemed like the tutorial said an example or an explantion on how to use the setOrigin to make a shape rotate around its center or other points, will be given. But there were none. (But you know your tutorials better than me:p)

___

Finally, yes i understand that you can't teach C++ here, after all its not called "C++ tutorials" it's all about SFML.

Quote
Thanks for taking the time to do so, that's appreciated ;)
My pleasure ;D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: (Solved) Expanding size of rotated quads vertices - my method
« Reply #10 on: April 19, 2020, 06:34:56 pm »
Quote
Yes! I feel like all classe using Vertices will be inheriting from the transformable class at some point. Maybe there is a purpose having it separated from the other "shape" classes?
sf::VertexArray is a lower level class. It is not the final entity, but it is generally used by those to store their geometry. Making them inherit from sf::Transformable would make this base class redundant in every other drawable class.
The idiom is really to wrap this class (typically with a texture, shader, or whatever) in a higher level drawable / transformable class.

Quote
Humm really? i don't know i don't think so, it seemed like the tutorial said an example or an explantion on how to use the setOrigin to make a shape rotate around its center or other points
So you were talking about explanations on setOrigin itself? Not how to use multiple origins (one for each transformation)?
Laurent Gomila - SFML developer

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: (Solved) Expanding size of rotated quads vertices - my method
« Reply #11 on: April 20, 2020, 04:44:29 pm »
Quote
Yes! I feel like all classe using Vertices will be inheriting from the transformable class at some point. Maybe there is a purpose having it separated from the other "shape" classes?
sf::VertexArray is a lower level class. It is not the final entity, but it is generally used by those to store their geometry. Making them inherit from sf::Transformable would make this base class redundant in every other drawable class.
The idiom is really to wrap this class (typically with a texture, shader, or whatever) in a higher level drawable / transformable class.
I can only imagine.
Quote
Quote
Humm really? i don't know i don't think so, it seemed like the tutorial said an example or an explantion on how to use the setOrigin to make a shape rotate around its center or other points
So you were talking about explanations on setOrigin itself? Not how to use multiple origins (one for each transformation)?
Both, because both points collide, making it rotate around itself = changing its origin, and giving it multiple origins for multiple transformations, means before every transformation go back the approriate Origin?
Anyway the tutorial says : "read the next chapters", was it referring to the advanced 3x3 matrix transform? Where are these "next chapters?"  ;D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: (Solved) Expanding size of rotated quads vertices - my method
« Reply #12 on: April 20, 2020, 06:13:08 pm »
The "next chapter" is the one about sf::Transform. This is a lower level class, more flexible than sf::Transformable, and thus it allows one origin for each transformation if you really need it.

https://www.sfml-dev.org/tutorials/2.5/graphics-transform.php#custom-transforms
Laurent Gomila - SFML developer

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: (Solved) Expanding size of rotated quads vertices - my method
« Reply #13 on: April 20, 2020, 07:05:13 pm »
The "next chapter" is the one about sf::Transform. This is a lower level class, more flexible than sf::Transformable, and thus it allows one origin for each transformation if you really need it.

https://www.sfml-dev.org/tutorials/2.5/graphics-transform.php#custom-transforms

Yes that's what i thought. Gotcha