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.