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

Using raycasts
http://www.volumesoffun.com/phpBB3/viewtopic.php?f=14&t=286
Page 1 of 1

Author:  GM_Riscvul [ Mon Nov 14, 2011 1:53 am ]
Post subject:  Using raycasts

I have been experimenting with raycasting recently and I am having a big issue...

I can't my raycasts to hit anything.

At least every time I test foundIntersection it is apparently false.

Code:
if(foundIntersection)
{
  //Never gets in here
}


I can only assume I don't understand what the raycast's start and direction should be so they are shooting off in the wrong direction.

Could someone who has used these successfully before explain what start and direction should be in order to get a ray to go the direction you want?

In my case thats straight down.

Thank you for your help!

Author:  David Williams [ Mon Nov 14, 2011 10:32 pm ]
Post subject:  Re: Using raycasts

GM_Riscvul wrote:
I can only assume I don't understand what the raycast's start and direction should be so they are shooting off in the wrong direction.


Can you post a more complete code snippet? In particular, not that the direction vector has to have some significant length to it. If you pass a normalised vector for the direction it won't travel very far. I guess you did see this example?

GM_Riscvul wrote:
In my case thats straight down.


Which way is that? Perhaps you are mixing up Y and Z?

Author:  GM_Riscvul [ Wed Nov 16, 2011 12:29 am ]
Post subject:  Re: Using raycasts

My code is pasted below. I'm simply trying to trace a ray down tell I hit the ground so I can find the y value to attach a tree or plant to. Its a bad way to do plants, but I think it will fit the requirement until I can implement procedural vegetation.

Code:
            Ogre::Vector4 * plantList = new Ogre::Vector4[numTrees + numPlants];
            PolyVox::RaycastResult groundCheckResult;
            Raycast<LargeVolume, MaterialDensityPair44> groundCheck(mWorldVoxels, Vector3DFloat(0,0,0), Vector3DFloat(0,0,0), groundCheckResult);

            for(int i = 0; i < numTrees; i++)
            {
               int x = rand() % 32 + 1;
               int z = rand() % 32 + 1;
               x += reg.getLowerCorner().getX();
               z += reg.getLowerCorner().getZ();

               stringstream test;
               test << "generated coordinates: " << x << " " << z << "\n";

               OutputDebugString(test.str().c_str());

               groundCheck.setStart(Vector3DFloat(x,75,z));
               groundCheck.setDirection(Vector3DFloat(x,74,z));
               groundCheck.execute();

               //If we hit something
               if(groundCheckResult.foundIntersection)
               {
                  //check if it is within bounds
                  if(groundCheckResult.intersectionVoxel.getY() < reg.getUpperCorner().getY() && groundCheckResult.intersectionVoxel.getY() > 32 && groundCheckResult.intersectionVoxel.getY() > -64)
                  {
                     plantList[i] = Ogre::Vector4(x,groundCheckResult.intersectionVoxel.getY(),z,0);
                     stringstream ss;
                     ss << "new loc" << plantList[i].x << " " << plantList[i].y << " " << plantList[i].z << " " << plantList[i].w << "\n";

                     OutputDebugString(ss.str().c_str());
                  }
                  else
                     break;
               }


As far as whether I'm mixing up Y and Z... I though Y was always up and down?

Thanks for the help.

Author:  ker [ Wed Nov 16, 2011 12:47 pm ]
Post subject:  Re: Using raycasts

Code:
groundCheck.setDirection(Vector3DFloat(x,74,z));

there's your problem...
you're supposed to give a direction, not a target position.
that means Vector3DFloat(0, -1, 0)
currently your ray goes up and sideways ;)

Author:  GM_Riscvul [ Thu Nov 17, 2011 1:24 am ]
Post subject:  Re: Using raycasts

Oh duh, its like a camera lookat direction...

For some reason I thought it was a second point, but that makes a lot more sense.

Ok I think that should fix things up.

Thanks for pointing out my error!

Author:  GM_Riscvul [ Fri Nov 18, 2011 6:44 pm ]
Post subject:  Re: Using raycasts

Well I applied the fix, and I'm still not getting any hits.

I suppose it would be more accurate to say that foundIntersection is never true.

Code:
//Generate some plants. This method will be replaced by voxel plants later.
            int numTrees = rand() % 10;
            int numPlants = rand() % 15;

            Ogre::Vector4 * plantList = new Ogre::Vector4[numTrees + numPlants];
            PolyVox::RaycastResult groundCheckResult;
            Raycast<LargeVolume, MaterialDensityPair44> groundCheck(mWorldVoxels, Vector3DFloat(0,0,0), Vector3DFloat(0,0,0), groundCheckResult);

            for(int i = 0; i < numTrees; i++)
            {
               int x = rand() % 32 + 1;
               int z = rand() % 32 + 1;
               x += reg.getLowerCorner().getX();
               z += reg.getLowerCorner().getZ();

               stringstream test;
               test << "Chunk Number: " << x / 32 << " " << reg.getLowerCorner().getY() / 32 << " " << z / 32 << "\n";
               test << "generated coordinates: " << x << " " << z << "\n";

               OutputDebugString(test.str().c_str());

               groundCheck.setStart(Vector3DFloat(x,75,z));
               groundCheck.setDirection(Vector3DFloat(0,-1,0));
               groundCheck.execute();

               //If we hit something
               if(groundCheckResult.foundIntersection)
               {
                  OutputDebugString("Hit!\n");
                  //check if it is within bounds
                  if(groundCheckResult.intersectionVoxel.getY() < reg.getUpperCorner().getY() && groundCheckResult.intersectionVoxel.getY() > 32 && groundCheckResult.intersectionVoxel.getY() > -64)
                  {
                     plantList[i] = Ogre::Vector4(x,groundCheckResult.intersectionVoxel.getY(),z,0);
                     stringstream ss;
                     ss << "new loc" << plantList[i].x << " " << plantList[i].y << " " << plantList[i].z << " " << plantList[i].w << "\n";

                     OutputDebugString(ss.str().c_str());
                  }
                  else
                     break;
               }
            }


I never get the "Hit!" debug message which would indicate that my ray never hits anything in my volume.

I am now using a proper direction, but it still never hits anything in my volume. I'm following the example pretty closely, I'm not sure where I could be going wrong this time.

Author:  VengantMjolnir [ Fri Nov 18, 2011 7:19 pm ]
Post subject:  Re: Using raycasts

Have you checked to make sure that your start position makes sense? I'm not sure what the rand() function you are using returns, but I'd check to make sure you are using correct values for X and Y. Also, you are using -1 for the direction, when the direction vector in the raycast is used both for direction of the ray and its length. You are only checking one unit length down below your 75 position... if your voxel's are not at that height you won't get a collision. You could always make sure the direction vector is long enough to hit the bottom of the volume using your start height and the reg.getLowerCorner().getY()

Hope that helps!

Author:  David Williams [ Sat Nov 19, 2011 12:23 am ]
Post subject:  Re: Using raycasts

VengantMjolnir wrote:
Also, you are using -1 for the direction, when the direction vector in the raycast is used both for direction of the ray and its length.


Yep, I suspect it is this. We need to set a limit on how far the ray can travel, otherwise it will get stuck in an infinite loop in the case that it doesn't hit anything. Providing this via the length of the direction ray seems to make sense.

I do think it would be nice to have another version which takes a start point and an end point... but this would have the same function signature as the existing version. I.e. both cases would be specified by two Vector3Ds so the function call would be ambiguous.

Author:  GM_Riscvul [ Sat Nov 19, 2011 3:06 am ]
Post subject:  Re: Using raycasts

Ahh... it doesn't go forever... I suppose that's what the notes on length meant.

Well that will be easy to fix.

I've already checked the generated coordinates and they are okay. So it should be the length.

Thanks again. Hopefully after all of this I will know enough to be able to help others on this forum too.

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