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

Author Topic: Rotate sprites relative to their position in a "container"  (Read 1384 times)

0 Members and 1 Guest are viewing this topic.

legacyblade

  • Newbie
  • *
  • Posts: 25
    • View Profile
Rotate sprites relative to their position in a "container"
« on: November 11, 2013, 09:12:07 pm »
Hello. I'm working on building a game engine. One problem I'm having deals with a class that contains multiple sprites. I need to be able to position individual sprites within this container class. When moving, rotating, or scaling the container class, every sprite contained needs to keep their relative position and rotation.

I retooled some old code of mine, but I'm getting unexpected results when handling rotation. If the child sprite is not in the exact center of the container, it spins around wildly and ends up in a semi-random location.

The code for the method setting all the sprites position and rotation relative to the container's current angle is as follows:

void sprite::render_children_angle()
        {
                for(unsigned int i = 0; i < drawables.size(); i++) {
                        drawable*& child = graphicSystem.get_drawable(drawables[i].first);

                        std::pair<float,float> childPos = child->get_pos();

                        float radius   = std::sqrt( (xPos - childPos.first)*(xPos - childPos.first) + (yPos - childPos.second)*(yPos - childPos.second));
                        float angle = atan2(yPos - childPos.second, xPos - childPos.first);

                        float newAngle = angle - (cAngle * 3.14/180);

                        float newX = radius * std::cos(newAngle);
                        float newY = radius * std::sin(newAngle);

                        child->set_pos(xPos - newX, yPos - newY);
                        child->set_angle(drawables[i].second->angleOffset + cAngle);
                }
        }
 

cAngle is the container's angle. The "drawables[ i ].second->angleOffset" is the individual sprite's rotation. Both are set outside this method, and I've verified that they're the proper values. I've no idea what's causing the problem. I've spent quite awhile tweaking the code, but nothing seems to produce any results.

Any help you could give me with this would be great. Geometry is a subject I've never been very good at, so I'm not sure how to go about even figuring out what went wrong or why.
« Last Edit: November 11, 2013, 09:31:40 pm by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Rotate sprites relative to their position in a "container"
« Reply #1 on: November 12, 2013, 10:17:51 am »
This is most likely not a programming problem but one in the vector math universe. ;)

Playing around with the code often makes you forget to think about what the issue is and one ends up in an endless trial and error, thus I can only recommend to take out pen, paper and a calculator (and maybe a math book) and think again what you have and what you want to achieve.
To simplify your life I'd also recommend to use Thor's vector math functions, makes quite a few things much easier and the code will often look a bit nicer.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/