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

Support for "closest approaches" #23

Closed
SorteKanin opened this issue Sep 6, 2021 · 1 comment
Closed

Support for "closest approaches" #23

SorteKanin opened this issue Sep 6, 2021 · 1 comment

Comments

@SorteKanin
Copy link

This is a great crate so far but what I'm personally missing is a way to see how close a ray was to intersecting when it didn't quite intersect.

The use case I'm imagining is picking meshes with some amount of leniency in precision. For example, in RTS games like Warcraft and Starcraft, you don't have to click exactly on the mesh of a unit to select it. Getting within a certain distance is good enough. This makes the experience for players much better because selecting units is much easier (because you don't have to be super precise).

I don't really see a way to implement this myself downstream, as this crate only provides intersections (do tell me if you think there is a way!). So I guess implementing directly in this crate is the only option.

I thought that perhaps RayCastSource could be extended with the closest approaches like this (names could use work):

pub struct RayCastSource<T> {
    pub cast_method: RayCastMethod,
    ray: Option<Ray3d>,
    intersections: Vec<(Entity, Intersection)>,
    closest: HashMap<Entity, ClosestPoint>,
    _marker: PhantomData<T>,
}

where the closest map holds for each entity, the single closest point from the mesh to the ray. (side note: Why is the type of intersections not HashMap<Entity, Vec<Intersection>>?). ClosestPoint (could be named better) would simply be something along the lines of this:

/// The two points that are the closest points between the ray and the triangle.
struct ClosestPoint {
    ray_point: Vec3,
    triangle_point: Vec3,
    origin: Vec3,
}

impl ClosestPoint {
    pub fn new(ray_point: Vec3, triangle_point: Vec3, origin: Vec3) -> Self {
        Self {
            ray_point,
            triangle_point,
            origin
        }
    }

    /// The point on the ray that is closest to the triangle.
    pub fn ray_point(&self) -> Vec3 {
        self.ray_point
    }

    /// The point on the triangle that is closest to the ray.
    pub fn triangle_point(&self) -> Vec3 {
        self.triangle_point
    }

    /// The origin of the ray.
    pub fn origin(&self) -> Vec3 {
        self.origin
    }
}

Then one could easily check if the ray is "close enough" to select it with some leniency.

Do you see another way of implementing this idea of picking leniency? Maybe I'm missing something obvious. Also is this the right crate to implement this kind of thing in, or should this be in a downstream crate (i.e. bevy_mod_picking)? Otherwise, if you think this is a good idea, I wouldn't mind trying my hand at implementing it and doing a PR.

@aevyrie
Copy link
Owner

aevyrie commented Sep 10, 2021

This would probably be best accomplished in a color picking shader in bevy_mod_picking. See this issue: aevyrie/bevy_mod_picking#3.

@aevyrie aevyrie closed this as completed Sep 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants