SFML community forums

Help => General => Topic started by: Student555 on February 23, 2015, 02:41:16 am

Title: How to rotate a triangle?
Post by: Student555 on February 23, 2015, 02:41:16 am
I am having trouble rotating a triangle. The triangle I am using is a   
sf::VertexArray
.
Basically, what I want to do is rotate my triangle around a fixed vertex like the image below.


I did some Googling and found this formula

x' = x * cos(t) - y * sin(t)
y' = x * sin(t) + y * cos(t)

I have tried implementing it like this, but I have had no luck.

Here is the implementation:
        sf::Vector2f PointA(350.0f, 350.0f);
        sf::Vector2f PointB(250.0f, 340.0f);
        sf::Vector2f PointC(250.0f, 360.0f);

        triangle[0].position = PointA;
        triangle[1].position = PointB;
        triangle[2].position = PointC;

        triangle[0].color = sf::Color::Blue;
        triangle[1].color = sf::Color::Blue;
        triangle[2].color = sf::Color::Blue;
 


while loop

                PointA = sf::Vector2f(object.getPosition().x, object.getPosition().y);
                PointB = sf::Vector2f(object.getPosition().x - 100.0f, object.getPosition().y - 10.0f);
                PointC = sf::Vector2f(object.getPosition().x - 100.0f, object.getPosition().y + 10.0f);

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        PointB = sf::Vector2f(PointB.x * cos(10) - PointB.y * sin(10), PointB.x * sin(10) + PointB.y * cos(10));
                        PointC = sf::Vector2f(PointC.x * cos(10) - PointC.y * sin(10), PointC.x * sin(10) + PointC.y * cos(10));
                }
       
                triangle[0].position = PointA;
                triangle[1].position = PointB;
                triangle[2].position = PointC;

                triangle[0].color = sf::Color(0, 0, 255, 20);
                triangle[1].color = sf::Color::Blue;
                triangle[2].color = sf::Color::Blue;
 


How would any of you go about rotating the triangle of VertexArray? I have tried making the triangle into a SFML entity, but it rotates the whole triangle relative to the world origin. I want it to rotate around a fixed point, or vertex.
Title: AW: How to rotate a triangle?
Post by: eXpl0it3r on February 23, 2015, 07:54:20 am
You might want to check out the sf::Transform class. It's essentially a transformation matrix.
Title: Re: How to rotate a triangle?
Post by: Student555 on February 23, 2015, 04:19:11 pm
So I should just apply a matrix rotation on the triangle.

I have tried using the sf::transform class before, and I didn't really know how to use it. I remember m_matrix being private. How does one create a matrix using sf::transform. Do I create my own matrix then apply the transformation?
Title: Re: How to rotate a triangle?
Post by: eXpl0it3r on February 23, 2015, 04:22:35 pm
sf::Transform IS a matrix. You apply your transformation to it and then transform each of your triangle's corners with it.
Title: Re: How to rotate a triangle?
Post by: Glocke on February 24, 2015, 02:55:30 pm
sf::Transform IS a matrix. You apply your transformation to it and then transform each of your triangle's corners with it.
Exactly that's it :)

When search the API documentation, you'll find the transformPoint() (http://www.sfml-dev.org/documentation/2.2/classsf_1_1Transform.php#ac322cd8f6d606598d1aacc4d1d160ad6)-method which does exactly what eXpl0it3r described. Just traverse your vertices and apply the transformation using the method.
Title: Re: How to rotate a triangle?
Post by: Student555 on March 17, 2015, 10:44:11 pm
I got the triangle rotating, but the rotating scales and shrinks the triangle in odd ways. As seen in the pictures below.

Here is the code

        sf::Transform rotation;
        rotation.rotate(10.0f);

        sf::Transform transform = rotation;

        sf::VertexArray triangle(sf::Triangles, 3);

        sf::Vector2f PointA(350.0f, 350.0f);
        sf::Vector2f PointB(250.0f, 340.0f);
        sf::Vector2f PointC(250.0f, 360.0f);

        triangle[0].position = PointA;
        triangle[1].position = PointB;
        triangle[2].position = PointC;

        triangle[0].color = sf::Color::Blue;
        triangle[1].color = sf::Color::Blue;
        triangle[2].color = sf::Color::Blue;

while()
{
      Window.clear();

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        transform = rotation.rotate(0.1f);
                }
       

                sf::Vector2f Point_BP = transform.transformPoint(PointB);
                sf::Vector2f Point_CP = transform.transformPoint(PointC);
               

                triangle[1].position = Point_BP;
                triangle[2].position = Point_CP;
               
                triangle[0].color = sf::Color(0, 0, 255, 20);
                triangle[1].color = sf::Color::Blue;
                triangle[2].color = sf::Color::Blue;

                Window.draw(object);
                Window.draw(triangle);
}

 

My goal is to have one of my vertices in a fixed position then rotate the other vertices (Like in the first diagram in my first post).
Title: Re: How to rotate a triangle?
Post by: G. on March 17, 2015, 11:04:39 pm
I think it rotates around (0,0). That's why when you apply the rotation to only 2 points of your triangle it gets deformed.
You can pass the center of rotation as an argument to the sf::Transform::rotate (http://www.sfml-dev.org/documentation/2.2/classsf_1_1Transform.php#af0b7cc3fed36d0fa22d5d331a779eee2) function. (or rotate all 3 points)
Title: Re: How to rotate a triangle?
Post by: Student555 on March 17, 2015, 11:21:10 pm
Thanks for the quick reply G., works like a charm.

Removed the
rotation.rotate()
 

then made PointA the center, as the second parameter.