[SOLVED] Problems with raycasting
Posted: Thu Oct 06, 2011 4:55 pm
Hi again,
I have a problem with some ray casts: I have a mesh which is a piece of a dungeon with stairs, corridors, platforms and so on. This mesh is a StaticMesh.
On this mesh I have to create a grid which, in the future, will be the grid I have to use for the AI, basically a grid of valid points for an A* implementation. Every point of the grid must be tested with a ray cast: if I hit then there is a valid point, which means a valid passage. Then I test every point with it's neighbours and I'll have a grid of connections. In the end I'll have a bidimensional array of points which tell me I can stay in a point of the grind and how I can move further.
Since I have this StaticMesh I just used the raycast function from there, taking the points starting from the BoundingBox, like this:
This code work perfectly. The matter is the scene is composed also of some placeables and this code will ignore them hitting the mesh under those placeables and adding unvalid points
So I had to pass to Space.raycast. I added to the scene a sample static box and I used instead this code:
Which seems to work sometimes. The spheres I used as debug are very few, only on a part of my mesh. Instead, on the box I used for debug there are correctly as many sphere as they should be. Look at the pictures I attached. EDIT: sorry, it seems the first one is at the bottom and the last one at the top, look at the file names please.
On the first one (eh_grid.png) I used StaticMesh.rayCast and the grid is fully created (the black lines represent the connections) but as you can see below the green box there are also some spheres which shouldn't be there (instead they should be on top of that box).
On the second picture (eh_grid2.png) I used Space.rayCast which correctly stop the ray on top of the box but may others rays fails.
If I enlarge the green box ad infinitum (eh_grid3.png) I have all my raycasts in the correct positions but on top of the green box (picture 3).
This means the raycasts seems to be in the correct place but if I use Space.rayCast many of them fails touching the StaticMesh but not the Box.
Can you help me understand why?
Do I mess up with the positions or what?
Consider also both the mesh and the box are not at the world origin, so I suppose I used the right translations when creating the Ray class.
I have a problem with some ray casts: I have a mesh which is a piece of a dungeon with stairs, corridors, platforms and so on. This mesh is a StaticMesh.
On this mesh I have to create a grid which, in the future, will be the grid I have to use for the AI, basically a grid of valid points for an A* implementation. Every point of the grid must be tested with a ray cast: if I hit then there is a valid point, which means a valid passage. Then I test every point with it's neighbours and I'll have a grid of connections. In the end I'll have a bidimensional array of points which tell me I can stay in a point of the grind and how I can move further.
Since I have this StaticMesh I just used the raycast function from there, taking the points starting from the BoundingBox, like this:
Code: Select all
StaticMesh Sector; // Got from somewhere.
Space Spazio; // Got from somewhere,
float startHeight = Sector.BoundingBox.Max.Y + m_fRayCastHeight;
Vector3 position = new Vector3(0, startHeight, 0);
Vector3 direction = new Vector3(0, -1, 0);
Ray raggio = new Ray(position, direction);
RayHit colpito = new RayHit();
RayCastResult risultato;
for (uint i = 0; i < Cols; i++)
{
position.X = Sector.BoundingBox.Min.X + i * Step;
for (uint j = 0; j < Rows; j++)
{
position.Z = Sector.BoundingBox.Min.Z + j * Step;
raggio.Position = position;
Node iter;
iter.connections = (byte)(Sector.RayCast(raggio, Single.MaxValue, out colpito) ? 1 : 0);
iter.height = colpito.Location.Y;
if (hit)
add a debug sphere on colpito.Location
}
}
}
So I had to pass to Space.raycast. I added to the scene a sample static box and I used instead this code:
Code: Select all
StaticMesh Sector; // Got from somewhere.
Space Spazio; // Got from somewhere,
float startHeight = Sector.BoundingBox.Max.Y + m_fRayCastHeight;
Vector3 position = new Vector3(0, startHeight, 0);
Vector3 direction = new Vector3(0, -1, 0);
Ray raggio = new Ray(position, direction);
RayHit colpito = new RayHit();
RayCastResult risultato;
for (uint i = 0; i < Cols; i++)
{
position.X = Sector.BoundingBox.Min.X + i * Step;
for (uint j = 0; j < Rows; j++)
{
position.Z = Sector.BoundingBox.Min.Z + j * Step;
raggio.Position = position;
Node iter;
iter.connections = (byte)(Sector.RayCast(raggio, Single.MaxValue, out risultato) ? 1 : 0);
iter.height = risultato.HitData.Location.Y;
if (hit)
add a debug sphere on risultato.HitData.Location
}
}
}
On the first one (eh_grid.png) I used StaticMesh.rayCast and the grid is fully created (the black lines represent the connections) but as you can see below the green box there are also some spheres which shouldn't be there (instead they should be on top of that box).
On the second picture (eh_grid2.png) I used Space.rayCast which correctly stop the ray on top of the box but may others rays fails.
If I enlarge the green box ad infinitum (eh_grid3.png) I have all my raycasts in the correct positions but on top of the green box (picture 3).
This means the raycasts seems to be in the correct place but if I use Space.rayCast many of them fails touching the StaticMesh but not the Box.
Can you help me understand why?
Do I mess up with the positions or what?
Consider also both the mesh and the box are not at the world origin, so I suppose I used the right translations when creating the Ray class.