I have an object that needs to be able to be rotated around its center, but keeping the same origin (for setting position, etc). So, I figured I needed to use a Transform.
I have a class, DoorTile, that extends Tile, that extends Drawable (just for context, not really relevant):
private Transform t;
public DoorTile() {
super();
t = new Transform();
setPassable(false);
sprite.setTexture(TileTextures.DOORTEXTURE);
}
public void rotateAroundCenter(float degrees) {
Vector2f center = SpriteUtils.getTextureCenter(TileTextures.DOORTEXTURE);
t = Transform.rotate(t, degrees, center.x, center.y);
}
public void draw(RenderTarget rt, RenderStates states) {
RenderStates newStates = new RenderStates(
states.blendMode,
Transform.combine(states.transform, t),
sprite.getTexture(),
states.shader);
sprite.draw(rt, newStates);
}
getTextureCenter(Texture t) returns the center coordinates of t, and sprite is the sprite of the Tile.
Now, when I apply this rotation, my sprites have a weird behavior. At first, using 45 degrees as a test, they completely disappeared from the screen. So then I tried 1 degree. The sprites affected seemed to have pieces "cut off" and translated downward. Here's a picture, using 1 degree as an argument:
Keep in mind, that I'm applying this rotation before setting their position on the map.
For context, I'm trying to rotate those white sprites (will be 'doors') so they fit properly in the corridor. E.g. horizontal on vertical cooridors, vertical on horizontal corridors. (The white part of the sprite is a placeholder for transparency, that's just there for debugging purposes.)
What should I do to be able to make this work properly? That is, how can I:
- Rotate a sprite around its center
- Keep the original origin (top left corner) for drawing
Thanks!