I could write the algorithm to do this myself if I took the time to work it out, but I wanted to see if someone else has done it first.
I'm working on a voxel engine for a game that I'm working on, and I need to trace a ray from the camera's position out a certain distance to try to find the nearest intersection. I was thinking of just taking the cell that the camera is in, then taking the direction, figuring out at what point the ray leaves the cell, then figure out what the next cell is that it will enter. I would keep doing this until I find an intersection.
Does anyone have experience with this? Maybe you have an algorithm that can do just that?
there are probably faster method's, but if we make a few assumptions about the ray being inside the voxel grid, then some pseudo-code would look like this:
CurrentCell = GetCell(Ray.Start); //we start in the current cell
EndCell = GetCell(Ray.End); //we mark the last cell so we know where to stop.
while(CurrentCell!=EndCell){
//we only need to check at most 3 faces depending on the direction of the ray(since we obviously can't hit faces we're not heading towards)
Vector3 Result; //Store ray point.
int Faces[3];
Faces[0] = Ray.Direction.x<0.0f?LEFT:(Ray.Direction.x>0.0f?Right:-1); //if direction.x == 0 then no need to test the face;
Faces[1] = Ray.Direction.y<0.0f?BOTTOM:(Ray.Direction.y>0.0f?TOP:-1); //" "
Faces[2] = Ray.Direction.z<0.0f?BACK:(Ray.Drection.z>0.0f?FRONT:-1); //" "
for(int i=0;i<3;i++){
if(Faces[i]==-1) continue;
if(CheckPlanexRay(CurrentCell.VoxelPlane[Faces[i]], Ray, &Result)){ //we need the point that intersects this cell.
if(CurrentCell.CheckPointInVoxel(Result)){
CurrentCell = CurrentCell.GetNeighbor(Faces[i]);
if(CurrentCell.isClosed()) return Result, CurrentCell;.
break;
}
}
}
return null;
}
anywho, i'm sure someone else has a better solution, much luck mate=-)