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

Single-shot ray cast: won't disable itself after the shot



  • I have this ray caster setup:

    m_rayCaster = new Qt3DRender::QRayCaster(m_scene->rootEntity());
    m_rayCaster->setRunMode(Qt3DRender::QAbstractRayCaster::SingleShot);
    m_scene->rootEntity()->addComponent(m_rayCaster);
    

    I'm doing consecutive ray cast tests. I handle hit results with this slot:

    QObject::connect(m_rayCaster, &Qt3DRender::QRayCaster::hitsChanged, this, &RayCastHandler::handleRayCasterHits);
    
    void RayCastHandler::handleRayCasterHits(const Qt3DRender::QAbstractRayCaster::Hits hits)
    {
        analyzeHits( ... , hits);
        return;
    }
    
    

    To decide when to do the next ray cast test, I'm using this slot. The logic is that whenever the ray-caster disables itself, it is ready to do the next test, according to this documentation.

    QObject::connect(m_rayCaster, &Qt3DCore::QNode::enabledChanged, this, &RayCastHandler::handleRayCasterEnabledChange);
    
    void RayCastHandler::handleRayCasterEnabledChange(const bool enabled)
    {
        // When the component disables itself, it is ready for the next ray-cast test
        if (!enabled) {
            bool required = isNextTestRequired( ... );
            if (required)
                triggerTest( ... );
            else
                // Send final ray-cast results by a signal, if next test is NOT needed
                emit rayCastResultsChanged( ... );
        }
        return;
    }
    

    I have a strange observation: the following code does NOT work. I mean, at some point ray-caster remains enabled forever and it won't disable itself after a test (cast):

    void RayCastHandler::triggerTest( ... )
    {
        ...
        // No delay
        rayCaster->trigger(origin, direction, length);
    
        ...
    }
    

    However, by adding a delay of a few milliseconds, the code works, at least most of the time. Sometimes I have to increase the time-delay to make it work:

    void RayCastHandler::triggerTest( ... )
    {
        ...
        // 1 millisecond delay time
        QTimer::singleShot(1, [rayCaster, origin, direction, length](){rayCaster->trigger(origin, direction, length);});
    
        ...
    }
    

    Does anybody know the cause of this observation? How can I do consecutive ray-casts reliably?


Log in to reply