It is currently Sat Aug 22, 2020 2:03 pm


All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Using raycasts
PostPosted: Mon Nov 14, 2011 1:53 am 
User avatar

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

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


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Using raycasts
PostPosted: Mon Nov 14, 2011 10:32 pm 
Developer
User avatar

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


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Using raycasts
PostPosted: Wed Nov 16, 2011 12:29 am 
User avatar

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

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


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Using raycasts
PostPosted: Wed Nov 16, 2011 12:47 pm 
User avatar

Joined: Wed Jan 26, 2011 3:20 pm
Posts: 203
Location: Germany
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 ;)


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Using raycasts
PostPosted: Thu Nov 17, 2011 1:24 am 
User avatar

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

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


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Using raycasts
PostPosted: Fri Nov 18, 2011 6:44 pm 
User avatar

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

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


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Using raycasts
PostPosted: Fri Nov 18, 2011 7:19 pm 

Joined: Thu Nov 17, 2011 1:33 am
Posts: 2
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!


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Using raycasts
PostPosted: Sat Nov 19, 2011 12:23 am 
Developer
User avatar

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


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Using raycasts
PostPosted: Sat Nov 19, 2011 3:06 am 
User avatar

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

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


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 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