Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raycasting #615

Closed
wants to merge 13 commits into from
18 changes: 13 additions & 5 deletions crates/bevy_physics/src/d3/intersectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,20 @@ impl RayIntersector for Sphere {
if d < 0.0 {
None
} else {
let t = (-b - d.sqrt()) / (2.0 * a);
let distance = (-b - d.sqrt()) / (2.0 * a);

Some(RayHit::new(t, *ray.origin() + *ray.direction() * t))
Some(RayHit::new(
distance,
*ray.origin() + *ray.direction() * distance,
))
}
}
}

impl RayIntersector for Triangle {
// using the Moeller-Trumbore intersection algorithm
// Can anyone think of sensible names for theese?
#[allow(clippy::many_single_char_names)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this allow acceptable?

fn intersect_ray(&self, ray: &Ray) -> Option<RayHit> {
let edges = (self.1 - self.0, self.2 - self.0);
let h = ray.direction().cross(edges.1);
Expand All @@ -86,10 +91,13 @@ impl RayIntersector for Triangle {
return None;
}

let t = f * edges.1.dot(q);
let distance = f * edges.1.dot(q);

if t > f32::EPSILON {
Some(RayHit::new(t, *ray.origin() + *ray.direction() * t))
if distance > f32::EPSILON {
Some(RayHit::new(
distance,
*ray.origin() + *ray.direction() * distance,
))
} else {
None
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_physics/src/d3/ray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ impl Ray {
let near = near.truncate() / near.w;
let far = far.truncate() / far.w;

let direction: Vec3 = (far - near).into();
let origin: Vec3 = near.into();
let direction: Vec3 = far - near;
let origin: Vec3 = near;

return Self { origin, direction };
Self { origin, direction }
}

pub fn origin(&self) -> &Vec3 {
Expand Down
2 changes: 2 additions & 0 deletions examples/physics/raycast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ struct MouseState {
cursor_position: Vec2,
}

// Is there a way to reduce the arguments?
#[allow(clippy::too_many_arguments)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this allow acceptable?

fn raycast(
commands: &mut Commands,
mut meshes: ResMut<Assets<Mesh>>,
Expand Down