Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. My CharacterController won't fall

My CharacterController won't fall

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 18 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.
  • S Offline
    S Offline
    Svyat
    wrote last edited by
    #1

    I have a CharacterController, gravity is -9.18 but it just doesn't want to fall.
    My code:

    import QtQuick
    import QtQuick3D
    import QtQuick3D.Helpers
    import QtQuick3D.Physics
    
    Window
    {
        id: window
        width: 1920
        height: 1080
        visible: true
        title: "qt quick 3d"
        visibility: Window.FullScreen
    
        property real moveSpeed: 4
    
        Timer
        {
            interval: 16
            running: true
            repeat: true
    
            onTriggered: {
                let dir = control.wasdVector(player.eulerRotation.y)
                player.movement = Qt.vector3d(
                    dir.x * moveSpeed,
                    player.movement.y,
                    dir.z * moveSpeed
                )
            }
        }
    
        View3D
        {
            anchors.fill: parent
            PhysicsWorld
            {
                id: physWorld
                gravity: Qt.vector3d(0, -100, 0)
                running: true
            }
    
            environment: SceneEnvironment
            {
                lightProbe: Texture
                {
                    source: "sunny_rose_garden_2k.hdr"
                }
                backgroundMode: SceneEnvironment.SkyBox
            }
    
    
            CharacterController
            {
                id: player
                position: Qt.vector3d(0, 20, 0)
                gravity: physWorld.gravity
                simulationEnabled: true
    
                collisionShapes: CapsuleShape
                {
                    height: 1.8
                    diameter: 0.4
                }
    
                PerspectiveCamera
                {
                    id: cam
                    y: 1.8
                }
            }
    
    
            Model {
                source: "#Cube"
                id: floor
                materials: PrincipledMaterial
                {
                    baseColor: "grey"
                    metalness: 0.0
                    roughness: 0.5
                }
                scale: Qt.vector3d(1, 0.01, 1)
    
                StaticRigidBody
                {
                    collisionShapes: BoxShape {}
                }
            }
        }
        /*WasdController
        {
            controlledObject: cam
        }*/
    
        Timer {
            interval: 500
            running: true
            repeat: true
            onTriggered: {
                console.log(
                    "pos.y =", player.position.y,
                    "movement =", player.movement, "gravity =", player.gravity
                )
            }
        }
    
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            focus: true
            acceptedButtons: Qt.AllButtons
            cursorShape: Qt.BlankCursor
    
            property real yaw: 0
            property real pitch: 0
            property real sensitivity: 0.2
            property real lastX: 0
            property real lastY: 0
    
            onPositionChanged: (m) => {
                // вычисляем дельту относительно центра
                let dx = m.x - width/2
                let dy = m.y - height/2
    
                yaw   -= dx * sensitivity
                pitch -= dy * sensitivity
                pitch = Math.max(-80, Math.min(80, pitch))
    
                cam.eulerRotation = Qt.vector3d(pitch, 0, 0)
                player.eulerRotation = Qt.vector3d(0, yaw, 0)
    
                // возвращаем мышь в центр
                control.setMousePos(width/2, height/2)
                lastX = width/2
                lastY = height/2
            }
    
            Component.onCompleted: {
                forceActiveFocus()
                control.setMousePos(width/2, height/2)
            }
    
            Keys.onPressed: (event) =>
            {
                if(event.key === Qt.Key_W) control.onWPressed()
                if(event.key === Qt.Key_A) control.onAPressed()
                if(event.key === Qt.Key_S) control.onSPressed()
                if(event.key === Qt.Key_D) control.onDPressed()
                if(event.key === Qt.Key_Space) control.onSpacePressed()
                if(event.key === Qt.Key_Control) control.onCtrlPressed()
                if(event.key === Qt.Key_Shift) control.onShiftPressed()
                }
            Keys.onReleased: (event) =>
            {
                if(event.key === Qt.Key_W) control.onWReleased()
                if(event.key === Qt.Key_A) control.onAReleased()
                if(event.key === Qt.Key_S) control.onSReleased()
                if(event.key === Qt.Key_D) control.onDReleased()
                if(event.key === Qt.Key_Space) control.onSpaceReleased()
                if(event.key === Qt.Key_Control) control.onCtrlReleased()
                if(event.key === Qt.Key_Shift) control.onShiftReleased()
            }
        }
    }
    
    

    The c++ function wasdVector works as intended, so where's the problem?

    P.S. sorry if the code is messy

    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