-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotation algorithm.cpp
More file actions
67 lines (54 loc) · 1.68 KB
/
Rotation algorithm.cpp
File metadata and controls
67 lines (54 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <SFML/Graphics.hpp>
#include <math.h>
int main()
{
// Window size
const int WIDTH = 800;
const int HEIGHT = 600;
// Create the window
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Rotation Example");
// Create a rectangle shape
sf::RectangleShape rectangle(sf::Vector2f(200.0f, 100.0f));
rectangle.setPosition(WIDTH / 2.0f, HEIGHT / 2.0f);
rectangle.setOrigin(100.0f, 50.0f);
rectangle.setFillColor(sf::Color::Red);
// Rotation properties
bool isRotating = true;
float rotationAngle = 0.0f;
float rotationSpeed = 90.0f; // Degrees per second
sf::Clock timer;
const float animationDuration = 3.0f;
// Game loop
while (window.isOpen())
{
// Handle events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
// Clear the window
window.clear(sf::Color::Black);
// Check if the animation duration has passed
if (timer.getElapsedTime().asSeconds() >= animationDuration)
{
timer.restart();
isRotating = !isRotating;
}
// Update the rotation angle
if (isRotating)
{
float rotationAmount = rotationSpeed * timer.getElapsedTime().asSeconds();
rotationAngle = fmod(rotationAmount, 360.0f);
}
// Rotate the rectangle
rectangle.setRotation(rotationAngle);
// Draw the rectangle
window.draw(rectangle);
// Update the window
window.display();
sf::sleep(sf::milliseconds(40)); // 60 FPS (1000 ms / 60)
}
return 0;
}