1
General / 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 );
}
}