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

Author Topic: How to rotate a triangle?  (Read 5215 times)

0 Members and 1 Guest are viewing this topic.

Student555

  • Guest
How to rotate a triangle?
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
AW: How to rotate a triangle?
« Reply #1 on: February 23, 2015, 07:54:20 am »
You might want to check out the sf::Transform class. It's essentially a transformation matrix.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Student555

  • Guest
Re: How to rotate a triangle?
« Reply #2 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: How to rotate a triangle?
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: How to rotate a triangle?
« Reply #4 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()-method which does exactly what eXpl0it3r described. Just traverse your vertices and apply the transformation using the method.
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

Student555

  • Guest
Re: How to rotate a triangle?
« Reply #5 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).
« Last Edit: March 17, 2015, 10:50:10 pm by Student555 »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How to rotate a triangle?
« Reply #6 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 function. (or rotate all 3 points)

Student555

  • Guest
Re: How to rotate a triangle?
« Reply #7 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.