-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Bevy picking fails after camera movment. Picking has wrong positions for entities after camera viewport is moved #19122
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
Comments
It seems that the problem is in the picking backend, since the PointerHits for the entity is missing after camera move. (A PointerHits event produced by a picking backend after it has run its hit tests, describing the entities under a pointer. https://docs.rs/bevy/latest/bevy/picking/backend/struct.PointerHits.html) How i got the debug info: Info is from hovering the mouse over objects and using this system fn listen_to_pointer_hits_on_keyboard_input(
mut pointer_hits_events: EventReader<PointerHits>,
keyboard_input: Res<ButtonInput<KeyCode>>,
) {
if keyboard_input.pressed(KeyCode::KeyB) {
info!("'B' currently pressed");
for event in pointer_hits_events.read() {
dbg!(&event);
}
}
} When it does not work, it only finds the "default nothing event", and "hit default camera in primary window"-event.
When it works before the camera move i see the event when i hover over it :
|
It seems like UI-elements/Node-elements are not impacted by this bug. In the example code i posted, the "click me to get a box..." text has correct hitbox, but the "box-entities" that are spawned are affected by the bug. |
Further findings: It seems like the RayMap is wrong after a camera viewport move. More specifically: The "origin" Vec3 of the Ray3D objects inside RayMap is wrong after a camera viewport move. The (0,0) X,Y spot is in the center of the camera before move, but after the move, it has shifted the same direction and distance as the camera viewport move. (to the right in this example) Debug systems // My raycasting backend debugger
pub fn debug_ray_map(ray_map: Res<RayMap>,
keyboard_input: Res<ButtonInput<KeyCode>>,
) {
// for (&ray_id, &ray) in ray_map.iter() {
if keyboard_input.pressed(KeyCode::KeyC) {
info!("'C' currently pressed");
for (ray) in ray_map.iter() {
dbg!(&ray);
}
}
}
fn move_camera_a_bit_on_button_press(
mut kamera_query: Query<&mut Camera>,
keyboard_input: Res<ButtonInput<KeyCode>>,
) {
if keyboard_input.pressed(KeyCode::KeyA) {
info!("'A' currently pressed");
if let Ok(mut camera) = kamera_query.single_mut() {
// println!("moving camera in window");
// move viewport
if let Some(viewport) = &mut camera.viewport {
let u32_vektor: &mut UVec2 = &mut viewport.physical_position;
u32_vektor.x += 20
}
}
}
} RayMap docs : https://docs.rs/bevy/latest/bevy/picking/backend/prelude/struct.RayMap.html |
i also ran into this yesterday, and looked at your investigation and went and fixed it, then found that someone already did the same thing in main :D 7f04906 |
Picking has wrong positions for entities after camera viewport is moved
Bevy version 0.16.0
Relevant system information
If your bug is rendering-related, copy the adapter info that appears when you run Bevy.
What you did
I have a window and a camera with a viewport + a button that allows me to drag the camera around on the window. The dragging is done with bevy picking (
Trigger<Pointer<Drag>>
). I simply move the camera around. And it only works once. Picking fails to find the button where it is rendered if the camera was moved.Code to replicate this bug is lower down.
What went wrong
The first time i draged the button, the camera-moving works fine.
If the camera was moved away from the start position, it fails the second time.
Pictures
The blue square is the button i am dragging. Note that the debugging find the correct entity in picture 1, but not in picture 2 after the camera has been draged to the right. Picture 3 shows the "ghost-button"
(i was not able to upload video, so it might be easiest to run the code and see)
What i suspect is wrong
I think the Picking is using the distance to the window left edge, instead of to the camera left edge.
Either that or the distance is being added twice. The ghost button always seems to be about twice the distance away.
Code to replicate bug
It is an extention of the https://github.com/bevyengine/bevy/blob/main/examples/picking/debug_picking.rs example, so it provides a lot of debug info. I changed it to 2d to make things a bit easier.
The text was updated successfully, but these errors were encountered: