Sign in to follow this  
martinperry

RayCasting problem

Recommended Posts

I have problem with my raycasting. I have 8 cubes, that are first level of octree (Eg. they create bigger cube).
I need raycast them and "render" cube. If I have only one cube, raycasting works fine. But if I use 8 cubes, i have problem. If I move my camera, gaps between cubes start to appear. Cubes are moving apart.

Whole octree bounding box min is (0,0,0), max is (64, 64, 64). Each box has size 32x32x32 and all boxes are set in [0,0,0] and move to their right position with transform matrix tmp

[code]
for (int i = 0; i < 8; i++)
{
//(AB)-1 = B-1 * A-1
MyMath::Vector3 centerVec = this->streams[i].center; //left-bottom corner of box
MyMath::Matrix4x4 tmp = MyMath::Matrix4x4::Translation(centerVec);

tmp = tmp * *(camera->GetViewMatrix());


tmp = MyMath::Matrix4x4::Invert(tmp);

CastRays(tmp, camera);

}
[/code]

CastRays goes throught every screen pixel and calculate ray from pixel position and camera coordinates.
[code]
foreach pixel
{

Vector3 vec = Map2DTo3D(x, y, width, height, &cameraCoordSystem);
Vector3 dir = vec - cameraPos;

Ray ray;
ray.dir = dir;
ray.origin = cameraPos;

ray.dir = TransformCoordinate(ray.dir, worldInv);
ray.origin = TransformCoordinate(ray.origin, worldInv);
ray.dir = Normalize(ray.dir);

int value = TraverseRay(ray);
.....
}
[/code]

Traverse ray uses "An Efficient Parametric Algorithm for Octree Traversal" algorithm
[code]
int TraverseRay(ray)
{
if (ray.dir.x < 0.0f)
{
ray.origin.x = boxSize - ray.origin.x;
ray.dir.x *= -1;
}
if (ray.dir.y < 0.0f)
{
ray.origin.y = boxSize - ray.origin.y;
ray.dir.y *= -1;
}
if (ray.dir.z < 0.0f)
{
ray.origin.z = boxSize - ray.origin.z;
ray.dir.z *= -1;
}


float tx0 = (0 - ray.origin.x) / ray.dir.x;
float tx1 = (boxSize - ray.origin.x) / ray.dir.x;

float ty0 = (0 - ray.origin.y) / ray.dir.y;
float ty1 = (boxSize - ray.origin.y) / ray.dir.y;

float tz0 = 0 - ray.origin.z) / ray.dir.z;
float tz1 = (boxSize - ray.origin.z) / ray.dir.z;

//infinity test - not included... have no effect on bug

if (Max(tx0, ty0, tz0) < Min(tx1, ty1, tz1))
{
return 1;
//there is procsubtree().. but again.. no effect on error... error occurs
//even with return 1
}
else
{
return -1;
}
}
[/code]

Bug look. The space between boxes is changing its size in respect of camera. Correctly, there should be no space between boxes, it should be one single square (if i rotate this, there are other 4 boxes from octree behind those)
[img]http://www.perry.cz/files/boxbug.jpg[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this