hey, uh..there's a little problem here.. I'm trying to make experiment with movement based on angle..(only 0 works fine)
I wonder what's wrong..
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cmath>
#include <iostream>
class User
{
private :
sf::RectangleShape Rect;
const float speed;
const float rotation;
public :
User() :
speed(0.8),
rotation(0.2)
{
Rect.setSize(sf::Vector2f(38, 17));
Rect.setOrigin(Rect.getSize().x / 2, Rect.getSize().y / 2);
Rect.move(Rect.getSize().x / 2, Rect.getSize().y / 2);
}
sf::Shape &getShape()
{
return Rect;
}
void Action(const sf::RenderWindow &window)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
Rect.rotate(rotation);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
Rect.rotate(-(rotation));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
sf::Vector2f Direction;
Direction.x = speed * std::cos(Rect.getRotation());
Direction.y = speed * std::sin(Rect.getRotation());
Rect.move(Direction);
}
check(window);
}
void check(const sf::RenderWindow &window)
{
if (Rect.getPosition().x < 0) Rect.move(speed, 0);
else if (Rect.getPosition().x > window.getSize().x) Rect.move(-(speed), 0);
if (Rect.getPosition().y < 0) Rect.move(0, speed);
else if (Rect.getPosition().y > window.getSize().y) Rect.move(0, -(speed));
}
};
int main()
{
sf::RenderWindow window(sf::VideoMode(900, 600), "some window");
User user;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) window.close();
}
user.Action(window);
window.clear(sf::Color::Black);
window.draw(user.getShape());
window.display();
std::cout << user.getShape().getRotation() << std::endl;
}
}