Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Screen coordinate and world coordinate troubles  (Read 2089 times)

0 Members and 1 Guest are viewing this topic.

foobarbaz

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Screen coordinate and world coordinate troubles
« on: April 23, 2013, 06:08:39 am »
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.
« Last Edit: April 23, 2013, 06:56:46 am by DrSuperSocks »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: Screen coordinate and world coordinate troubles
« Reply #1 on: April 23, 2013, 07:12:07 am »
With the latest SFML version from Git (not the RC) you can simply use window.mapPixelToCoords and window.mapCoordsToPixel, which works for any RenderTarget with any View. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

foobarbaz

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Screen coordinate and world coordinate troubles
« Reply #2 on: April 23, 2013, 07:45:05 am »
Works like a charm, thanks!

 

anything