diff --git a/src/editor/level_editor.jai b/src/editor/level_editor.jai index bbf626a..2b7f5f6 100644 --- a/src/editor/level_editor.jai +++ b/src/editor/level_editor.jai @@ -498,6 +498,7 @@ add_trile :: (name: string, x: s32, y: s32, z: s32, orientation: u8 = 0) { array_add(*chunk.groups, group); } invalidate_buried_around(*curworld.world, x, y, z); + chunk_set_occupancy_mask_at(chunk, lx, ly, lz, true); } @Command remove_trile :: (x: s32, y: s32, z: s32) { @@ -524,6 +525,7 @@ remove_trile :: (x: s32, y: s32, z: s32) { if removed break; } if removed invalidate_buried_around(*curworld.world, x, y, z); + chunk_set_occupancy_mask_at(chunk, lx, ly, lz, false); } @Command diff --git a/src/pseudophysics/worldphysics.jai b/src/pseudophysics/worldphysics.jai index 65171df..bf101db 100644 --- a/src/pseudophysics/worldphysics.jai +++ b/src/pseudophysics/worldphysics.jai @@ -1,15 +1,12 @@ is_trile_collision :: (fx: float, fy: float, fz: float) -> bool { - x := cast(s32)fx; - y := cast(s32)fy; - z := cast(s32)fz; + x := cast(s32)floor(fx); + y := cast(s32)floor(fy); + z := cast(s32)floor(fz); world := get_current_world(); key := world_to_chunk_coord(x, y, z); chunk := table_find_pointer(*world.world.chunks, key); if !chunk then return false; lx, ly, lz := world_to_local(x, y, z); - - print("% % %\n", lx, ly, lz); - - return chunk.occupancy_mask[lx][ly][lz]; + return chunk_get_occupancy_mask_at(chunk, lx, ly, lz); } diff --git a/src/world.jai b/src/world.jai index 825cb6c..dc8c83e 100644 --- a/src/world.jai +++ b/src/world.jai @@ -59,13 +59,38 @@ Chunk :: struct { coord: Chunk_Key; groups: [..]Chunk_Trile_Group; - occupancy_mask : [32][32][32]bool; + // This goes x,z,y so y is the bitmask because + // that is useful when we're checking for ground under + // entities + occupancy_mask : [32][32]u32; sh_probe_grid: sg_image; // 192x4096 RGBA16F 2D texture (2 probes/trile/axis) sh_valid: bool; sh_dirty: bool; } +// @Note: these are local coords inside the chunk +chunk_get_occupancy_mask_at :: inline (chunk: *Chunk, lx: u32, ly: u32, lz: u32) -> bool { + return (chunk.occupancy_mask[lx][lz] & (cast(u32)1 << ly)) != 0; +} + +// @Note: these are local coords inside the chunk +chunk_set_occupancy_mask_at :: inline (chunk: *Chunk, lx: u32, ly: u32, lz: u32, occupied: bool) { + if occupied { + chunk.occupancy_mask[lx][lz] |= (cast(u32)1 << ly); + } else { + chunk.occupancy_mask[lx][lz] &= ~(cast(u32)1 << ly); + } +} + +chunk_clear_full_occupancy_mask :: (chunk: *Chunk) { + for i: 0..31 { + for j: 0..31 { + chunk.occupancy_mask[cast(u32)i][cast(u32)j] = 0; + } + } +} + Editor_Note :: struct { position : Chunk_Key; text : string; @@ -100,21 +125,14 @@ World :: struct { } calculate_and_set_chunk_occupancy_mask :: (chunk: *Chunk) { - print("Calculating occupancy mask for chunk %\n", chunk.coord); - // Clear it first: - for i: 0..15 { - for j: 0..15 { - for k: 0..15 { - chunk.occupancy_mask[i][j][k] = false; - } - } - } + chunk_clear_full_occupancy_mask(chunk); for group: chunk.groups { // @ToDo: Handle non-colliding triles by getting the // trile data here and continuing if it's non-colliding. for instance : group.instances { - chunk.occupancy_mask[instance.x][instance.y][instance.z] = true; + + chunk_set_occupancy_mask_at(chunk, instance.x, instance.y, instance.z, true); } } }