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

Author Topic: Rotating a Sprite  (Read 1445 times)

0 Members and 1 Guest are viewing this topic.

Carlows

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Rotating a Sprite
« on: June 25, 2013, 11:59:51 pm »
I'm having problems with this, I have a Ship class, that inherits the Sprite class... I change It's origin to the center, but if I try to rotate the Sprite, It's seems like it rotates the sprite from the top-left corner, there's my code:

Quote
Ship::Ship()
    : sf::Sprite()
{
   texture.loadFromFile("10B.png");
   this->setTexture(texture);
 
   this->setOrigin( this->getGlobalBounds().width / 2, this->getGlobalBounds().height / 2 );
   this->scale(sf::Vector2f(0.25f, 0.25f));   
   this->setPosition(100.0f, 100.0f);
 
   // Definimos la velocidad
   this->speed.x = 300.0f;
   this->speed.y = 300.0f;

}
 
void Ship::update(sf::Time& delta)
{   
   float left = this->getPosition().x;
   float right = this->getPosition().x + this->getGlobalBounds().width;
   float top = this->getPosition().y;
   float bottom = this->getPosition().y + this->getGlobalBounds().height;

   if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && top > 0)
      //this->setRotation(this->getRotation() + 5);
      this->move(0.0f, -delta.asSeconds() * speed.y);
   if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
      this->move(0.0f, delta.asSeconds() * speed.y);
   if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && left > 0)
      this->move(-delta.asSeconds() * speed.x, 0.0f);
   if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
      this->move(delta.asSeconds() * speed.x, 0.0f);

   if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
      this->rotate(5);

   if( top < 0 )
   {
      this->setPosition( this->getPosition().x, 0 );
   }
   if( left < 0 )
   {
      this->setPosition( 0, this->getPosition().y );
   }
    
}

BlockyNewt

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Rotating a Sprite
« Reply #1 on: July 15, 2020, 02:08:44 am »
Try this in the constructor.

Ship::Ship()
    : sf::Sprite()
{
   texture.loadFromFile("10B.png");
   this->setTexture(texture);
 
   
   this->scale(sf::Vector2f(0.25f, 0.25f));  
   this->setPosition(100.0f, 100.0f);
   this->setOrigin( this->getLocalBounds().width / 2, this->getLocalBounds().height / 2 );
 
   // Definimos la velocidad
   this->speed.x = 300.0f;
   this->speed.y = 300.0f;

}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rotating a Sprite
« Reply #2 on: July 20, 2020, 11:02:04 pm »
To clarify, the origin should be set to local co-ordinates, not global ones. :D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything