Hey all, I'm having problems converting between screen and world coordinates when zooming my views. Everything has been going smoothly up until I added zooming. As long as I don't zoom in or out, I can rotate and move the camera and still have proper world/screen coordinate conversions. Also, it seems when I zoom, it scales things about the top left of the screen rather than the center even though I set the center of the view to the center of the screen.
My questions are these: How do I zoom relative to the center of the screen instead of the top left corner? And how do I properly convert screen coordinates to world coordinates and vice versa when scaling is involved?
Edit: Actually, it seems like the zooming is about the lower right hand corner o.0 and it doesn't change no matter what I set the center of the view to.
View code:
mView.setCenter(mRenderWindow->getSize().x/2, mRenderWindow->getSize().y/2);
mView.setRotation(-mCameraRotation);
mView.zoom(mCameraZoom);
mRenderWindow->setView(mView);
Coordinate conversion code:
sf::Vector2f RenderingManager::screenToWorld(sf::Vector2f screenPos)
{
screenPos -= getCameraScreenOffset();
screenPos *= mCameraZoom;
screenPos += getCameraScreenOffset();
sf::Vector2f worldPos = screenPos-getCameraScreenOffset();
worldPos.x = worldPos.x/mPTU;
worldPos.y = -worldPos.y/mPTU;
worldPos.rotateBy(getCameraRotation(), getCameraPosition());
return worldPos;
}
sf::Vector2f RenderingManager::worldToScreen(sf::Vector2f worldPos)
{
sf::Vector2f screenPos = worldPos;
screenPos -= getGame()->getRenderingManager()->getCameraPosition();
screenPos /= mCameraZoom;
screenPos += getGame()->getRenderingManager()->getCameraPosition();
screenPos.rotateBy(-getCameraRotation(), getCameraPosition());
screenPos.x *= mPTU;
screenPos.y *= -mPTU;
screenPos += getCameraScreenOffset();
return screenPos;
}
Thanks for your time.