Okay, so my game uses a grid, pathfinding etc, i.e agents are stuck down to definite integer coordinates and can't just roam freely. To cut down on having to make a ton of artwork (and because the game is orthogonal/top down) I decided it would be easier to use pure top down sprites and simply rotate them, effectively making it so I only needed to make artwork for one walk cycle, idle, attack etc.
However sadly I can't get rotation to work because of changing the origin. Now I've seen much discussion stating that if you want to keep the origin in the top left I would need to use a transform object and apply rotation to that and use it as a renderstate, however I couldn't get that to work. The main reason being that the transform only has transform.rotate, whereas the formula i use to work out rotation outputs an angle which is only good for sprite.setRotation as it sets the absolute rotation as opposed to the relative one.
So what I decided to try and do was to try various means of changing the the sprite's origin, set the rotation and then set the origin back to the left top corner, however this still produces undesirable outcomes. Here's my latest attempt at that. I used the hardcoded number 16 for ease of use and my sprites are 32x32, so it's their centre point:
void Entity::handleRotation(Tile * target)
{
setOrigin(16, 16);
setPosition(getPosition().x + 16, getPosition().y + 16);
float angle = atan2(target->getPosition().y - getPosition().y, target->getPosition().x - getPosition().x);
angle = ((angle * 180) / 3.14159265) + 90;
{
angle = 360 - (-angle);
}
setRotation(angle);
setOrigin(0, 0);
setPosition(getPosition().x - 16, getPosition().y - 16);
}
Again, this hasn't done as expected and I've tried many variants to no effect
Any suggestions, explanations or examples would be extremely helpful. Thanks in advance
*note setOrigin, setPosition, getPosition, setRotation are usable because the entity class publicly inherits from sprite.