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

Author Topic: Proper zoom usage  (Read 2953 times)

0 Members and 1 Guest are viewing this topic.

Arith

  • Newbie
  • *
  • Posts: 3
    • View Profile
Proper zoom usage
« on: August 11, 2014, 04:01:33 pm »
So, I'm fairly new to SFML and I've run into some unusual behaviour. I'm wondering if I'm using the zoom function incorrectly.
I've adapted my camera code to mostly work with sf::View. It scrolls across the map to my satisfaction. Until I use zoom. Then the map coasts off into oblivion on load.

I'm going to paraphrase my code, to copy it would clutter up the post

Camera.cpp:
    void camApplyTranslation()
    {
        Camera.wView.setCenter(sf::Vector2f(Camera.x, Camera.y));
// Problem here
        // Camera.wView.zoom(Camera.z);
    }

    void camInitCamera()
    {
       // ... other camera initializations

        Camera.wView.setCenter(sf::Vector2f(0,0));
        Camera.wView.setSize(1024, 768);
        Camera.wView.zoom(1.0);
        Window.setView(Camera.wView);
        camLookAt(200.0,200.0,1.0); // Move to view these coordinates on load
    }
 

Game.cpp
void GameLoop()
{
  camInitCamera();
  while (Window.isOpen())
  {
    // Controls
    Window.clear();
    camPollCamera();
    camApplyTranslation();
    Window.setView(Camera.wView);
    //Draw stuff
    Window.display();
  }
}

The code in it's current state works, minus the zoom functionality. When I uncomment that zoom line, as soon as the map loads I can see it zoom out until the map is invisible. I had a look at the variables per-frame and there's nothing that would tell me something is wrong. Camera.z starts out at 5.0f and slowly decreases to 1.0f every frame.  Instead each frame is zooming the map away from the camera.

So, to establish a baseline, I changed that line to Camera.wView.zoom(1.0);
Which worked, of course minus the zoom, being the same value every frame.  So that tells me it's not zooming out  by x every frame. I've played around enough with the static value on wView.zoom() to understand what range I'm looking at. The documentation I've come across seems to agree with me. zoom(0.5) would effectively zoom in half way into the map. zoom(2.0) would zoom out away from the map.

The camPollCamera() is a bit long, but in short what it does is manage a few floats representing the camera position and target position. As I mentioned, I checked that the values are in acceptable ranges using my debugger. It seems with static values, the zoom behaves like a static value. With a variable in place, it behaves like the value is cumulative.

I tested that, by rigging a test float to it. Started it out at 0.0 and each frame would increase it by 0.05, capping it at 1.1. In theory that should start the camera right up to the map, and slowly zoom out to just outside of 'normal' view. Instead, the view just hovered just in front of the map right up close, yet the variable was behaving as expected: ramping from 0..1.1 each frame. Stranger yet, awhile after the test float had plateaued to 1.1, the map just zoomed off into oblivion anyway.

Comment that zoom line again, and everything works fine. So I'm obviously using it wrong.
Could someone enlighten me, or point me to some reading materials? (Yes, I've read most of the documentation on this site - though might not have completely exhausted the forums yet.)

Using SFML 2.1, Thanks



G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Proper zoom usage
« Reply #1 on: August 11, 2014, 04:54:45 pm »
Zooming is achieved by a factor, relative to the current zoom.
Thus zooming twice with an 0.5 factor is the same as zooming once with an 0.25 factor. Zooming with a factor of 1 does nothing.

Arith

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Proper zoom usage
« Reply #2 on: August 11, 2014, 08:47:19 pm »
Thank you. It all makes sense now.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Proper zoom usage
« Reply #3 on: August 12, 2014, 05:51:24 pm »
There a bit of information about this in this tutorial (scroll to "Zooming (scaling) the view"). Zoom() acts like move() and rotate(), whereas setSize() acts like setCenter() and setRotation().
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Arith

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Proper zoom usage
« Reply #4 on: August 14, 2014, 11:38:30 pm »
I wanted to mention, my last post kinda sounds sarcastic now that I read it again.
Not the case, I genuinely had my head stuck in a certain mode... been coding the entire day you see.
So used to dealing with absolute values I just couldn't figure it out.
Thanks again folks, much appreciated.

 

anything