Join object hitting halves together
This commit is contained in:
parent
8e31359a69
commit
0a4078d4f3
|
|
@ -136,11 +136,6 @@ struct Hit {
|
|||
rel: Ray, // в локальной ортонормированной СК объекта
|
||||
}
|
||||
|
||||
struct HitInternal<'a> {
|
||||
object: &'a Object,
|
||||
distance: f32,
|
||||
}
|
||||
|
||||
struct FlatTraceResult {
|
||||
end: Option<Ray>,
|
||||
objects: Vec<Hit>,
|
||||
|
|
@ -263,31 +258,26 @@ impl Space {
|
|||
}).collect()
|
||||
}
|
||||
|
||||
fn trace_to_objects(objs: &[Object], ray: Ray) -> Vec<HitInternal> {
|
||||
fn hit_objects(objs: &[Object], ray: Ray, limit: f32, globalize: impl Fn(Vec2) -> Vec2) -> Vec<Hit> {
|
||||
objs.iter()
|
||||
.filter_map(|obj| {
|
||||
let rel = ray.pos - obj.loc.pos;
|
||||
let diff = rel.dot(ray.dir).powi(2) - ray.dir.length_squared() * (rel.length_squared() - obj.r.powi(2));
|
||||
if diff > 0.0 {
|
||||
let t = (-rel.dot(ray.dir) - diff.sqrt()) / ray.dir.length_squared();
|
||||
if t >= 0.0 {
|
||||
return Some(HitInternal { object: &obj, distance: t });
|
||||
}
|
||||
Some((obj, t))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
None
|
||||
}).collect()
|
||||
}
|
||||
|
||||
fn hit_objects(objs: &[Object], ray: Ray, dist: f32, globalize: impl Fn(Vec2) -> Vec2) -> Vec<Hit> {
|
||||
let hits = Self::trace_to_objects(objs, ray);
|
||||
hits.into_iter().filter_map(|HitInternal { object, distance }|
|
||||
if distance < dist {
|
||||
let pos = ray.forward(distance).pos;
|
||||
let rel = object.loc.rot.inverse() * (pos - object.loc.pos);
|
||||
let dir = object.loc.rot.inverse() * ray.dir;
|
||||
Some(Hit { id: object.id, distance, pos: globalize(pos), rel: Ray { pos: rel, dir } })
|
||||
} else { None }
|
||||
).collect()
|
||||
})
|
||||
.filter(|&(_, t)| t >= 0.0 && t < limit)
|
||||
.map(|(obj, t)| {
|
||||
let pos = ray.forward(t).pos;
|
||||
let rel = obj.loc.rot.inverse() * (pos - obj.loc.pos);
|
||||
let dir = obj.loc.rot.inverse() * ray.dir;
|
||||
Hit { id: obj.id, distance: t, pos: globalize(pos), rel: Ray { pos: rel, dir } }
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn line(&self, a: Vec2, b: Vec2, step: f32) -> Vec<Vec2> {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user