voxel-framework 0.12.1

A reusable voxel engine framework built on Bevy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! DDA (Digital Differential Analysis) voxel raycasting.
//!
//! # Algorithm
//!
//! This module implements the classic 3-D DDA traversal introduced by Amanatides
//! and Woo (1987).  Starting from `origin`, the algorithm maintains three
//! "time-to-next-boundary" values (`t_max_x/y/z`).  At each iteration it
//! advances across the voxel boundary that is reached soonest, records which
//! axis was crossed (to reconstruct the face normal), and samples the new voxel.
//! Because only one boundary is crossed per step, every solid voxel on the ray
//! is guaranteed to be visited in order.
//!
//! # Coordinate space
//!
//! All positions are in **world space**, where each voxel occupies a
//! `VOXEL_SIZE`-unit cube.  Integer grid coordinates correspond to voxel
//! minimum corners; the center of voxel `(vx, vy, vz)` is at
//! `(vx + 0.5, vy + 0.5, vz + 0.5)`.
//!
//! # Integration
//!
//! Call [`raycast`] from any Bevy system that has read access to
//! [`crate::world::ChunkManager`] and the chunk query.  The returned
//! [`RaycastHit`] gives you the chunk coordinate, local voxel index, world
//! position, face normal, and travel distance — enough to drive both mining and
//! block-placement logic.
//!
//! ```no_run
//! # use bevy::prelude::*;
//! # use voxel_framework::world::{ChunkManager, raycast::raycast};
//! # use voxel_framework::voxel::DefaultVoxel;
//! # use voxel_framework::chunk::Chunk;
//! # fn example(
//! #     manager: &ChunkManager,
//! #     chunks: &Query<&Chunk<DefaultVoxel>>,
//! # ) {
//! let origin    = Vec3::new(0.0, 64.0, 0.0);
//! let direction = Vec3::new(0.0, -1.0, 0.0); // straight down
//! if let Some(hit) = raycast(origin, direction, 8.0, manager, chunks) {
//!     println!("hit voxel {:?} at distance {:.2}", hit.local, hit.distance);
//! }
//! # }
//! ```

use bevy::prelude::*;

use crate::chunk::{CHUNK_SIZE, Chunk, ChunkCoord, VOXEL_SIZE, world_to_chunk_coord};
use crate::voxel::VoxelType;

use super::ChunkManager;

/// Describes the first solid voxel intersected by a ray.
///
/// Returned by [`raycast`] when the ray hits a solid voxel before
/// `max_distance` is exceeded.
///
/// # Examples
///
/// ```no_run
/// # use bevy::prelude::*;
/// # use voxel_framework::world::{ChunkManager, raycast::raycast};
/// # use voxel_framework::voxel::DefaultVoxel;
/// # use voxel_framework::chunk::Chunk;
/// # fn example(manager: &ChunkManager, chunks: &Query<&Chunk<DefaultVoxel>>) {
/// if let Some(hit) = raycast(Vec3::ZERO, Vec3::X, 16.0, manager, chunks) {
///     // Place a block one step back along the face normal.
///     let place_pos = hit.position + hit.face_normal;
///     println!("chunk {:?}, local {:?}", hit.chunk_coord, hit.local);
/// }
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct RaycastHit {
    /// World-space center of the hit voxel, i.e. `(vx + 0.5, vy + 0.5, vz + 0.5)`.
    pub position: Vec3,
    /// Integer chunk coordinate (in chunk-grid units) that contains the hit voxel.
    ///
    /// Multiply by `CHUNK_SIZE` to get the chunk's world-space origin.
    pub chunk_coord: ChunkCoord,
    /// Local `(x, y, z)` index of the hit voxel within its chunk.
    ///
    /// Each component is in `[0, CHUNK_SIZE)`.
    pub local: (usize, usize, usize),
    /// Outward unit normal of the voxel face that was struck.
    ///
    /// One axis component is `±1.0`, the other two are `0.0`.
    /// For a voxel entered from its origin (the ray starts inside), this is
    /// `Vec3::ZERO`.
    pub face_normal: Vec3,
    /// Travel distance along the ray from `origin` to the hit voxel boundary, in world units.
    ///
    /// `0.0` when the ray origin is already inside a solid voxel.
    pub distance: f32,
}

/// Cast a ray through the voxel world and return the first solid voxel hit.
///
/// Uses a 3-D DDA traversal so every voxel on the ray path is visited in
/// order without skipping any.
///
/// # Parameters
///
/// - `origin` — World-space ray start position.
/// - `direction` — Ray direction (need not be normalised; the function
///   normalises it internally).
/// - `max_distance` — Maximum travel distance in world units.  The traversal
///   stops as soon as the current `t` value exceeds this limit.
/// - `manager` — The chunk registry used to resolve world coordinates to
///   chunk entities.
/// - `chunks` — ECS query providing read access to chunk data.
///
/// # Return value
///
/// Returns `Some(RaycastHit)` for the **first** solid voxel the ray enters.
/// Returns `None` when the ray travels `max_distance` without striking a
/// solid voxel, or if all traversed chunks are unloaded.
///
/// # Examples
///
/// ```no_run
/// # use bevy::prelude::*;
/// # use voxel_framework::world::{ChunkManager, raycast::raycast};
/// # use voxel_framework::voxel::DefaultVoxel;
/// # use voxel_framework::chunk::Chunk;
/// # fn example(manager: &ChunkManager, chunks: &Query<&Chunk<DefaultVoxel>>) {
/// let origin    = Vec3::new(8.0, 70.0, 8.0);
/// let direction = Vec3::new(1.0, -0.2, 0.0);
/// match raycast(origin, direction, 10.0, manager, chunks) {
///     Some(hit) => println!("hit at distance {:.2}, face {:?}", hit.distance, hit.face_normal),
///     None      => println!("no solid voxel in range"),
/// }
/// # }
/// ```
pub fn raycast<V: VoxelType>(
    origin: Vec3,
    direction: Vec3,
    max_distance: f32,
    manager: &ChunkManager,
    chunks: &Query<&Chunk<V>>,
) -> Option<RaycastHit> {
    crate::profile_span!("voxel::world::raycast");
    let dir = direction.normalize();

    // Current voxel position (integer grid)
    let mut vx = origin.x.floor() as i32;
    let mut vy = origin.y.floor() as i32;
    let mut vz = origin.z.floor() as i32;

    // Step direction: +1 or -1 per axis
    let step_x = if dir.x >= 0.0 { 1i32 } else { -1i32 };
    let step_y = if dir.y >= 0.0 { 1i32 } else { -1i32 };
    let step_z = if dir.z >= 0.0 { 1i32 } else { -1i32 };

    // tDelta: how far along the ray (in t units) to cross one voxel on each axis
    let delta_x = if dir.x.abs() > f32::EPSILON {
        VOXEL_SIZE / dir.x.abs()
    } else {
        f32::MAX
    };
    let delta_y = if dir.y.abs() > f32::EPSILON {
        VOXEL_SIZE / dir.y.abs()
    } else {
        f32::MAX
    };
    let delta_z = if dir.z.abs() > f32::EPSILON {
        VOXEL_SIZE / dir.z.abs()
    } else {
        f32::MAX
    };

    // tMax: distance along ray to the next voxel boundary on each axis
    let next_x = if step_x > 0 {
        vx as f32 + 1.0
    } else {
        vx as f32
    };
    let next_y = if step_y > 0 {
        vy as f32 + 1.0
    } else {
        vy as f32
    };
    let next_z = if step_z > 0 {
        vz as f32 + 1.0
    } else {
        vz as f32
    };

    let mut t_max_x = if dir.x.abs() > f32::EPSILON {
        (next_x - origin.x) / dir.x
    } else {
        f32::MAX
    };
    let mut t_max_y = if dir.y.abs() > f32::EPSILON {
        (next_y - origin.y) / dir.y
    } else {
        f32::MAX
    };
    let mut t_max_z = if dir.z.abs() > f32::EPSILON {
        (next_z - origin.z) / dir.z
    } else {
        f32::MAX
    };

    // Check the initial voxel before any stepping
    {
        let world_pos = Vec3::new(vx as f32 + 0.5, vy as f32 + 0.5, vz as f32 + 0.5);
        let coord = world_to_chunk_coord(world_pos);
        if let Some(entity) = manager.get_entity(coord)
            && let Ok(chunk) = chunks.get(entity)
        {
            let lx = (vx.rem_euclid(CHUNK_SIZE as i32)) as usize;
            let ly = (vy.rem_euclid(CHUNK_SIZE as i32)) as usize;
            let lz = (vz.rem_euclid(CHUNK_SIZE as i32)) as usize;
            if chunk.is_solid_at(lx, ly, lz) {
                return Some(RaycastHit {
                    position: world_pos,
                    chunk_coord: coord,
                    local: (lx, ly, lz),
                    face_normal: Vec3::ZERO,
                    distance: 0.0,
                });
            }
        }
    }

    loop {
        // Advance to next voxel boundary — pick the closest axis and record which one.
        let (t, last_step_axis): (f32, u8);
        if t_max_x < t_max_y && t_max_x < t_max_z {
            t = t_max_x;
            if t > max_distance {
                break;
            }
            vx += step_x;
            t_max_x += delta_x;
            last_step_axis = 0;
        } else if t_max_y < t_max_z {
            t = t_max_y;
            if t > max_distance {
                break;
            }
            vy += step_y;
            t_max_y += delta_y;
            last_step_axis = 1;
        } else {
            t = t_max_z;
            if t > max_distance {
                break;
            }
            vz += step_z;
            t_max_z += delta_z;
            last_step_axis = 2;
        }

        let world_pos = Vec3::new(vx as f32 + 0.5, vy as f32 + 0.5, vz as f32 + 0.5);
        let coord = world_to_chunk_coord(world_pos);

        let entity = match manager.get_entity(coord) {
            Some(e) => e,
            None => continue,
        };
        let chunk = match chunks.get(entity) {
            Ok(c) => c,
            Err(_) => continue,
        };

        let lx = (vx.rem_euclid(CHUNK_SIZE as i32)) as usize;
        let ly = (vy.rem_euclid(CHUNK_SIZE as i32)) as usize;
        let lz = (vz.rem_euclid(CHUNK_SIZE as i32)) as usize;

        if chunk.is_solid_at(lx, ly, lz) {
            // Face normal: opposite to the step direction on the last-crossed axis
            let face_normal = match last_step_axis {
                0 => Vec3::new(-(step_x as f32), 0.0, 0.0),
                1 => Vec3::new(0.0, -(step_y as f32), 0.0),
                _ => Vec3::new(0.0, 0.0, -(step_z as f32)),
            };

            return Some(RaycastHit {
                position: world_pos,
                chunk_coord: coord,
                local: (lx, ly, lz),
                face_normal,
                distance: t,
            });
        }
    }

    None
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::chunk::Chunk;
    use crate::voxel::DefaultVoxel;
    use crate::world::ChunkManager;

    /// Build a minimal Bevy World, spawn a chunk entity, register it in a
    /// ChunkManager, and return (world, manager).
    fn setup_world_with_chunk(fill: impl Fn(&mut Chunk<DefaultVoxel>)) -> (World, ChunkManager) {
        let mut world = World::new();
        let mut chunk = Chunk::<DefaultVoxel>::new();
        fill(&mut chunk);
        let entity = world.spawn(chunk).id();

        let mut manager = ChunkManager::default();
        // Chunk at coord (0,0,0) spans world positions [0, 16)^3
        manager.register(IVec3::ZERO, entity);

        (world, manager)
    }

    #[test]
    fn test_raycast_hits_solid_voxel() {
        // Place a single solid voxel at local (8,8,8) → world center ~(8.5, 8.5, 8.5)
        let (mut world, manager) = setup_world_with_chunk(|chunk| {
            chunk.set(8, 8, 8, DefaultVoxel::Rock);
        });

        // Ray starts far on +X axis, aimed toward the solid voxel
        let origin = Vec3::new(-5.0, 8.5, 8.5);
        let direction = Vec3::new(1.0, 0.0, 0.0);

        let mut system_state =
            bevy::ecs::system::SystemState::<Query<&Chunk<DefaultVoxel>>>::new(&mut world);
        let chunks = system_state.get(&world);

        let hit = raycast(origin, direction, 50.0, &manager, &chunks);
        assert!(hit.is_some(), "expected a hit but got None");
        let hit = hit.unwrap();
        assert_eq!(hit.local, (8, 8, 8));
        assert_eq!(hit.chunk_coord, IVec3::ZERO);
    }

    #[test]
    fn test_raycast_misses_empty_chunk() {
        // All-air chunk
        let (mut world, manager) = setup_world_with_chunk(|_| {});

        let origin = Vec3::new(-5.0, 8.5, 8.5);
        let direction = Vec3::new(1.0, 0.0, 0.0);

        let mut system_state =
            bevy::ecs::system::SystemState::<Query<&Chunk<DefaultVoxel>>>::new(&mut world);
        let chunks = system_state.get(&world);

        let hit = raycast(origin, direction, 50.0, &manager, &chunks);
        assert!(hit.is_none(), "expected None but got a hit");
    }

    #[test]
    fn test_raycast_correct_face_normal() {
        // Solid voxel at local (4,8,8) → world ~(4.5, 8.5, 8.5)
        // Ray coming from +X (right side), so the hit face normal should point +X
        let (mut world, manager) = setup_world_with_chunk(|chunk| {
            chunk.set(4, 8, 8, DefaultVoxel::Rock);
        });

        // Approach from the right: origin at x=20, shoot in -X direction
        let origin = Vec3::new(20.0, 8.5, 8.5);
        let direction = Vec3::new(-1.0, 0.0, 0.0);

        let mut system_state =
            bevy::ecs::system::SystemState::<Query<&Chunk<DefaultVoxel>>>::new(&mut world);
        let chunks = system_state.get(&world);

        let hit = raycast(origin, direction, 50.0, &manager, &chunks);
        assert!(hit.is_some(), "expected a hit but got None");
        let hit = hit.unwrap();
        assert_eq!(hit.local, (4, 8, 8));
        // Approaching from +X with step=-1 → face_normal = -(-1) = +1 on X
        assert_eq!(hit.face_normal, Vec3::new(1.0, 0.0, 0.0));
    }

    // ── Multi-chunk cases ─────────────────────────────────────────────────────

    use crate::test_support::world_with_chunks;

    fn chunk_with(voxels: &[(usize, usize, usize)]) -> Chunk<DefaultVoxel> {
        let mut chunk = Chunk::new();
        for &(x, y, z) in voxels {
            chunk.set(x, y, z, DefaultVoxel::Rock);
        }
        chunk
    }

    fn cast(world: &mut World, origin: Vec3, dir: Vec3, max: f32) -> Option<RaycastHit> {
        let mut state = bevy::ecs::system::SystemState::<(
            Res<ChunkManager>,
            Query<&Chunk<DefaultVoxel>>,
        )>::new(world);
        let (manager, chunks) = state.get(world);
        raycast(origin, dir, max, &manager, &chunks)
    }

    #[test]
    fn ray_crosses_two_empty_chunks_before_hitting_the_third() {
        let mut world = world_with_chunks([
            (IVec3::ZERO, Chunk::new()),
            (IVec3::new(1, 0, 0), Chunk::new()),
            (IVec3::new(2, 0, 0), chunk_with(&[(3, 8, 8)])),
        ]);

        let hit = cast(&mut world, Vec3::new(0.5, 8.5, 8.5), Vec3::X, 100.0)
            .expect("ray should reach the third chunk");

        assert_eq!(hit.chunk_coord, IVec3::new(2, 0, 0));
        assert_eq!(hit.local, (3, 8, 8));
        assert_eq!(hit.face_normal, Vec3::NEG_X);
        // Enters voxel x = 35 at its min face: 35.0 - 0.5.
        assert!(
            (hit.distance - 34.5).abs() < 1e-4,
            "distance {}",
            hit.distance
        );
    }

    #[test]
    fn ray_in_negative_direction_hits_lower_chunk_last_cell() {
        let mut world = world_with_chunks([
            (IVec3::new(-1, 0, 0), chunk_with(&[(15, 4, 4)])),
            (IVec3::ZERO, Chunk::new()),
        ]);

        let hit = cast(&mut world, Vec3::new(5.5, 4.5, 4.5), Vec3::NEG_X, 20.0)
            .expect("ray should hit voxel at world x = -1");

        assert_eq!(hit.chunk_coord, IVec3::new(-1, 0, 0));
        assert_eq!(hit.local, (15, 4, 4));
        assert_eq!(hit.position, Vec3::new(-0.5, 4.5, 4.5));
        assert_eq!(hit.face_normal, Vec3::X);
    }

    #[test]
    fn ray_skips_unloaded_gap_and_hits_beyond_it() {
        let mut world = world_with_chunks([
            (IVec3::ZERO, Chunk::new()),
            // chunk (1, 0, 0) intentionally not loaded
            (IVec3::new(2, 0, 0), chunk_with(&[(0, 1, 1)])),
        ]);

        let hit = cast(&mut world, Vec3::new(1.5, 1.5, 1.5), Vec3::X, 64.0)
            .expect("unloaded chunks are treated as empty");
        assert_eq!(hit.chunk_coord, IVec3::new(2, 0, 0));
    }

    #[test]
    fn hit_beyond_max_distance_is_ignored() {
        let mut world = world_with_chunks([(IVec3::ZERO, chunk_with(&[(10, 0, 0)]))]);

        assert!(cast(&mut world, Vec3::new(0.5, 0.5, 0.5), Vec3::X, 5.0).is_none());
        assert!(cast(&mut world, Vec3::new(0.5, 0.5, 0.5), Vec3::X, 10.0).is_some());
    }

    #[test]
    fn origin_inside_solid_hits_immediately() {
        let mut world = world_with_chunks([(IVec3::ZERO, chunk_with(&[(2, 2, 2)]))]);

        let hit = cast(&mut world, Vec3::new(2.5, 2.5, 2.5), Vec3::Y, 10.0).unwrap();
        assert_eq!(hit.local, (2, 2, 2));
        assert_eq!(hit.distance, 0.0);
        assert_eq!(hit.face_normal, Vec3::ZERO);
    }

    #[test]
    fn diagonal_ray_across_a_chunk_corner_hits_target() {
        let mut world = world_with_chunks([
            (IVec3::ZERO, Chunk::new()),
            (IVec3::new(1, 0, 0), Chunk::new()),
            (IVec3::new(0, 0, 1), Chunk::new()),
            (IVec3::new(1, 0, 1), chunk_with(&[(1, 5, 1)])),
        ]);

        let hit = cast(
            &mut world,
            Vec3::new(14.5, 5.5, 14.5),
            Vec3::new(1.0, 0.0, 1.0),
            10.0,
        )
        .expect("diagonal ray should hit across the corner");
        assert_eq!(hit.chunk_coord, IVec3::new(1, 0, 1));
        assert_eq!(hit.local, (1, 5, 1));
    }
}