Jump to content
  • Advertisement
Sign in to follow this  

RayCasting problem

This topic is 2956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

for (int i = 0; i < 8; i++)
//(AB)-1 = B-1 * A-1
MyMath::Vector3 centerVec = this->streams.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);


CastRays goes throught every screen pixel and calculate ray from pixel position and camera coordinates.

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);

Traverse ray uses "An Efficient Parametric Algorithm for Octree Traversal" algorithm

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
return -1;

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)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!