It is currently Sat Aug 22, 2020 3:31 pm


All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Ogre camera raycast woes
PostPosted: Thu Jan 26, 2012 5:13 pm 

Joined: Wed Jan 25, 2012 12:29 am
Posts: 18
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


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ogre camera raycast woes
PostPosted: Thu Jan 26, 2012 11:11 pm 
User avatar

Joined: Tue Feb 22, 2011 8:04 am
Posts: 101
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.

_________________
--Real Programmers use a magnetized needle and a steady hand.
--xkcd--


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ogre camera raycast woes
PostPosted: Thu Jan 26, 2012 11:46 pm 

Joined: Wed Jan 25, 2012 12:29 am
Posts: 18
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;


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ogre camera raycast woes
PostPosted: Fri Jan 27, 2012 9:08 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
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.


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net