Good day,
let's cut to the chase, shall we?
https://www.dropbox.com/s/f4w3ydjm2f8tu4m/Release.rarThis is a small test program in which a green square turns without delay towards the mouse pointer. The red square underneath does the same, but at a lower speed. When the user moves his mouse over the white line, the red square turns the opposite direction of which it is supposed to turn. This means that when the mouse pointer moves over the white line by going up, the red square turns clockwise while it should be turning counterclockwise.
This is the full code of the program:
#include <SFML\Graphics.hpp>
#include <cmath>
#include <thread>
#include <chrono>
// define PI
#define PI 3.14159265
// main
int main(){
sf::Vector2i mouse;
sf::Vector2f player;
sf::RectangleShape shape[3];
shape[0].setPosition(400, 300);
shape[0].setSize(sf::Vector2f(200, 200));
shape[0].setOrigin(100, 100);
shape[0].setFillColor(sf::Color::Red);
shape[1].setPosition(400, 300);
shape[1].setSize(sf::Vector2f(100, 100));
shape[1].setOrigin(50, 50);
shape[1].setFillColor(sf::Color::Green);
shape[2].setPosition(400, 300);
shape[2].setSize(sf::Vector2f(400, 1));
shape[2].setFillColor(sf::Color::White);
player = shape[0].getPosition();
sf::RenderWindow window(sf::VideoMode(800, 600), "test", sf::Style::Close);
sf::Event event;
while (window.isOpen()){
// check events
while (window.pollEvent(event)){
switch (event.type){
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Escape)
window.close();
break;
case sf::Event::Closed:
window.close();
break;
case sf::Event::MouseMoved:
mouse = sf::Mouse::getPosition(window);
break;
default:
break;
}
}
// logic
std::this_thread::sleep_for(std::chrono::milliseconds(10));
shape[1].setRotation(atan2(mouse.y - player.y, mouse.x - player.x) * 180 / PI);
if (shape[0].getRotation() < shape[1].getRotation() - 1)
shape[0].rotate(1);
else if (shape[0].getRotation() > shape[1].getRotation() + 1)
shape[0].rotate(-1);
else
shape[0].setRotation(shape[1].getRotation());
// draw
window.clear(sf::Color::Black);
window.draw(shape[2]);
window.draw(shape[0]);
window.draw(shape[1]);
window.display();
}
}
I hope you understand the issue.