Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Simultaneous input  (Read 1484 times)

0 Members and 1 Guest are viewing this topic.

idontmakesense

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Simultaneous input
« on: July 19, 2013, 02:37:26 pm »
How do I go about moving and turning the rectangle at the same time. He is my code..

#include<SFML/Graphics.hpp>
#include<iostream>
#include<cmath>

#define PI 3.14159265

using namespace std;

void displace(sf::RectangleShape *shape, float angle)
{

}

int main()
{
        const int xRes= 800;
        const int yRes= 600;
       
        sf::RenderWindow window(sf::VideoMode(xRes, yRes), "Window");
       
        float rectLength= 20, rectBreadth= 10;
        sf::RectangleShape rectangle(sf::Vector2f(rectLength, rectBreadth));
       
        float xCod= 100, yCod= 100;
        rectangle.setPosition(xCod, yCod);
        rectangle.setFillColor(sf::Color::Black);
        rectangle.setOrigin(rectLength/2, rectBreadth/2);
       
        float direction, speed= 0.5;
       
        while(window.isOpen())
        {
                sf::Event event;
                while(window.pollEvent(event))
                {
                        if(event.type== sf::Event::Closed || (event.type== sf::Event::KeyReleased) && (event.key.code== sf::Keyboard::Escape))
                                window.close();
                }
               
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        direction= rectangle.getRotation();
                        rectangle.move(speed*cos(direction*PI/180), speed*sin(direction*PI/180));
                }
               
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        direction= rectangle.getRotation();
                        rectangle.move(-speed*cos(direction*PI/180), -speed*sin(direction*PI/180));
                }
               
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        rectangle.rotate(-0.5);
                        direction= rectangle.getRotation();
                }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        rectangle.rotate(0.5);
                        direction= rectangle.getRotation();
                }
               
                window.clear(sf::Color::White);
                window.draw(rectangle);
                window.display();
        }
        return 0;
}
« Last Edit: July 19, 2013, 03:01:58 pm by Laurent »

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Simultaneous input
« Reply #1 on: July 19, 2013, 02:50:57 pm »
Just leave out the else 's before the if statements and then you can move and rotate at the same time

idontmakesense

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Simultaneous input
« Reply #2 on: July 19, 2013, 03:16:38 pm »
It took me while to realize the difference. xD Really dumb of me.

Thanks a lot.