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. Single-shot ray cast: won't disable itself after the shot
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 115 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.
  • M Offline
    M Offline
    m3g1dd
    wrote on last edited by
    #1

    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?

    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