bevyengine / bevyengine/bevy

window.cursor_position is inaccurate and kind of unreasonable.

Open
#8,507 8 comments 0 reactions 0 assignees View on GitHub
A-Input A-Windowing C-Bug
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## Bevy version

Newest master branch #[670f3f0](https://github.com/bevyengine/bevy/commit/670f3f0dce2ced27388b10672d30594d399434e1)

## \[Optional\] Relevant system information

If you cannot get Bevy to build or run on your machine, please include:

- the Rust version you're using (you can get this by running `cargo --version`)
- 1.69.0
- the operating system or browser used, including its version
- macOS v13

## What you did

I'm trying to get cursor_position in FileDragAndDrop::DroppedFile event.

Here is the code.
```rust
fn file_drag_and_drop_system(
mut file_events: EventReader,
mut image_drop_event_writer: EventWriter,
windows: Query<&mut Window>,
cameras: Query<(&Camera, &GlobalTransform), With>,
) {
let (cam, cam_transform) = cameras.single();

for event in file_events.iter() {
if let FileDragAndDrop::DroppedFile { window, path_buf } = event {
let win = windows.get(*window).unwrap();

if path_buf.is_file() {
let ext = path_buf.extension().unwrap().to_str().unwrap();
if ["bmp", "dds", "exr", "jpeg", "jpg", "tga", "png"].contains(&ext) {
let pos = win
.cursor_position()
.and_then(|cursor| cam.viewport_to_world(cam_transform, cursor))
.map(|ray| ray.origin.truncate());

println!("{:?} {:?}", win.cursor_position(), pos);

image_drop_event_writer.send(ImageDropEvent {
droped_image_path: path_buf.clone(),
world_pos: Vec2::new(0.0, 0.0),
})
}
}
}
}
}
```

## What went wrong

When I drop a png file into same location of window multiple times, the cursor position is acting weird and kind of unreasonable, it will be none sometime.

And the world_position is also very weird.

Following is the screen record, check the left bottom corner of the print out messages.

https://user-images.githubusercontent.com/18550/234897526-95117a43-6d37-4b1b-a15c-dd46c925a8c5.mp4

Here is the screen shot of the output incase the video is not clear enough.

![截屏2023-04-27 22 43 55](https://user-images.githubusercontent.com/18550/234898947-a77a654e-8ff7-4488-830e-a36568edd496.png)

- what were you expecting?

window.cursor_position should always return the cursor position.
and camera.viewport_to_world should be able to convert cursor position to world position.

- what actually happened?

window.cursor_position will return none sometime
camera.viewport_to_world always returns wrong position.

Contributor guide

Open the contributing guide

Research direction

Reproduce the repeated file-drop case on macOS using the reported Bevy revision and inspect the FileDragAndDrop::DroppedFile event, Window::cursor_position, and Camera::viewport_to_world behavior. Compare the cursor and world positions across repeated drops, then define the fix by ensuring the reported cursor position and converted world position remain correct for each drop.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.