fix dda t_hit offset

This commit is contained in:
jacob 2026-02-13 03:12:08 -06:00
parent f16a102cfa
commit 5332e1bdd9
3 changed files with 32 additions and 36 deletions

View File

@ -1472,36 +1472,36 @@ void P_UniqueSpaceEntriesFromRay(Arena *arena, P_SpaceEntryList *result, i32 spa
ray_p0 = AddVec2(ray_p0, VEC2(P_WorldPitch / 2.0, P_WorldPitch / 2.0)); ray_p0 = AddVec2(ray_p0, VEC2(P_WorldPitch / 2.0, P_WorldPitch / 2.0));
ray_p1 = AddVec2(ray_p1, VEC2(P_WorldPitch / 2.0, P_WorldPitch / 2.0)); ray_p1 = AddVec2(ray_p1, VEC2(P_WorldPitch / 2.0, P_WorldPitch / 2.0));
Vec2I32 grid_start = Vec2I32FromVec(FloorVec2(ray_p0)); Vec2I32 dda_start = Vec2I32FromVec(FloorVec2(ray_p0));
Vec2I32 grid_end = Vec2I32FromVec(FloorVec2(ray_p1)); Vec2I32 dda_end = Vec2I32FromVec(FloorVec2(ray_p1));
Vec2 delta = SubVec2(ray_p1, ray_p0); Vec2 delta = SubVec2(ray_p1, ray_p0);
Vec2 inv_delta = RecipVec2(delta); Vec2 inv_delta = RecipVec2(delta);
Vec2 step_dir = VEC2((delta.x > 0) - (delta.x < 0), (delta.y > 0) - (delta.y < 0)); Vec2 step_dir = VEC2((delta.x > 0) - (delta.x < 0), (delta.y > 0) - (delta.y < 0));
Vec2 t_delta = MulVec2Vec2(step_dir, inv_delta); Vec2 t_delta = MulVec2Vec2(step_dir, inv_delta);
Vec2 t_max = SubVec2(Vec2FromVec(grid_start), ray_p0); Vec2 t_max = SubVec2(Vec2FromVec(dda_start), ray_p0);
t_max.x += step_dir.x > 0; t_max.x += step_dir.x > 0;
t_max.y += step_dir.y > 0; t_max.y += step_dir.y > 0;
t_max = MulVec2Vec2(t_max, inv_delta); t_max = MulVec2Vec2(t_max, inv_delta);
Vec2I32 grid_pos = grid_start; Vec2I32 dda_pos = dda_start;
b32 done = 0; b32 done = 0;
while (!done) while (!done)
{ {
if (grid_pos.x >= 0 && grid_pos.y >= 0 && grid_pos.x < P_WorldPitch && grid_pos.y < P_WorldPitch) if (dda_pos.x >= 0 && dda_pos.y >= 0 && dda_pos.x < P_WorldPitch && dda_pos.y < P_WorldPitch)
{ {
if (P_tl.debug_draw_enabled) if (P_tl.debug_draw_enabled)
{ {
Vec2 world_pos = Vec2FromVec(grid_pos); Vec2 world_pos = Vec2FromVec(dda_pos);
world_pos = SubVec2(world_pos, VEC2(P_WorldPitch / 2.0, P_WorldPitch / 2.0)); world_pos = SubVec2(world_pos, VEC2(P_WorldPitch / 2.0, P_WorldPitch / 2.0));
P_DebugDrawRect(RNG2(world_pos, AddVec2(world_pos, VEC2(1, 1))), VEC4(0.85, 0.5, 0.75, 0.75)); P_DebugDrawRect(RNG2(world_pos, AddVec2(world_pos, VEC2(1, 1))), VEC4(0.85, 0.5, 0.75, 0.75));
} }
for (i32 space_idx = 0; space_idx < spaces_count; ++space_idx) for (i32 space_idx = 0; space_idx < spaces_count; ++space_idx)
{ {
P_Space *space = spaces[space_idx]; P_Space *space = spaces[space_idx];
if (grid_pos.x < space->dims.x && grid_pos.y < space->dims.y) if (dda_pos.x < space->dims.x && dda_pos.y < space->dims.y)
{ {
i64 cell_idx = grid_pos.y * P_WorldPitch + grid_pos.x; i64 cell_idx = dda_pos.y * P_WorldPitch + dda_pos.x;
P_SpaceCell cell = space->cells[cell_idx]; P_SpaceCell cell = space->cells[cell_idx];
for ( for (
P_SpaceEntryNode *src_space_entry_node = cell.first; P_SpaceEntryNode *src_space_entry_node = cell.first;
@ -1538,18 +1538,18 @@ void P_UniqueSpaceEntriesFromRay(Arena *arena, P_SpaceEntryList *result, i32 spa
} }
} }
} }
if (grid_pos.x == grid_end.x && grid_pos.y == grid_end.y) if (dda_pos.x == dda_end.x && dda_pos.y == dda_end.y)
{ {
done = 1; done = 1;
} }
else if (t_max.x < t_max.y) else if (t_max.x < t_max.y)
{ {
grid_pos.x += step_dir.x; dda_pos.x += step_dir.x;
t_max.x += t_delta.x; t_max.x += t_delta.x;
} }
else else
{ {
grid_pos.y += step_dir.y; dda_pos.y += step_dir.y;
t_max.y += t_delta.y; t_max.y += t_delta.y;
} }
} }

View File

@ -2503,7 +2503,6 @@ void V_TickForever(WaveLaneCtx *lane)
////////////////////////////// //////////////////////////////
//- Push test emitter //- Push test emitter
// if (frame->held_buttons[Button_G])
if (frame->held_buttons[Button_G] && !prev_frame->held_buttons[Button_G]) if (frame->held_buttons[Button_G] && !prev_frame->held_buttons[Button_G])
{ {
V_Emitter emitter = Zi; V_Emitter emitter = Zi;

View File

@ -210,57 +210,54 @@ ComputeShader(V_SimParticlesCS, 64)
{ {
Vec2 tile_p0 = mul(frame.af.world_to_tile, Vec3(p0, 1)); Vec2 tile_p0 = mul(frame.af.world_to_tile, Vec3(p0, 1));
Vec2 tile_p1 = mul(frame.af.world_to_tile, Vec3(p1, 1)); Vec2 tile_p1 = mul(frame.af.world_to_tile, Vec3(p1, 1));
Vec2I32 grid_p0 = floor(tile_p0); Vec2I32 dda_p0 = floor(tile_p0);
Vec2I32 grid_p1 = floor(tile_p1); Vec2I32 dda_p1 = floor(tile_p1);
Vec2 delta = tile_p1 - tile_p0; Vec2 delta = tile_p1 - tile_p0;
Vec2 inv_delta = 1.0 / delta; Vec2 inv_delta = 1.0 / delta;
Vec2 grid_step_dir = Vec2((delta.x > 0) - (delta.x < 0), (delta.y > 0) - (delta.y < 0)); Vec2 dda_step_dir = Vec2((delta.x > 0) - (delta.x < 0), (delta.y > 0) - (delta.y < 0));
Vec2 t_delta = abs(inv_delta); Vec2 t_delta = abs(inv_delta);
Vec2 t_crossover_next = grid_p0 - tile_p0; Vec2 t_max = dda_p0 - tile_p0;
t_crossover_next.x += grid_step_dir.x > 0; t_max.x += dda_step_dir.x > 0;
t_crossover_next.y += grid_step_dir.y > 0; t_max.y += dda_step_dir.y > 0;
t_crossover_next *= inv_delta; t_max *= inv_delta;
t_crossover_next = abs(t_crossover_next); t_max = abs(t_max);
f32 t_hit = 0; f32 t_hit = 1;
Vec2I32 grid_pos = grid_p0; Vec2I32 dda_pos = dda_p0;
b32 stepped_x = 0; b32 stepped_x = 0;
b32 stepped_y = 0; b32 stepped_y = 0;
// TODO: Tune this
u32 max_iterations = 32; u32 max_iterations = 32;
u32 iteration_idx = 0;
b32 done = 0; b32 done = 0;
while (!done && iteration_idx < max_iterations) for (u32 iteration_idx = 0; iteration_idx < max_iterations && !done; ++iteration_idx)
{ {
if (grid_pos.x == grid_p1.x && grid_pos.y == grid_p1.y) if (dda_pos.x == dda_p1.x && dda_pos.y == dda_p1.y)
{ {
done = 1; done = 1;
} }
else if (t_crossover_next.x < t_crossover_next.y) else if (t_max.x < t_max.y)
{ {
grid_pos.x += grid_step_dir.x; dda_pos.x += dda_step_dir.x;
t_hit = t_crossover_next.x - t_delta.x; t_hit = t_max.x - t_delta.x * 0.01;
t_crossover_next.x += t_delta.x; t_max.x += t_delta.x;
stepped_x = 1; stepped_x = 1;
stepped_y = 0; stepped_y = 0;
} }
else else
{ {
grid_pos.y += grid_step_dir.y; dda_pos.y += dda_step_dir.y;
t_hit = t_crossover_next.y - t_delta.y; t_hit = t_max.y - t_delta.y * 0.01;
t_crossover_next.y += t_delta.y; t_max.y += t_delta.y;
stepped_x = 0; stepped_x = 0;
stepped_y = 1; stepped_y = 1;
} }
if (all(grid_pos >= 0) && all(grid_pos < countof(tiles))) if (all(dda_pos >= 0) && all(dda_pos < countof(tiles)))
{ {
P_TileKind tile = tiles[grid_pos]; P_TileKind tile = tiles[dda_pos];
if (tile == P_TileKind_Wall) if (tile == P_TileKind_Wall)
{ {
done = 1; done = 1;