Hi there,
I can't seem to understand why my sprite is not rotating around it's center even after adjusting the origin.
In the code below, the first For loop chooses a particular texture, while the inner For loop draws the sprite multiple times at different positions but with random rotations. The drawn image gives incorrect positions.
Any suggestions would be appreciated.
for (const auto& resource : list) {
const auto& id = resource.mId;
const auto& cells = resource.mCells;
sf::Sprite sprite(mResources.get(id));
const float w { sprite.getGlobalBounds().width };
const float h { sprite.getGlobalBounds().height };
const float r { h / w };
const float drawWidth = w > 3 * mLiAlpha ?
2.0f * mLiAlpha : w;
const float drawHeight = h > 3 * mLi ?
r * drawWidth : h;
float widthScale { drawWidth / w };
float heightScale { drawWidth / h };
sprite.setScale(widthScale, heightScale);
for (auto& i : cells) {
sprite.setOrigin
(sprite.getGlobalBounds().width / 2.0f,
sprite.getGlobalBounds().height / 2.0f);
sprite.setRotation(rand(eng));
sprite.setOrigin(0,0);
sprite.setPosition(
mCellCentres[i].mXc -
sprite.getGlobalBounds().width / 2.0f,
mCellCentres[i].mYc -
sprite.getGlobalBounds().height / 2.0f);
texture->draw(sprite);
}
}
texture->display();
return std::unique_ptr<sf::RenderTexture>(texture);