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

Author Topic: SFML 2.0 - Move in direction faced  (Read 5006 times)

0 Members and 1 Guest are viewing this topic.

AltF4ToWin

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
SFML 2.0 - Move in direction faced
« on: May 12, 2013, 04:42:47 pm »
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))  
              Ship_s.rotate(7.f);
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
              Ship_s.rotate(-7.f);
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))    
             Ship_s.move(cos(Ship_s.getRotation()*3.14159265/180)*3.f,sin(Ship_s.getRotation()*3.14159265/180)*-3.f);
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))  
             Ship_s.move(cos(Ship_s.getRotation()*3.14159265/180)*-3.f,sin(Ship_s.getRotation()*3.14159265/180)*3.f);

 

The above code works, however when I try to move the ship around the screen, it only moves in the correct direction in one of the compass directions.

What do?

EDIT: https://www.youtube.com/watch?v=0SRkzad7sVw&feature=youtu.be Here's a video of what happens.
« Last Edit: May 12, 2013, 07:26:33 pm by AltF4ToWin »

gostron

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: SFML 2.0 - Move in direction faced
« Reply #1 on: May 12, 2013, 06:38:03 pm »
what does "compass direction" means ? I've tried your code and it works just fine, however you can only turn by multiples of 7 degrees.

Perhaps what you want to do is depend on the frame.

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            Ship_s.rotate(-30*frameTime);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            Ship_s.rotate(30*frameTime);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            Ship_s.move(500*sin(Ship_s.getRotation()*3.14159265/180)*frameTime, -500*cos(Ship_s.getRotation()*3.14159265/180)*frameTime);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            Ship_s.move(-500*sin(Ship_s.getRotation()*3.14159265/180)*frameTime, 500*cos(Ship_s.getRotation()*3.14159265/180)*frameTime);
 

where i get frameTime by mesuring how long did the last image to get printed.

Note that I make these calls outside of the pollEvent function.

AltF4ToWin

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: SFML 2.0 - Move in direction faced
« Reply #2 on: May 12, 2013, 06:39:51 pm »
Compass directions - North, South, East & West.

I originally had it by frametime, but it didn't change much.

gostron

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: SFML 2.0 - Move in direction faced
« Reply #3 on: May 12, 2013, 06:43:38 pm »
I don't know in which direction is your original sprite is, but my code is for an image that is facing up.

Works perfectly fine for me (your code did too however, but out of the pollEvent loop

AltF4ToWin

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: SFML 2.0 - Move in direction faced
« Reply #4 on: May 12, 2013, 07:36:42 pm »
It's facing to the right as that seems to make it looks most normal.

I figure it's something in the maths, but I'm not sure what.

gostron

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: SFML 2.0 - Move in direction faced
« Reply #5 on: May 12, 2013, 07:50:28 pm »
I can't see why it wouldn't work. Looks like math.h is included (or else you'd have an error) plus it works for me.
Be careful with the minus ("-") in the middle of an equation. I've had issue with it when accessing variables (it inverted the address value and not the value contained by the variable.

Sorry I couldn't of more help.