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

Author Topic: Sprite rotating problem  (Read 1684 times)

0 Members and 1 Guest are viewing this topic.

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Sprite rotating problem
« on: April 25, 2011, 02:12:17 pm »
I have very simple vector program but it doesnt work. It moves to different direction as the angle of sprite. Please how can I fix this problem?
My code
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <cmath>
#include <sstream>
#include <string>

static inline std::string int2Str(float x)
{
      std::stringstream type;
      type << x;
      return type.str();
}

#define PI 3.14159265

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600), "ShootingGun");
   
    sf::Image imgGun;
    imgGun.LoadFromFile("gun.png");
    imgGun.SetSmooth(false);
    sf::Sprite Gun(imgGun);
   
    float x = 400, y = 300, angle = 0, lenght = 5;
   
    App.SetFramerateLimit(200);
   
    while(App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
                {
                        if (Event.Type == sf::Event::Closed)
                        {
                            App.Close();
                        }
               
                        if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Up)
                        {
                            x = x - lenght * cos(angle*PI/180);
                            y = y - lenght * sin(angle*PI/180);
                           
                        }
                       
                        if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Down)
                        {
                            x = x + lenght * cos(angle*PI/180);
                            y = y + lenght * sin(angle*PI/180);
                        }
                       
                        if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Right)
                        {
                            angle += 1;
 
                            Gun.Rotate(1);
                        }
                       
                        if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Left)
                        {
                            angle -= 1;
                           
                            Gun.Rotate(-1);
                        }
                }
       
        Gun.SetPosition(x,y);
       
        App.Clear();
        sf::String text("x: " + int2Str(x) + "y: " + int2Str(y) + "angle: " + int2Str(angle)
        + "sprite angle: " + int2Str(Gun.GetRotation()), sf::Font::GetDefaultFont());
        text.SetPosition(0.f,0.f);
        App.Draw(text);
        App.Draw(Gun);
        App.Display();
    }
   
    return 0;
}


my picture, it is only simple straight line

Dimple

  • Newbie
  • *
  • Posts: 30
    • View Profile
Sprite rotating problem
« Reply #1 on: April 25, 2011, 03:40:15 pm »
Is this what you want?
Code: [Select]

#include <SFML/Graphics.hpp>
 #include <SFML/Window.hpp>
 #include <SFML/System.hpp>
 #include <cmath>
 #include <sstream>
 #include <string>

 static inline std::string int2Str(float x)
 {
       std::stringstream type;
       type << x;
       return type.str();
 }

 #define PI 3.14159265

 int main()
 {
     sf::RenderWindow App(sf::VideoMode(800, 600), "ShootingGun");

     sf::Image imgGun;
     imgGun.LoadFromFile("bar.png");
     imgGun.SetSmooth(false);
     sf::Sprite Gun(imgGun);

     float x = 400, y = 300, angle = 0, lenght = 5;

     App.SetFramerateLimit(200);

     while(App.IsOpened())
     {
         sf::Event Event;
         while (App.GetEvent(Event))
                 {
                         if (Event.Type == sf::Event::Closed)
                         {
                             App.Close();
                         }

                         if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Up)
                         {
                             x = x + lenght * cos(angle*PI/180);
                             y = y - lenght * sin(angle*PI/180);

                         }

                         if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Down)
                         {
                             x = x - lenght * cos(angle*PI/180);
                             y = y + lenght * sin(angle*PI/180);
                         }

                         if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Right)
                         {
                             angle += 1;

                             Gun.Rotate(1);
                         }
                         
                         if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Left)
                         {
                             angle -= 1;

                             Gun.Rotate(-1);
                         }
                 }

         Gun.SetPosition(x,y);

         App.Clear();
         
         sf::String text("x: " + int2Str(x) + "y: " + int2Str(y) + "angle: " + int2Str(angle)
         + "sprite angle: " + int2Str(Gun.GetRotation()), sf::Font::GetDefaultFont());
         text.SetPosition(0.f,0.f);
         App.Draw(text);
         App.Draw(Gun);
         App.Display();
     }

     return 0;
 }


In math you would do this:
Code: [Select]

x = x + cos(angle)*speed
y = y + sin(angle)*speed

But because in SFML (like in most libraries/languages) the y-coordinate goes down instead of going up like in math, you have to negate the y-coordinate change. So what you actually want to do when the user presses up is this:
Code: [Select]

x = x + cos(angle)*speed
y = y - sin(angle)*speed

And when going backwards, you negate them both
Code: [Select]

x = x - cos(angle)*speed
y = y + sin(angle)*speed

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Sprite rotating problem
« Reply #2 on: April 25, 2011, 04:55:30 pm »
Thanks, that is exactly what I wanted  :D