Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt3D picking Point on plane



  • I have a custom 3D scene with a planeEntity (QPlainMesh) as ground plate. To add more objects, I thought of selecting points (by RayCasting?!) and construct the new object there.

    I've tried QScreenRayCaster and QRayCaster, but no one seems to work as expected.
    I trigger my rayCast on Qt3DWindow::mousePressEvent.

    How can I check if the ray has hit the plane or not? Currently I'm getting rayCast hits as soon as I click somewhere (doesnt matter where)

    Do I really need to set a QLayer on every entity I want to hit with my ray?

    I followed this (https://stackoverflow.com/questions/52666741/qscreenraycaster-not-finding-entity-what-am-i-doiong-wrong)

    It's my first project in Qt3D, so dont blame me if I made beginner's mistakes :-)

    Custom3DScene::Custom3DScene()
        : Qt3DExtras::Qt3DWindow()
    {
    
        // Root entity
        m_rootEntity = new Qt3DCore::QEntity();
    
        m_rayCaster = new Qt3DRender::QRayCaster(m_rootEntity);
        m_rayCaster->setRunMode(Qt3DRender::QAbstractRayCaster::SingleShot);
    
    
        Qt3DRender::QLayer *pickableLayer = new Qt3DRender::QLayer(m_rootEntity);
        m_rayCaster->addLayer(pickableLayer);
    
    
        // Camera and Light
        // [ ... ]
    
    
        setRootEntity(m_rootEntity);
    
    
        // Plane data, transform and mesh
        // [ ... ]
    
        // Plane
        m_planeEntity = new Qt3DCore::QEntity(m_rootEntity);
        m_planeEntity->addComponent(planeMesh);
        m_planeEntity->addComponent(planeMaterial);
        m_planeEntity->addComponent(planeTransform);
        m_planeEntity->addComponent(pickableLayer);
    
        // Add rayCaster to rootEntity
        m_rootEntity->addComponent(m_rayCaster);
    
        connect(m_rayCaster, &Qt3DRender::QRayCaster::hitsChanged, this, &Custom3DScene::printCast); // just to see if ray was fired
    
    }
    

    TIA


Log in to reply