|
| 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 | + |
0 commit comments