-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflection algorithm.cpp
More file actions
74 lines (61 loc) · 1.94 KB
/
Reflection algorithm.cpp
File metadata and controls
74 lines (61 loc) · 1.94 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
67
68
69
70
71
72
73
#include <SFML/Graphics.hpp>
int main()
{
// Window size
const int WIDTH = 800;
const int HEIGHT = 600;
// Create the window
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Reflection 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);
// Reflection properties
bool isReflected = false;
float reflectionPosition = 0.0f;
float reflectionSpeed = 1.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);
// Update the reflection position
if (!isReflected)
{
reflectionPosition += reflectionSpeed;
if (reflectionPosition >= 1.0f)
{
reflectionPosition = 1.0f;
isReflected = true;
}
}
else
{
reflectionPosition -= reflectionSpeed;
if (reflectionPosition <= 0.0f)
{
reflectionPosition = 0.0f;
isReflected = false;
}
}
// Create a reflected rectangle shape
sf::RectangleShape reflectedRect(rectangle);
reflectedRect.setPosition(rectangle.getPosition().x, rectangle.getPosition().y + rectangle.getSize().y * reflectionPosition);
reflectedRect.setScale(1.0f, -1.0f);
// Draw the original and reflected rectangle
//window.draw(rectangle);
window.draw(reflectedRect);
// Update the window
window.display();
sf::sleep(sf::milliseconds(100)); // 60 FPS (1000 ms / 60)
}
return 0;
}