Page 1 of 1

ray query object returns 0

Posted: Sat Apr 20, 2019 11:15 pm
by rrl
Ogre Version: :2.1:
Operating System: :linux:
Render System: :GL3+:

When I click on a mesh, this mouseDown event eventually executes a ray scene query like so ...

Code: Select all

    void MyWidget::mousePressEvent(QMouseEvent* e)
    {
        cMgr->injectMouseDown(e);

        Ogre::Camera *cam = cMgr->getCamera();

        Ogre::Ray ray = cam->getCameraToViewportRay(e->x(), e->y());

        if (!gs->cast_ray(ray))
	    return;
    }

    bool MyGame::cast_ray(Ogre::Ray &ray)
    {
        if (!rSceneQuery)
	    return false;

        rSceneQuery->setRay(ray);

        if (rSceneQuery->execute().size() <= 0)  // this returns zero
            return false;

	// ...
The problem is, rSceneQuery->execute().size() returns 0. Any ideas where I'm going wrong?

Re: ray query object returns 0

Posted: Mon Apr 22, 2019 8:19 am
by Tobse
May be the ray calculation is wrong. Check that getCameraToViewportRay got values between 0 and 1.

Re: ray query object returns 0

Posted: Sat Apr 27, 2019 6:10 pm
by rrl
Maybe I'm misunderstanding, but the values that I'm passing in to getCameraToViewportRay() are x and y valued on where I clicked on the Ogre canvas.

How do I make these values between 0 and 1?

Re: ray query object returns 0

Posted: Sat Apr 27, 2019 6:20 pm
by xrgo
the values are in pixel units? just normalize them:

Code: Select all

float x = e->x()/width
float y = e->y()/height
maybe you'll need to subtract an offset for the position of the window:

Code: Select all

float x = (e->x()-xwinpos)/width
float y = (e->y()-ywinpos)/height
not sure if that answer the question

Re: ray query object returns 0

Posted: Sat Apr 27, 2019 6:48 pm
by rrl
Yes, it helps, but I'm still unable to get the value between 0 and 1. Do I not have the same thing here?

Code: Select all

    void MyWidget::mousePressEvent(QMouseEvent* e)
    {
        cameraMgr->injectMouseDown(e);

        Ogre::Real x = Ogre::Math::Abs(e->x());
        Ogre::Real y = Ogre::Math::Abs(e->y());

        Ogre::Viewport *vport = ogreWindow->getViewport(0);

        Ogre::Real width = e->x() / vport->getActualWidth();
        Ogre::Real height = e->y() / vport->getActualHeight();

        std::cout << "width = " << std::fixed << std::setprecision(3) << width << std::endl;
        std::cout << "height = " << std::fixed << std::setprecision(3) << height << std::endl;

        Ogre::Camera *camera = cameraMgr->getCamera();

        Ogre::Ray ray = camera->getCameraToViewportRay(width, height);

	// ...

Re: ray query object returns 0

Posted: Sat Apr 27, 2019 8:10 pm
by xrgo
print the e->x() and e->y() values while your cursor is in the left top corner, and in the bottom right... that way you'll now if there's an offset going on, and if the width and height fit the ones that getActual_ gives you