ray query object returns 0

Problems building or running the engine, queries about how to use features etc.
Post Reply
rrl
Halfling
Posts: 78
Joined: Sun Jun 04, 2017 12:33 am

ray query object returns 0

Post 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?
Tobse
Kobold
Posts: 26
Joined: Tue Apr 10, 2007 12:21 am

Re: ray query object returns 0

Post by Tobse »

May be the ray calculation is wrong. Check that getCameraToViewportRay got values between 0 and 1.
rrl
Halfling
Posts: 78
Joined: Sun Jun 04, 2017 12:33 am

Re: ray query object returns 0

Post 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?
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 168

Re: ray query object returns 0

Post 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
rrl
Halfling
Posts: 78
Joined: Sun Jun 04, 2017 12:33 am

Re: ray query object returns 0

Post 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);

	// ...
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 168

Re: ray query object returns 0

Post 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
Post Reply