-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Add delta to CursorMoved
event
#11695
Comments
For the moment, this is the approach i took in my own game code: Codeuse bevy::prelude::*;
pub struct CursorMovedDeltaPlugin;
impl Plugin for CursorMovedDeltaPlugin {
fn build(&self, app: &mut App) {
app.add_event::<CursorMovedWithDelta>()
.init_resource::<LastCursorPosition>()
.add_systems(PreUpdate, translate_cursor_moved_events);
}
}
#[derive(Event, Debug)]
pub struct CursorMovedWithDelta {
pub window: Entity,
pub position: Vec2,
pub delta: Vec2,
}
/// `None` if not set yet
#[derive(Resource, Debug, Default)]
pub struct LastCursorPosition(Option<Vec2>);
fn translate_cursor_moved_events(
mut last_cursor_position: ResMut<LastCursorPosition>,
mut cursor_motion: EventReader<CursorMoved>,
mut cursor_motion_with_delta: EventWriter<CursorMovedWithDelta>,
) {
for evt in cursor_motion.read() {
if let Some(last_cursor_position) = last_cursor_position.0 {
let delta = evt.position - last_cursor_position;
cursor_motion_with_delta.send(CursorMovedWithDelta {
window: evt.window,
position: evt.position,
delta,
});
}
last_cursor_position.0 = Some(evt.position);
}
} |
I agree that this might be useful, but since |
That's what i thought too. |
# Objective - Fixes #11695 ## Solution - Added `delta: Option<Vec2>` to `bevy_window::CursorMoved`. `delta` is an `Option` because the `CursorMoved` event does get fired when the cursor was outside the window area in the last frame. In that case there is no cursor position from the last frame to compare with the current cursor position. --- ## Changelog - Added `delta: Option<Vec2>` to `bevy_window::CursorMoved`. ## Migration Guide - You need to add `delta` to any manually created `CursorMoved` struct. --------- Co-authored-by: Kanabenki <lucien.menassol@gmail.com> Co-authored-by: James Liu <contact@jamessliu.com>
What problem does this solve or what need does it fill?
Since
MouseMotion
(afaik) intentionally doesn't take mouse acceleration or (maybe also?) scaling into account, one would have to useCursorMoved
to stay "up-to-date" to the actual cursor position(s).Unfortunately the
CursorMoved
event doesn't contain deltas. That means if one needs that, they would need to implement that themselves in their code.What solution would you like?
If possible and/or wanted
delta
to be added to theCursorMoved
event.What alternative(s) have you considered?
Implementing the delta calculation in my own game code or trying to solve my problem without using deltas.
Additional context
If this is nothing against this addition, i'd like to try implementing this myself. I haven't gone into the bevy internals so idk how possible this is, but i'd like to try :)
For additionaly context why i want this change: Added is a video of my WIP app, where i'm trying to implement a mouse-dragging camera controller. At the moment i'm using the deltas from
MouseMotion
, which causes issues since apparently on my laptop there's some kind of mouse acceleration or something.2024-02-04.14-08-08.mp4
The text was updated successfully, but these errors were encountered: