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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Power

Pages: 1 2 [3] 4 5
31
Graphics / Re: Expanding size of rotated quads vertices - my method
« 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.

32
Graphics / Re: Expanding size of rotated quads vertices - my method
« 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?

33
Graphics / Re: Expanding size of rotated quads vertices - my method
« 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?

34
In case any person find this post by doing a research, i will explain how best the code should be done to allow fluid actions :

while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
 if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {u=1;}
 if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Up)
                {u=0;}
}

/// outside of window.isOpen
if(u==1){
shiftVertex(CenterObj,10,'d',4);initialPositionY=initialPositionY-10;
shiftVertex(RightObj,10,'d',3);
shiftVertex(LeftObj,10,'d',3);
shiftVertex(UpperObj,10,'d',3);
shiftVertex(BottomObj,10,'d',3);}
}
 

Do this for every action.
It works great.

35
Graphics / Re: Expanding size of rotated quads vertices - my method
« 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.



36
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

37
Graphics / Re: Using ::transform to rotate vertices
« on: April 11, 2020, 12:16:21 am »
Quote
1. sf::Transform is meant to be a wrapper around a mathematical matrix, so in most cases you don't have to know about matrices, nor about what sf::Transform does internally. Don't use the constructor that takes 9 matrix elements if you don't know what to do with it, just ignore (you can assume it's for "advanced" uses).

2. So, to build your custom transformation, create a sf::Transform instance and translate/rotate/scale it however you like.

3. Then, from it, you can either:
- transform full entites at draw time (ie. it's the GPU that does the job), using sf::RenderStates
Alright,
Quote
- transform single points, using sf::Transform::transformPoint
Interesting, this one i missed, i will explore it next on my to SFML to learn list.
Quote
4. If you want to transform a vertex (it's position), then just write v.position = transform.transformPoint(v.position). But if it's for rendering, use sf::RenderStates instead, when you draw the entity. Transforming points explicitly is rather for computations (collisions, etc.)
Noted, good to know.
Quote
Don't forget to mention what is the problem that you're actually trying to solve (what are you trying to transform? for what purpose?), because instead of focusing on matrices we may have more direct answers to your real problem
Alright, thank you.

38
Graphics / Re: Using ::transform to rotate vertices
« on: April 10, 2020, 10:15:25 am »

Quote
What do you think Transform::rotate does?
It works for shapes but not for vertices.


So i have to find how to use rotate with vertices?
I have to create a class that inherit from transform class, and for every vertix i would be able to apply the rotate thing.. something like that?

39
Java / Re: Android studio - java - sfml
« on: April 09, 2020, 11:37:34 pm »
You can write games in C++ for Android. You should be able to port your existing SFML code to Android or iOS with very little modifications.
Very promising. Thanks a lot.

40
General / Re: Hide the files of your "game" or "app"
« on: April 09, 2020, 11:12:43 pm »
Google has quite a few results when you look for resource compiler and similar keywords. ;)

Don't forget that a simple resource hack tool, will reveal all the images and sounds anyways, so if that's the only reason, it's not really worth it. The ones that want to look at the resource files will find a way regardless - all the AAA games are being reverse engineered to some extend.
True indeed.
I guess it wanted to hide them from random close contacts who would alpha test my game/app.
Okay i will try to search and find how to do it.thx

41
Graphics / Re: Using ::transform to rotate vertices
« on: April 09, 2020, 11:08:19 pm »
Ah, more detail about that is here:
https://en.wikipedia.org/wiki/Rotation_matrix

Scroll down to "In three dimensions".

Rotating in 2D is the same as rotating in 3D around the Z axis.
Aaah Finally thank good! It's been a month i am trying to do this. Now i get it thank youu. The answer was :

@Laurent, nevermind you were right, i just wasn't familiar with rotation matrix a lot in my life recently i guess. You would add just a small, few lines to know what could be inside "mygraphicalEntity" maybe.

The last thing i did not fully understand from the transormations tutorial is how to inherit vertices from transformable class and use these functions. I will probably figure it when analysing the game examples SFML provide.

Thanks again!

42
Graphics / Re: Using ::transform to rotate vertices
« on: April 09, 2020, 10:49:22 pm »
The matrix is just a standard mathematical matrix and the calculations are standard ones too. Transform does all the things you expect a mathematical matrix to do.

To understand the actual, underlying matrix and how they work, there's a lot of good explanations here:
https://en.wikipedia.org/wiki/Matrix_(mathematics)
(particularly this part: https://en.wikipedia.org/wiki/Matrix_(mathematics)#Linear_transformations )
https://en.wikipedia.org/wiki/Square_matrix
Yes, you actually already linked this link and i have read a bit of it,
So my question here, to use this :
[cos(A)     -sin(A)]
[sin(A)      cos(A)]

I will be doing something like this?
#define SIN(x) sin(x * 3.141592653589/180)
#define COS(x) cos(x * 3.141592653589/180)
...
int main()
{

// defining a random shape
sf::RectangulareShape ...
..
float A=25.

sf::Transform t(COS(A) -SIN(A) 0,
                 SIN(A), COS(A), 0,
                 0, 0, 0);

...
..
window.draw(shape,transform)
 

?
I still don't see how it is used. (I know where the matrix with cos and sin came from, i spent few time writing on a paper and understanding it btw)

I just don't see where the sf::transform intervenes, sincerely sorry with my lack on understanding for this specific point.

43
Yes here it is
void contract(sf::VertexArray &V,float ExpandValue,char item,int NumberOfVertices)
{
    if (V.getVertexCount()==4)
  {
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;

NewXCenterShape=V[1].position.x-V[0].position.x;
NewYCenterShape=V[2].position.y-V[1].position.y;

...
...
 
It's the part about the 4 vertices in the center (the rectangle)

And then called the function :
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);
    CenterObj[0].texCoords = sf::Vector2f(80.f, 76.f);
    CenterObj[1].texCoords = sf::Vector2f(122.f, 76.f);
    CenterObj[2].texCoords = sf::Vector2f(122.f, 122.f);
    CenterObj[3].texCoords = sf::Vector2f(80.f, 122.f);

...
...
if(event.type==sf::Event::KeyPressed&&event.key.code==sf::Keyboard::C)
            {
                float ContactValue=1;
               contract(CenterObj,ContactValue,'e',4); //here the char variable is not important, it is not used, 'e' can be replaced
..
..

 

44
Graphics / Re: Using ::transform to rotate vertices
« on: April 09, 2020, 10:25:21 pm »
Quote
1) How is a 3x3 matrix relevant when we are only working on 2D spaces (sf::window has x and y coordinates only)?
Translations require an additional dimension.

Quote
2) is it possible to use the matrix 2x2 that i mentionned (with cos and sin, just above) with the "transform" class?
What do you think Transform::rotate does? ;)

Quote
3) If you have anything to add, or examples about this transform class that is different from what is said on the tutorial, i'd like to hear please.
If you feel like the documentation or tutorial is lacking important information, I'd like to hear please.

You should really take one or two days to read the documentation and tutorials (and other available resources) carefully. You ask too many questions, I have the feeling that you're not trying enough by yourself ;)
Maybe you are right about me needing to explore this more but i "feel" you are not (i may be wrong), because :
I actually had an answer about a month ago an the subject "do this three features exist?" and i spent whole lot of time RE-READING most tutorials, believe me. Proof i found myself asking if there was a full class diagramm for SFML (you can find it on the suggestions forum), because i was rewrite by hand all the functions i found and try to memorize them.


And ONLY when i suceeded in using the matrix that has been suggested to me a month ago.. i felt "free" to ask my questions. ( you can see that i separated my posts on the graphics forum by weeks). I would not ask this questions if i did not try hard to understand.


Quote
If you feel like the documentation or tutorial is lacking important information, I'd like to hear please.

Yes i feel there is 0 example for the use of a matrix with transform class, i have no idea what the matrix does within the transform and how it works, eventhough i understand where the matrix 
[cos(A)     -sin(A)]
[sin(A)      cos(A)]
came from. I know its (NewCoordinaters) = Matrix * (OldCoordinates). So i don't know if i lack more maths maybe. I just don't see what the 3x3 matrix does and i have not encoutenred "any" example using it. Not do i know how to implement the 2x2 matrix above on the class transform.
I managed to write this code :
void pivot(sf::VertexArray &V,float angle,sf::Vertex CenterCoordinates,int NumberOfVertices) // number of points
{
    for(int i=0;i<NumberOfVertices;i++)
    {
           
          OldPoint_x_shifted=V[i].position.x-CenterCoordinates.position.x;
          OldPoint_y_shifted=V[i].position.y-CenterCoordinates.position.y;
          NewPoint_x_shifted=COS(angle)*OldPoint_x_shifted-OldPoint_y_shifted*SIN(angle);
          NewPoint_y_shifted=OldPoint_x_shifted*SIN(angle)+OldPoint_y_shifted*COS(angle);
          V[i].position.x= NewPoint_x_shifted + CenterCoordinates.position.x;  //  NewPoint_x FINAL DESTINATION
          V[i].position.y= NewPoint_y_shifted + CenterCoordinates.position.y;  //  NewPoint_y FINAL DESTINATION
          cout << "i = " << i <<endl;

    }

}
Yet i don't see how to do with the class transform, you should just tell me.
Quote
What do you think Transform::rotate does?
It works for shapes but not for vertices.

Let's take this example from the tutorial  :

I still wonder to this day what each part of the 9 points of this matrix does. And how it applies to my (x,y) coordinates for my shape.

This example is perfectly understood :
sf::Transform t;
t.translate(10.f, 100.f);
t.rotate(90.f);
t.translate(-10.f, 50.f);
t.scale(0.5f, 0.75f);
It's perfectly fine, i tested it direclty.

I have no game design previous knowledge nor lot of experience with C++ so the matrix of transform might be for all of you obvious but not for me at least not through reading the turorial.

Let me try to give feedback for the turotial i have read twice :


I think i might not know how to do the inheritence from transformable class, nor will i know to use the matrix function anyway. I can't wait to know the answer for my questions and feel stupid and see how easy it was, i am really excited to know the answers.




45
You don't want to react to events, but rather check the keyboard state at every iteration of your game loop. Read the documentation and tutorials again, this is well explained ;)

https://www.sfml-dev.org/tutorials/2.5/window-inputs.php

Ah perfect,
So you the code should be like this :


if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
{
                pivot(RightObj,ang,sf::Vector2f(saveCenter1,saveCenter2),3);
                pivot(LeftObj,ang,sf::Vector2f(saveCenter1,saveCenter2),3);
                }

So you use events only to check for important events that have to happen once, like jumping
Quote
This event is the one to use if you want to trigger an action exactly once when a key is pressed or released, like making a character jump with space, or exiting something with escape.
Quote from another tutorial actually : https://www.sfml-dev.org/tutorials/2.5/window-events.php

Thanks Laurent.

Pages: 1 2 [3] 4 5