#Can you get mouse/cursor position more than once per frame?

3 messages · Page 1 of 1 (latest)

wind dagger
#

I'm building a word game and the player is able to select letters by clicking and dragging. However if they drag too fast letters get skipped. Basically the cursor passes entirely over the letter in less than one frame.

I'm triggering the system by listening for a MouseMotion event, but because that only gives you deltas and not the position so I'm reading the cursor position as shown in this example. https://github.com/bevyengine/bevy/blob/latest/examples/2d/2d_viewport_to_world.rs

If there is more than one MouseMotion event per frame, both of those will have the same cursor position read from the world. Which makes sense since the system is executed once per frame.

I was able the log this. The two MouseMotions that happen between frames have the same cursor position, the second Vec2 in the log.

2024-01-25T19:38:16.884567Z INFO Wordtris::cursor: FRAME
2024-01-25T19:38:16.884588Z INFO Wordtris::cursor: MouseMotion { delta: Vec2(0.0, -17.0) } === Vec2(-52.606808, 342.55542)
2024-01-25T19:38:16.884597Z INFO Wordtris::cursor: MouseMotion { delta: Vec2(12.0, -48.0) } === Vec2(-52.606808, 342.55542)
2024-01-25T19:38:16.900753Z INFO Wordtris::cursor: FRAME
2024-01-25T19:38:16.900777Z INFO Wordtris::cursor: MouseMotion { delta: Vec2(34.0, -56.0) } === Vec2(-18.312813, 398.2832)
2024-01-25T19:38:16.917889Z INFO Wordtris::cursor: FRAME

I was thinking that I could use the delta's from the MouseMotion to sort of back calculate the mouse position at each point but there must be a way to pull this data directly.

Thanks.

GitHub

A refreshingly simple data-driven game engine built in Rust - bevyengine/bevy

dim monolith
#

I think a better way of solving this is instead of thinking of mouse events as discrete points, form a line every 2 mouse events and check if it intersects a letter. increasing the amount of points doesn't solve the problem, it just means you would have to move your mouse faster to skip letters.

wind dagger
#

Thanks. I like that approach a lot.

I wouldn't necessarily even need to use MouseMotion events because I could just record position every frame while the mouse button is down.

I'm sure there are other use cases that could use better time precision, so I'm open to hear if there is a direct way. But the line method should work great for my game.