Skip to content

Commit b44b0b5

Browse files
Working velocity :)
1 parent ad79d83 commit b44b0b5

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ if (BarGraph)
2828
)
2929
endif()
3030

31+
if (Velocity)
32+
add_subdirectory(velocity)
33+
add_executable(${PROJECT_NAME}
34+
${PROD_SOURCES}
35+
)
36+
target_include_directories(${PROJECT_NAME} PUBLIC
37+
${PROD_NAME}
38+
)
39+
endif()
40+
3141
target_link_libraries(${PROJECT_NAME}
3242
sfml-graphics
3343
sfml-window

randomwalker/randomwalker_controller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void RandomWalkerController::startWalking<Randomness::Random>()
2828
template<>
2929
void RandomWalkerController::startWalking<Randomness::Custom>()
3030
{
31-
m_walker.setPosition(m_walker.getPosition() + getCustomDirection(0.55, 0.5));
31+
m_walker.setPosition(m_walker.getPosition() + getCustomDirection(0.5, 0.5));
3232
}
3333

3434
sf::Vector2f RandomWalkerController::getRandomDirection() const

utils/utilities.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <random>
3+
#include <cmath>
34

45
namespace utils
56
{
@@ -30,5 +31,11 @@ namespace utils
3031
return 0;
3132
return res;
3233
}
34+
35+
template<typename T>
36+
T magnitude(const T x, const T y)
37+
{
38+
return std::sqrt((x * x) + (y * y));
39+
}
3340
}
3441

velocity/CMakeLists.txt

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

velocity/velocity.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
#include "SFML/Graphics.hpp"
3+
4+
#define SCREEN_WIDTH 300
5+
#define SCREEN_HEIGHT 300
6+
7+
int main()
8+
{
9+
std::cout << "Velocity!" << std::endl;
10+
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Velocity!");
11+
12+
13+
// Object
14+
sf::CircleShape obj{10.f};
15+
obj.setFillColor(sf::Color::White);
16+
obj.setPosition({SCREEN_WIDTH/2, SCREEN_HEIGHT/2});
17+
18+
const float displacement{0.0125f};
19+
// Velocity
20+
sf::Vector2f velocity{displacement, 0};
21+
22+
while (window.isOpen())
23+
{
24+
sf::Event ev;
25+
while (window.pollEvent(ev))
26+
{
27+
if (ev.type == sf::Event::Closed ||
28+
(ev.type == sf::Event::KeyPressed &&
29+
(ev.key.code == sf::Keyboard::Key::Escape)))
30+
window.close();
31+
if (ev.type == sf::Event::KeyPressed)
32+
{
33+
if (ev.key.code == sf::Keyboard::Key::Up)
34+
velocity = sf::Vector2f{0, -displacement};
35+
if (ev.key.code == sf::Keyboard::Key::Down)
36+
velocity = sf::Vector2f{0, displacement};
37+
if (ev.key.code == sf::Keyboard::Key::Left)
38+
velocity = sf::Vector2f{-displacement, 0};
39+
if (ev.key.code == sf::Keyboard::Key::Right)
40+
velocity = sf::Vector2f{displacement, 0};
41+
}
42+
}
43+
44+
// Apply velocity to object
45+
obj.setPosition(obj.getPosition() + velocity);
46+
47+
// Reset Object position if out of bounds
48+
const auto& objPos{obj.getPosition()};
49+
if (objPos.x <= 0)
50+
obj.setPosition({SCREEN_WIDTH, objPos.y});
51+
else if (objPos.x >= SCREEN_WIDTH)
52+
obj.setPosition({0, objPos.y});
53+
else if (objPos.y <= 0)
54+
obj.setPosition({objPos.x, SCREEN_HEIGHT});
55+
else if (objPos.y >= SCREEN_HEIGHT)
56+
obj.setPosition({objPos.x, 0});
57+
58+
const auto& mousePos{sf::Mouse::getPosition(window)};
59+
60+
sf::Vertex line[] = {
61+
sf::Vertex{sf::Vector2f{(float)mousePos.x, (float)mousePos.y}, sf::Color::Green},
62+
sf::Vertex{
63+
sf::Vector2f{
64+
obj.getPosition().x+obj.getRadius(),
65+
obj.getPosition().y+obj.getRadius()},
66+
sf::Color::Red}
67+
};
68+
69+
window.clear();
70+
window.draw(obj);
71+
window.draw(line, 2, sf::Lines);
72+
window.display();
73+
}
74+
75+
return EXIT_SUCCESS;
76+
}

0 commit comments

Comments
 (0)