Created
December 23, 2013 15:42
-
-
Save fallahn/8099202 to your computer and use it in GitHub Desktop.
rotational movement in SFML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SFML/Window.hpp> | |
#include <SFML/Graphics.hpp> | |
int main() | |
{ | |
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!"); | |
sf::RectangleShape shape(sf::Vector2f(70.f, 10.f)); | |
shape.setFillColor(sf::Color::Green); | |
shape.setOrigin(35.f, 5.f); | |
shape.setPosition(400.f, 300.f); | |
sf::Clock clock; | |
while (window.isOpen()) | |
{ | |
sf::Event event; | |
while (window.pollEvent(event)) | |
{ | |
if (event.type == sf::Event::Closed) | |
window.close(); | |
} | |
//------------------------------------------ | |
const float dt = clock.restart().asSeconds(); | |
const float rotation = 200.f; | |
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) | |
{ | |
shape.rotate(-rotation * dt); | |
} | |
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) | |
{ | |
shape.rotate(rotation * dt); | |
} | |
sf::Vector2f unitVec(-1.f, 0.f); | |
sf::Transform t; | |
t.rotate(shape.getRotation()); | |
sf::Vector2f movement = t.transformPoint(unitVec); | |
const float velocity = 300.f; | |
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) | |
{ | |
shape.move(movement * velocity * dt); | |
} | |
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) | |
{ | |
shape.move(-movement * velocity * dt); | |
} | |
//------------------------------------------- | |
window.clear(); | |
window.draw(shape); | |
window.display(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment