Record relative hit position

This commit is contained in:
numzero 2024-06-10 22:22:50 +03:00
parent 5947a6e324
commit 3dec491bb5

View File

@ -131,7 +131,8 @@ enum Subspace {
struct Hit {
distance: f32,
id: i32,
pos: Vec2,
pos: Vec2, // положение в основной СК
rel: Vec2, // положение в локальной ортонормированной СК объекта
}
struct FlatTraceResult {
@ -190,8 +191,8 @@ impl Space {
let ray = ray.forward(dist);
FlatTraceResult {
end: Some(cell.ray_to_global(ray)),
objects: objs.into_iter().filter_map(|Hit { id, distance, pos }|
if distance < dist { Some(Hit { id, distance, pos: cell.pos_to_global(pos) }) } else { None }
objects: objs.into_iter().filter_map(|Hit { id, distance, pos, rel }|
if distance < dist { Some(Hit { id, distance, pos: cell.pos_to_global(pos), rel }) } else { None }
).collect(),
}
}
@ -269,7 +270,8 @@ impl Space {
if diff > 0.0 {
let t = (-rel.dot(ray.dir) - diff.sqrt()) / ray.dir.length_squared();
if t >= 0.0 {
return Some(Hit { id: obj.id, distance: t, pos: ray.forward(t).pos });
let pos = ray.forward(t).pos;
return Some(Hit { id: obj.id, distance: t, pos, rel: pos - obj.loc.pos });
}
}
None
@ -318,6 +320,11 @@ fn draw_ray_2(gc: &mut Vec<Draw>, space: &Space, base: Vec2, dir: Vec2) {
Boundary => panic!(),
};
for hit in ret.objects {
let obj = space.objs[hit.id as usize];
hits.move_to(obj.loc.pos.x, obj.loc.pos.y);
for pt in trace_iter(&space.rect, obj.loc.pos, obj.loc.rot * hit.rel, hit.rel.length() / 100.0).take(100) {
hits.line_to(pt.x, pt.y);
}
hits.circle(hit.pos.x, hit.pos.y, 1.5);
}
let a = ray.pos;