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.
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).