Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <SFML/Graphics.hpp>
- float toRadian(const float °ree) {
- return degree / 57.2957795;
- }
- float toDegree(const float &radian) {
- return radian * 57.2957795;
- }
- float getVectorLength(const sf::Vector2f &vector) {
- return sqrt(vector.x*vector.x + vector.y*vector.y);
- }
- sf::Vector2f normalizeVector(const sf::Vector2f &vector, const float &scale = 1.f) {
- return (vector / getVectorLength(vector) ) * scale;
- }
- sf::Vector2f getDeltaPosition(const sf::Vector2f &startPosition, const sf::Vector2f &endPosition) {
- return sf::Vector2f( endPosition - startPosition );
- }
- int main()
- {
- sf::RenderWindow window({ 1280, 720 }, "Screenshot!");
- window.setFramerateLimit(50);
- sf::Vector2f position(5, 650);
- sf::Vector2f velocity;
- const float GRAVITY = 0.05;
- bool releaseBall = false;
- bool isBallFlying = false;
- sf::RectangleShape rect({ 1280, 5 });
- rect.setFillColor(sf::Color(200, 100, 100, 100));
- rect.setPosition(0, 650);
- sf::CircleShape ball(5);
- ball.setOrigin(5, 5);
- ball.setPosition(position);
- sf::CircleShape predictBall(5);
- predictBall.setOrigin(5, 5);
- predictBall.setPosition(position);
- float minDistance = 10000;
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && releaseBall == false && isBallFlying == false)
- releaseBall = true;
- }
- sf::Vector2f deltaMouse = normalizeVector(getDeltaPosition({ 5,650 }, (sf::Vector2f)sf::Mouse::getPosition(window)), 7);
- float vectorLength = getVectorLength(deltaMouse);
- if (isBallFlying == false && releaseBall == false) {
- float x =
- vectorLength * vectorLength * sin(
- 2 * std::asin( std::abs(deltaMouse.y)/vectorLength )
- )
- / GRAVITY;
- predictBall.setPosition(sf::Vector2f(x, 650));
- //std::cout << x << std::endl;
- }
- if (releaseBall) {
- velocity = deltaMouse;
- //std::cout << velocity.x << " " << velocity.y << std::endl;
- isBallFlying = true;
- releaseBall = false;
- }
- if (isBallFlying) {
- //std::cout << "HALO" << std::endl;
- position += velocity;
- velocity.y += GRAVITY;
- float tempDist = getVectorLength( getDeltaPosition(predictBall.getPosition(), ball.getPosition()));
- if (tempDist < minDistance)
- minDistance = tempDist;
- if (position.y > 700) {
- position = {5,650};
- isBallFlying = false;
- std::cout << "Distance: " << minDistance << std::endl;
- }
- ball.setPosition(position);
- }
- window.clear(sf::Color(37,37,48));
- window.draw(rect);
- window.draw(ball);
- window.draw(predictBall);
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement