0

I've recently run into a mild problem when trying to program a class that holds multiple sprites with SFML.

I have a container with multiple sprites, each of which can have an offset of scale, rotation, and position from the center point of the container. However, I run into an issue when trying to rotate the container. If I just rotate the individual sprites, it won't take into account their offset from the center of the container (for some of the user interface bits, this is a problem). After a bit of digging, I discovered how to get their angle from the center and calculate the position they should end up in when rotated. However, I don't know how to keep their reletive rotation the same. Basically, I want to be able to do this

enter image description here

And after rotating, it would look like this

enter image description here

Because of how SFML handles rotation and origin, I can't just set the origin of each sprite to the center of the container and rotate. I need to have some sort of function that calculates where they should go as well as the orientation they should have relative to what they already had (so if the sprite had an angle of 0, when the container is rotated by 90 degrees, it should now have an angle of 90 degrees. Though if it started with an angle of 90, it should be rotated 180 degrees afterwards, so as to preserve it's rotation relative to the rest of the sprites in the container)

The code I'm using to get the location it should be relative to the container when rotated is as follows

float angle = atan2(p1.y -p2.y, p1.x - p2.x); float x = radius * cos(angle); float y = radius * sin(angle); icon.set_position(x + spriteX, y + spriteY); 

Thanks for reading. I've never been all that great at math, so I don't know how to figure this one out on my own. I didn't even know what cos and sin where until I started researching this problem x.x Any help would be appreciated.

1
  • Look into composing transformations: en.wikipedia.org/wiki/… . SFML provides a sf::Transformable class and sf::Sprite inherits from that. Commented Feb 7, 2013 at 3:08

2 Answers 2

1

Do you know how to rotate the sprite without moving it's position? If so, just rotate the actual sprite itself by the same angle that you get when position on the circle above. Right now, your rotating the position by using sin and cos. You also need to set the "Orientation" by the same angle that you calculated with: float angle = atan2(p1.y - p2.y, p1.x - p2.x);

Good luck!

-pBlack

Sign up to request clarification or add additional context in comments.

1 Comment

I don't know why I didn't think of just rotating the sprite by the exact same angle. It seems to work great x.x Thanks. I feel really dumb sometimes, lol.
0

Simple, create a vector from the container's centre point to a sprite. Then rotate the vector and you have your new sprite position.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.