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