Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem with my raycasting. I have 8 cubes, that define the first level of an octree (Eg. they create bigger cube).

I need to raycast them and "render" each cube. If I have only one cube, the raycasting works fine. If I use 8 cubes, I have a problem. If I move my camera, gaps between cubes start to appear. Cubes are moving apart.

The 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++)

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

CastRays goes through 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;

The space between boxes is changing its size with respect to the camera. 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|improve this question

I cannot sure how your math library is working exactly, but a function named TransformCoordinate is usually used to transform a position vector including translation. A directional vector should use another transform usually named TransformNormal.

In the CastRays function, you used TransformCoordinate to transform a directional vector, ray.dir. It might cause wrong results.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.