Volumes Of Fun
http://www.volumesoffun.com/phpBB3/

Ogre camera raycast woes
http://www.volumesoffun.com/phpBB3/viewtopic.php?f=14&t=321
Page 1 of 1

Author:  charlieb [ Thu Jan 26, 2012 5:13 pm ]
Post subject:  Ogre camera raycast woes

I have a 64x64 volume with a sphere of radius 30 centered on (32,32). Which I meshized for ogre::manualobject as in the example in the wiki. The node to which this mesh is attached is at the origin so ogre's and polyvox's coordinate systems are aligned (right?).

I am using the ogre::camera's getPosition and getDirection members and passing the results directly to the polyvox raycast setStart and setDirection. I have another mesh that I'm using as a marker that I put at the point given by raycastresult so I can visually inspect the result.

The problem is that the results are very unreliable. Sometimes the intersection will be in the perfect place right in the middle of the screen, sometimes it will be off-center or at the edge and sometimes no intersection will be detected at all even though the voxel sphere fills the entire viewport.

There is a region of perfect accuracy that I discovered offset in a particular axis (I forget which) from the sphere's center (the camera position == sphere center for 2 out of 3 axes). As I move away from alignment the marker drifts diagonally away from center in the direction of movement. (I move left and the marker moves down and left, I move right the marker moves up and right).

I suspect that there is a common gotcha that I have fallen victim to. If not I'll post code and screens when I get a chance.

Cheers,
Charlie

Author:  GM_Riscvul [ Thu Jan 26, 2012 11:11 pm ]
Post subject:  Re: Ogre camera raycast woes

This is due to a slight bit of confusion on what the parameters of raycast are. Start is the position as you have guessed, but direction is not as simple as simply using the camera's direction. The length of the vector determines how far the raycast will go. They have to limit the distance in order to avoid an infinite search.

The example tries to show this but at least for me it wasn't terribly clear. In another thread this was cleared up for me.

Quote:
Vector3DFloat start(rayOrigin.x(), rayOrigin.y(), rayOrigin.z());
Vector3DFloat direction(rayDir.x(), rayDir.y(), rayDir.z());
direction.normalise();
direction *= 1000.0f; //Casts ray of length 1000

RaycastResult raycastResult;
Raycast<Material8> raycast(m_pPolyVoxVolume, start, direction, raycastResult);
raycast.execute();


The important bit is the two lines regarding direction. First it is normalized then multiplied by the desired length. This combination should give you the length and direction you want.

Author:  charlieb [ Thu Jan 26, 2012 11:46 pm ]
Post subject:  Re: Ogre camera raycast woes

I'm just starting out with both Ogre and PolyVox. My application consists almost entirely of copypasta. Below is the meat of my mouseMoved function. Looks familiar no :)

And of course as I type this I have spotted my error. (note the extra pos.z in the start initializer)
Well, this was the outcome I was looking for so thanks :D
I just tested the fix and it works perfectly.

Cheers,
Charlie

Code:
   Ogre::Vector3 pos = mCamera->getPosition();
   Ogre::Vector3 dir = mCamera->getDirection();
   // Find the voxel we are looking at.
   PolyVox::Vector3DFloat start(pos.x,pos.z,pos.z);
   PolyVox::Vector3DFloat direction(dir.x, dir.y, dir.z);
   std::cout << start << std::endl;
   std::cout << direction << std::endl;
   direction.normalise();
   direction *= 10000.0f; //Casts ray of length 1000
   
   PolyVox::RaycastResult raycastResult;
   PolyVox::Raycast<PolyVox::SimpleVolume, PolyVox::MaterialDensityPair44>
      raycast(mSphereVox, start, direction, raycastResult);
   raycast.execute();
   
   if(raycastResult.foundIntersection) {
      mSceneMgr->getSceneNode("MarkerNode")->
         setPosition(raycastResult.intersectionVoxel.getX(),
                           raycastResult.intersectionVoxel.getY(),
                           raycastResult.intersectionVoxel.getZ());
      std::cout << raycastResult.intersectionVoxel << std::endl;
   }
   std::cout << std::endl;

Author:  David Williams [ Fri Jan 27, 2012 9:08 am ]
Post subject:  Re: Ogre camera raycast woes

Glad you got it working.

I've now added a further note to the docs regarding the need to set the direction vector to a suitable length as this does seem to cause some confusion (you can't read the note until the next release). I've also renamed the function parameter from v3dDirection to v3dDirectionAndLength in the hope that it makes people stop and think. Lastly, I'll try to add some kind of warning when the direction vector is very short as I don't think people will really want to do this.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/