Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(3 edits)

It is also strange that you say you think it is the ray origins and diretions that are not calculated correctly, (although you could very possibly be right, I wouldn't doubt it) But it is strange, because if I do a basic ray box intersection using the same ray directions and origins there is no warping at all. The warping exists entirely within the DDA algorithm I have. Here it is:


fn Sphere_DDA_March(
    start_pos: vec3<f32>,  // Starting position of the ray in world space
    end_pos: vec3<f32>,    // Ending position of the ray in world space
    voxel_size: vec3<f32>,       // Size of each voxel in the voxel grid
    bounds: vec3<i32>,     // Dimensions of the voxel grid (width, height, depth)
) -> vec4<f32> {
    var direction: vec3<f32> = normalize(vec3<f32>(end_pos - start_pos));
    var step: vec3<i32> = vec3<i32>(
    select(-1, 1, direction.x > 0.0),
    select(-1, 1, direction.y > 0.0),
    select(-1, 1, direction.z > 0.0)
    );
    var tMax: vec3<f32> = vec3<f32>(
        ((select(floor(start_pos.x / voxel_size.x) + 1.0, floor(start_pos.x / voxel_size.x), direction.x < 0.0) * voxel_size.x) - start_pos.x) / direction.x,
        ((select(floor(start_pos.y / voxel_size.y) + 1.0, floor(start_pos.y / voxel_size.y), direction.y < 0.0) * voxel_size.y) - start_pos.y) / direction.y,
        ((select(floor(start_pos.z / voxel_size.z) + 1.0, floor(start_pos.z / voxel_size.z), direction.z < 0.0) * voxel_size.z) - start_pos.z) / direction.z
    );
    var tDelta: vec3<f32> = vec3<f32>(
        abs(voxel_size.x / direction.x),
        abs(voxel_size.y / direction.y),
        abs(voxel_size.z / direction.z)
    );
    let center: vec3<f32> = vec3<f32>(15.0);
    var ray_world_coord = vec3<f32>(vec3<f32>(current_voxel) * voxel_size);
    var ray_dist: f32 = f32(sqrt(((ray_world_coord.x - camera.position.x) * (ray_world_coord.x - camera.position.x)) + ((ray_world_coord.y - camera.position.y) * (ray_world_coord.y - camera.position.y)) + ((ray_world_coord.z - camera.position.z) * (ray_world_coord.z - camera.position.z))));
    while(ray_dist < 256.0){
        ray_world_coord = vec3<f32>(vec3<f32>(current_voxel) * voxel_size);
        var world_cord = vec3<f32>(vec3<f32>(current_voxel) * voxel_size);
        ray_dist = f32(sqrt(((ray_world_coord.x - camera.position.x) * (ray_world_coord.x - camera.position.x)) + ((ray_world_coord.y - camera.position.y) * (ray_world_coord.y - camera.position.y)) + ((ray_world_coord.z - camera.position.z) * (ray_world_coord.z - camera.position.z))));
        var dist: f32 = f32(sqrt(((world_cord.x - center.x) * (world_cord.x - center.x)) + ((world_cord.y - center.y) * (world_cord.y - center.y)) + ((world_cord.z - center.z) * (world_cord.z - center.z))));
        if (dist < 60.0){
            let start_light: vec3<f32> = vec3<f32>(world_cord.x, world_cord.y + -0.5, world_cord.z);
            let end_light: vec3<f32> = vec3<f32>(world_cord.x, world_cord.y + 10.0, world_cord.z);
            let output = DDA_Light(start_light, end_light, voxel_size, bounds);
            if (output == 0) {
                return vec4<f32>(1.0, ray_dist / 100.0, 0.0, 1.0);
            } else {
                return vec4<f32>(0.75, ray_dist / 100.0, 0.0, 1.0);
            }
            
        }
        if (tMax.x < tMax.y && tMax.x < tMax.z) {
            current_voxel.x += step.x;
            tMax.x += tDelta.x;
        } else if (tMax.y < tMax.z) {
            current_voxel.y += step.y;
            tMax.y += tDelta.y;
        } else {
            current_voxel.z += step.z;
            tMax.z += tDelta.z;
        }
    }
    return vec4<f32>(0.0);
}

The code is too complicated for me to easily see where the calculations are incorrect.  Perhaps a test case in 2D would be easier to (visually) debug.