Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Modifying QFirstPersonCameraController (keybinds)
Qt 6.11 is out! See what's new in the release blog

Modifying QFirstPersonCameraController (keybinds)

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 252 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    choriz
    wrote on last edited by
    #1

    0

    Just started using QT and I've ran into what is probably a very simple problem. I'm making a 3D scene where you're supposed to move the camera through space using WASD + mouse for rotation. The existing QFirstPersonCameraController worked great except for the key binds. I decided to make my own camera class inheriting from QAbstractCameraController to change this and potentially other things later on.

    I've essentially just copied the code from QFirstPersonCamera but I can't wrap my mind around how the keyboard movement works. How does the InputState work? Where are the keybinds set up for the tAxisValues? I feel really lost and couldn't find anything when trying to search for it.

    customcamera.h:

    #ifndef CUSTOMCAMERA_H
    #define CUSTOMCAMERA_H
    
    #include <Qt3DExtras/qabstractcameracontroller.h>
    
    namespace Qt3DExtras
    {
    
    class CustomCamera : public QAbstractCameraController
    {
        Q_OBJECT
    
    public:
        explicit CustomCamera(Qt3DCore::QNode* parent = nullptr);
        ~CustomCamera();
    
    private:
        void moveCamera(const InputState &state, float dt) override;
    };
    
    }
    
    
    #endif // CUSTOMCAMERA_H
    

    customcamera.cpp:

    #include "customcamera.h"
    
    #include <Qt3DRender/QCamera>
    
    QT_BEGIN_NAMESPACE
    
    namespace Qt3DExtras {
    
    CustomCamera::CustomCamera(Qt3DCore::QNode *parent)
        : QAbstractCameraController(parent)
    {
    }
    
    CustomCamera::~CustomCamera()
    {
    }
    
    void CustomCamera::moveCamera(const QAbstractCameraController::InputState &state, float dt)
    {
        Qt3DRender::QCamera *theCamera = camera();
    
        if (theCamera == nullptr)
            return;
    
        theCamera->translate(QVector3D(state.txAxisValue * linearSpeed(),
                                          state.tyAxisValue * linearSpeed(),
                                          state.tzAxisValue * linearSpeed()) * dt);
    
        if (state.leftMouseButtonActive) {
            float theLookSpeed = lookSpeed();
            if (state.shiftKeyActive) {
                theLookSpeed *= 0.2f;
            }
    
            const QVector3D upVector(0.0f, 1.0f, 0.0f);
    
            theCamera->pan(state.rxAxisValue * theLookSpeed * dt, upVector);
            theCamera->tilt(state.ryAxisValue * theLookSpeed * dt);
        }
    }
    
    
    } // Qt3DExtras
    
    QT_END_NAMESPACE
    

    I was thinking that maybe I could just use normal key press events or whatever you would use for holding down buttons but shouldn't the code related to moving the camera go inside the moveCamera() function? Also unsure how I continuously call such a function while a button is held down.

    Any help appreciated!

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved