I am fighting with a really simple problem that has been driving me nuts. I'm usually fairly confident with trigonometry and I'm pretty sure the issue here is not the math but in some behavior behind the scenes.
A bit of background - basically I'm developing my own little 2D game engine using C++, in my free-time for fun. I work with game technology and simulation products IRL so for the most part this has been plain sailing. I am using Box2D for physics, and SFML for rendering.
My game at current has a few space ships flying around via keyboard and AI input. These space ships need exhausts! So I'm currently in the process of getting a 'component' system in place, however my problem is that the animated exhaust sprites are not going where I want them to.
When the base GameObject class runs it's update method, it does the following:
Queries the physics object for a position
Converts the physics position to a pixel position (x10 scale factor)
Moves the sprite to the new position
Passes new position to all 'component' objects into their 'UpdateComponent' method
Components move their sprite to the correct position
However the exhaust sprites seem to wander around all over the place, in no way representing where I actually want them to be! This should have been a simple case of:
newPosition = (sin(rotationRadians) * offsetX, cos(rotationRadians) * offsetY) + oldPosition
Here is the current code:
void Exhaust::UpdateComponent(const Vec2f & position, float rotationDegrees, float deltaTime)
float angleRad = DEGREES_TO_RADIANS(rotationDegrees);
float offsetX = sin(angleRad) * mOffset.x;
float offsetY = cos(angleRad) * mOffset.y;
sf::Vector2f newPos(position.x + offsetX, position.y + offsetY);
sf::Vector2f p(position.x, position.y);
ss << rotationDegrees;
DebugUtils::DrawCross(p, 8, sf::Color::Green, ss.str());
DebugUtils::DrawCross(newPos, 8, sf::Color::Green);
DebugUtils::DrawLine(p, newPos, sf::Color::Green);
cout << rotationDegrees << endl;
#endif // _DEBUG
And here is a gif of it all going wrong! (link below)
Gif of game engine problem
Any help would be much appreciated, even if it is just a thought of where the problem might be originating from!