Skip to content

Commit 490abb7

Browse files
FRICTION
1 parent 42d4c98 commit 490abb7

File tree

7 files changed

+330
-32
lines changed

7 files changed

+330
-32
lines changed

CMakeLists.txt

+18-28
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,28 @@ message(STATUS "Configuring project ${PROJECT_NAME}:c++${CMAKE_CXX_STANDARD}")
66

77
if (RandomWalker)
88
add_subdirectory(randomwalker)
9-
add_executable(${PROJECT_NAME}
10-
${PROD_SOURCES}
11-
utils/utilities.hpp
12-
)
13-
target_include_directories(${PROJECT_NAME} PUBLIC
14-
${PROD_NAME}
15-
utils
16-
)
17-
endif()
18-
19-
if (BarGraph)
9+
elseif (BarGraph)
2010
add_subdirectory(bargraph)
21-
add_executable(${PROJECT_NAME}
22-
${PROD_SOURCES}
23-
utils/utilities.hpp
24-
)
25-
target_include_directories(${PROJECT_NAME} PUBLIC
26-
${PROD_NAME}
27-
utils
28-
)
29-
endif()
30-
31-
if (Velocity)
11+
elseif (Velocity)
3212
add_subdirectory(velocity)
33-
add_executable(${PROJECT_NAME}
34-
${PROD_SOURCES}
35-
)
36-
target_include_directories(${PROJECT_NAME} PUBLIC
37-
${PROD_NAME}
38-
)
13+
elseif (ObjectOnMouseClick)
14+
add_subdirectory(objectonclick)
15+
elseif (Friction)
16+
add_subdirectory(friction)
17+
else()
18+
set(PROD_SOURCES main.cpp)
3919
endif()
4020

21+
add_executable(${PROJECT_NAME}
22+
${PROD_SOURCES}
23+
utils/utilities.hpp
24+
)
25+
26+
target_include_directories(${PROJECT_NAME} PUBLIC
27+
${PROD_NAME}
28+
utils
29+
)
30+
4131
target_link_libraries(${PROJECT_NAME}
4232
sfml-graphics
4333
sfml-window

friction/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
set(PROD_NAME "friction" PARENT_SCOPE)
4+
file(GLOB_RECURSE SRCS "*.cpp")
5+
set(PROD_SOURCES ${SRCS} PARENT_SCOPE)

friction/friction.cpp

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <utility>
4+
#include <SFML/Graphics.hpp>
5+
6+
#define SCREEN_WIDTH 1920
7+
#define SCREEN_HEIGHT 1080
8+
#define UNIT_FORCE 1
9+
#define FRICTION_COEFF 0.95
10+
11+
sf::Vector2f operator*(const sf::Vector2f& vec, float val)
12+
{
13+
return sf::Vector2f{vec.x * val, vec.y * val};
14+
}
15+
16+
sf::Vector2f calculateFriction(const sf::Vector2f& velocity)
17+
{
18+
return velocity * -1 * UNIT_FORCE * FRICTION_COEFF;
19+
}
20+
21+
bool isInLeftEdge(float leftBound)
22+
{
23+
return leftBound <= 0;
24+
}
25+
26+
bool isInRightEdge(float rightBound)
27+
{
28+
return rightBound >= SCREEN_WIDTH;
29+
}
30+
31+
float zoneOffset{50};
32+
std::vector<std::pair<float, float>> zones = {
33+
{SCREEN_WIDTH/4 - zoneOffset, SCREEN_WIDTH/4 + zoneOffset},
34+
{SCREEN_WIDTH * 3/4 - zoneOffset, SCREEN_WIDTH * 3/4 + zoneOffset}};
35+
36+
bool isInZone(int identifier)
37+
{
38+
for (const auto& zone : zones)
39+
{
40+
if (identifier >= zone.first && identifier <= zone.second)
41+
{
42+
//std::cout << "In the zone!" << std::endl;
43+
return true;
44+
}
45+
}
46+
//std::cout << "NOT In the zone!" << std::endl;
47+
return false;
48+
}
49+
50+
int main()
51+
{
52+
std::cout << "Friction!" << std::endl;
53+
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Friction!");
54+
55+
// Setup Floor
56+
float floorHeight{20};
57+
sf::RectangleShape floor(sf::Vector2f{SCREEN_WIDTH, floorHeight});
58+
floor.setFillColor(sf::Color::Cyan);
59+
floor.setPosition(sf::Vector2f{0, SCREEN_HEIGHT - floorHeight});
60+
61+
// Setup Rough Floor
62+
sf::RectangleShape roughFloor1(sf::Vector2f{zoneOffset * 2, floorHeight});
63+
sf::RectangleShape roughFloor2(sf::Vector2f{zoneOffset * 2, floorHeight});
64+
roughFloor1.setFillColor(sf::Color::Blue);
65+
roughFloor2.setFillColor(sf::Color::Blue);
66+
roughFloor1.setPosition(sf::Vector2f{SCREEN_WIDTH/4 - zoneOffset, SCREEN_HEIGHT - floorHeight});
67+
roughFloor2.setPosition(sf::Vector2f{SCREEN_WIDTH * 3/4 - zoneOffset, SCREEN_HEIGHT - floorHeight});
68+
69+
// Setup the BALL
70+
const float ballRadius{10.f};
71+
sf::CircleShape ball{ballRadius};
72+
ball.setFillColor(sf::Color::Red);
73+
ball.setPosition(sf::Vector2f{
74+
SCREEN_WIDTH/2 - ballRadius,
75+
SCREEN_HEIGHT - (2 * ballRadius) - floorHeight});
76+
77+
//const float force{0.0125f};
78+
const float force{0.0125f};
79+
sf::Vector2f velocity{0, 0};
80+
auto appliedVelocity = velocity;
81+
while (window.isOpen())
82+
{
83+
sf::Event ev;
84+
while (window.pollEvent(ev))
85+
{
86+
if (ev.type == sf::Event::Closed ||
87+
(ev.type == sf::Event::KeyPressed &&
88+
(ev.key.code == sf::Keyboard::Key::Escape)))
89+
window.close();
90+
if (ev.type == sf::Event::KeyPressed)
91+
{
92+
if (ev.key.code == sf::Keyboard::Key::Left)
93+
velocity = sf::Vector2f{-force, 0};
94+
if (ev.key.code == sf::Keyboard::Key::Right)
95+
velocity = sf::Vector2f{force, 0};
96+
}
97+
}
98+
99+
if ((isInLeftEdge(ball.getPosition().x) && velocity.x <= 0) ||
100+
(isInRightEdge(ball.getPosition().x + (ball.getRadius() * 2)) && velocity.x >= 0))
101+
{
102+
velocity = sf::Vector2f{0, 0};
103+
}
104+
105+
if (isInZone(ball.getPosition().x + ball.getRadius()))
106+
{
107+
const auto& friction = calculateFriction(velocity);
108+
appliedVelocity = velocity + friction;
109+
//std::cout << "Friction: " << friction.x << "," << friction.y << std::endl;
110+
//std::cout << "Velocity: " << velocity.x << "," << velocity.y << std::endl;
111+
std::cout << "AppliedVelocity: " << appliedVelocity.x << "," << appliedVelocity.y << std::endl;
112+
}
113+
else
114+
{
115+
appliedVelocity = velocity;
116+
std::cout << "AppliedVelocity: " << appliedVelocity.x << "," << appliedVelocity.y << std::endl;
117+
}
118+
//std::cout << "Position: " << ball.getPosition().x << "," << ball.getPosition().y << std::endl;
119+
120+
ball.setPosition(ball.getPosition() + appliedVelocity);
121+
122+
123+
window.clear();
124+
window.draw(floor);
125+
window.draw(roughFloor1);
126+
window.draw(roughFloor2);
127+
window.draw(ball);
128+
window.display();
129+
}
130+
131+
return EXIT_SUCCESS;
132+
}
133+

main.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#include <iostream>
2-
#include "IController.hpp"
2+
3+
int main()
4+
{
5+
std::cout << "Nature Of Code!" << std::endl;
6+
}
37

objectonclick/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
cmake_minimum_required(VERSION 3.16)
3+
4+
set(PROD_NAME "objectonmouseclick" PARENT_SCOPE)
5+
file(GLOB_RECURSE SRCS "*.cpp")
6+
set(PROD_SOURCES ${SRCS} PARENT_SCOPE)

objectonclick/objectonclick.cpp

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
#include <iostream>
3+
#include "SFML/Graphics.hpp"
4+
5+
#define SCREEN_WIDTH 1920
6+
#define SCREEN_HEIGHT 1080
7+
8+
template<typename T>
9+
T createObject(float size, float xPos, float yPos)
10+
{
11+
T shape{sf::Vector2f{size, size}};
12+
shape.setPosition({xPos-(size/2), yPos-(size/2)});
13+
return shape;
14+
}
15+
16+
template<>
17+
sf::CircleShape createObject<sf::CircleShape>(float size, float xPos, float yPos)
18+
{
19+
float radius{size/2};
20+
sf::CircleShape shape{radius};
21+
shape.setPosition({xPos-radius, yPos-radius});
22+
return shape;
23+
}
24+
25+
template<typename T>
26+
void drawObjects(sf::RenderWindow& window, const std::vector<T>& objects)
27+
{
28+
for (const auto& obj : objects)
29+
{
30+
window.draw(obj);
31+
}
32+
}
33+
34+
int main()
35+
{
36+
std::cout << "ObjectOnClick!" << std::endl;
37+
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "ObjectOnClick!");
38+
39+
std::vector<sf::CircleShape> objects;
40+
bool isMouseLeftButtonPressed{false};
41+
float size{10};
42+
sf::CircleShape mouseObj{size/2};
43+
mouseObj.setFillColor(sf::Color::White);
44+
while (window.isOpen())
45+
{
46+
sf::Event ev;
47+
while (window.pollEvent(ev))
48+
{
49+
if (ev.type == sf::Event::Closed ||
50+
(ev.type == sf::Event::KeyPressed &&
51+
(ev.key.code == sf::Keyboard::Key::Escape)))
52+
window.close();
53+
if (ev.type == sf::Event::MouseButtonReleased && ev.mouseButton.button == sf::Mouse::Button::Left)
54+
isMouseLeftButtonPressed = false;
55+
if (ev.type == sf::Event::MouseButtonPressed)
56+
{
57+
if (ev.mouseButton.button == sf::Mouse::Button::Right)
58+
{
59+
objects.clear();
60+
}
61+
else if (ev.mouseButton.button == sf::Mouse::Button::Left)
62+
{
63+
isMouseLeftButtonPressed = true;
64+
}
65+
}
66+
if (ev.type == sf::Event::MouseWheelScrolled)
67+
{
68+
std::cout << "MouseWheelScroll Delta :3 " << ev.mouseWheelScroll.delta << std::endl;
69+
size += 1.f * ev.mouseWheelScroll.delta;
70+
}
71+
}
72+
73+
window.clear();
74+
const auto& mousePos{sf::Mouse::getPosition(window)};
75+
if (isMouseLeftButtonPressed)
76+
{
77+
objects.emplace_back(createObject<sf::CircleShape>(size, (float)mousePos.x, (float)mousePos.y));
78+
//std::cout << "Objects created: " << objects.size() << std::endl;
79+
}
80+
// Draw Mouse Object
81+
mouseObj.setPosition(sf::Vector2f{mousePos.x - (size/2), mousePos.y - (size/2)});
82+
mouseObj.setRadius(size/2);
83+
window.draw(mouseObj);
84+
85+
drawObjects(window, objects);
86+
window.display();
87+
}
88+
89+
return EXIT_SUCCESS;
90+
}

0 commit comments

Comments
 (0)