SFML community forums

Help => Graphics => Topic started by: battosaijenkins on January 16, 2020, 10:25:18 pm

Title: is it possible to rotate a line?
Post by: battosaijenkins on January 16, 2020, 10:25:18 pm
I'm having a tiny brain fart and sorry if it seems very simple to figure out, but i cant seem to rotate a line. I'm using 2 points using
Code: [Select]
sf::Vertex lines[] = {
        sf::Vertex(sf::Vector2f(pos)),
        sf::Vertex(sf::Vector2f(pos.x + ret.getRadius(), pos.y)) // <-- this one is the radius line
    };
w.draw(lines, 2, sf::Lines);
-or-
Code: [Select]
sf::Vertex lines[2];
    lines[0].position = sf::Vector2f(pos);
    lines[0].color = sf::Color::Yellow;
    lines[1].position = sf::Vector2f(pos.x + ret.getRadius(), pos.y); // <-- this one is the radius line
    lines[1].color = sf::Color::Yellow;
    w.draw(lines, 2, sf::Lines);

its for my stroke circle. I want to rotate just the radius line around the circle's center point around and around like a spinning radar and cant seem to use rotate or transform on these lines, or maybe I'm doing it wrong? I can rotate an image using trig but thought this may be possible as well
Title: Re: is it possible to rotate a line?
Post by: Laurent on January 17, 2020, 08:36:28 am
Rotating is usually done like this:

x = center.x + radius * cos(angle);
y = center.y + radius * sin(angle);

// switch +- or sin/cos depending on your axes directions and where you want 0° to be


Or you can use a properly configured sf::Transform that you pass when drawing the vertices.
Title: Re: is it possible to rotate a line?
Post by: battosaijenkins on January 17, 2020, 09:00:15 am
Rotating is usually done like this:

x = center.x + radius * cos(angle);
y = center.y + radius * sin(angle);

// switch +- or sin/cos depending on your axes directions and where you want 0° to be


Or you can use a properly configured sf::Transform that you pass when drawing the vertices.

@Laurent, thanks so much this did it. I incremented the angle by += 0.1 for a slow effect for cos(angle) and sin(angle) and it's spinning. Just out of curiosity, how can I use sf::Transform properly to pass through vertices? I was having trouble with transform and lines.
Title: Re: is it possible to rotate a line?
Post by: Laurent on January 17, 2020, 10:00:46 am
How to use sf::Transform to draw an entity:
https://www.sfml-dev.org/tutorials/2.5/graphics-transform.php#custom-transforms

How to build the right transform for what you want:
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Transform.php#af0b7cc3fed36d0fa22d5d331a779eee2
Title: Re: is it possible to rotate a line?
Post by: battosaijenkins on January 17, 2020, 08:55:04 pm
How to use sf::Transform to draw an entity:
https://www.sfml-dev.org/tutorials/2.5/graphics-transform.php#custom-transforms

How to build the right transform for what you want:
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Transform.php#af0b7cc3fed36d0fa22d5d331a779eee2

That did it. Thanks man. I couldn't figure out how to add another parameter for window.draw when I already had
Code: [Select]
w.draw(lines, 2, sf::Lines);
Passing transform to the draw function had only 2 arguments stated here
https://www.sfml-dev.org/tutorials/2.5/graphics-transform.php#custom-transforms
Code: [Select]
window.draw(entity, transform);
on a whim I just added transform at the end of
Code: [Select]
w.draw(lines, 2, sf::Lines, transform);
And then the .rotate() function had the center argument for rotation and that did the trick. Thx again Laurent!!