Move camera along y-axis by scrolling the mouse wheel

Is there a way to make the camera move along an axis when scrolling the mouse wheel? I tried EasyCam but in that case the wheel makes the camera go “forwards/backwards”, while I would need a camera that is fixed and only moves up and down along the y axis.

I tried to find a way to measure the rotation of the mouse wheel so that I could then produce a custom camera movement based on that but I haven’t been able to do it so far. I mostly found people talking about measuring the y movement of the cursor to calculate the scrolling indirectly or other similar suggestions, but I need to move the camera based on the scrolling input, not the opposite.

I tried mouseScrolled and mouseWheel but they seem not to trigger when I scroll the mouse wheel, maybe because they don’t measure the actual scrolling of the mouse wheel but whether something moves in the program, if I understood correctly?

Hey @Atlas! I worked on this for a bit. ofEasyCam has a lot of private class members, so deriving a child class from it is very difficult.

An easy solution might be to use ofCamera instead:

// in ofApp.h
    // add a method to run when the mouse is scrolled
    void mouseScrolled(ofMouseEventArgs& mouse);
    // and add a camera
    ofCamera camera;

// in ofApp.setup()
    // listen for when the mouse is scrolled
    ofAddListener(ofEvents().mouseScrolled, this, &ofApp::mouseScrolled);

// in ofApp.cpp
// this function will run from the listener when the mouse is scrolled
void ofApp::mouseScrolled(ofMouseEventArgs& mouse){
    glm::vec3 position = camera.getPosition();
    position.y += mouse.scrollY;
    camera.setPosition(position);
}

Both ofEasyCam and ofCamera derive from ofNode, so their functions related to position, rotation, orientation, etc should all be the same.

Don’t forget to delete the listener when the application exits:

    ofRemoveListener(ofEvents().mouseScrolled, this, &ofApp::mouseScrolled);

You can also use an ofEventListener, which is an object and doesn’t need to be deleted because its destroyed when it goes out of scope. I like to use them with a lambda:

// in ofApp.h
    ofCamera camera;
    ofEventListener listener;

// in ofApp.setup()
    listener = ofEvents().mouseScrolled.newListener([this](ofMouseEventArgs& m){
        // do something unique here, and/or call another function:
        mouseScrolled(m);
    });

Finally, ofEasyCam has a way to set the functions of the mouse buttons, but not the scroll wheel. roymacdonald has a wonderful summary of how to do this:

Thank you, I will try it and let you know.

Hi, maybe we could add the scrollwheel interaction custumization option to ofEasyCam. It does not look like it would be much of a problem. I’ll see how it works and submit a PR once done.

1 Like