0

Once again, camera zooming on a 2D-Plane. I searched a lot and know that there are similar questions, but I am obviously way too stupid to apply what I was able to find.

Basically I multiply the distance of all elements to the origin by mouseDelta, which is a double between 0.5 and 1. works fine for all elements, but since the anchor of the camera (camX, camY) are the upper left corner of the camera, the objects in the focus of the cam change their position in relation to the focus. I want to scroll "towards" the focus. Here is what I got, but it behaves really weird:

camX and camY, as mentioned, are the coordinates for the upper left of the cam. mouseDelta is the zoom-level thats stored globally and is changed by each wheel-event. screenX is the width of the screen/window (fullscreen anyways) screenY is the height of the screen/window

if (newEvent.type == sf::Event::MouseWheelMoved) //zoom
{

    mouseDelta += ((double)newEvent.mouseWheel.delta)/20;
    if (mouseDelta > 1) { mouseDelta = 1; }
    else if (mouseDelta < 0.5) { mouseDelta = 0.5; }

    //resize graphics
    for (int i = 0; i < core->universe->world->nodes.size(); i++) {
        core->universe->world->nodes.at(i).pic->setSize(mouseDelta);
    }
    for (int i = 0; i < core->universe->world->links.size(); i++) {
        core->universe->world->links.at(i).pic->setSize(mouseDelta);
    }

    camX = (camX + screenX/2) - (camX + screenX/2)*mouseDelta;
    camY = (camY + screenY/2) - (camY + screenY/2)*mouseDelta;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
user3808217
  • 135
  • 3
  • 12
  • Don't be afraid to use matrix math. – Rook Jun 11 '17 at 14:59
  • this might have what you need https://www.sfml-dev.org/tutorials/2.0/graphics-view.php – Crimson Jun 12 '17 at 12:43
  • Possible duplicate of [Zooming graphics based on current mouse position](https://stackoverflow.com/questions/37262282/zooming-graphics-based-on-current-mouse-position) – Spektre Jun 16 '17 at 05:10
  • see the duplicate QA in my answer is what you need (it is in C++) and use screen center instead of mouse position. – Spektre Jun 16 '17 at 05:11

0 Answers0