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

Sprite backend improvements #354

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jnhyatt
Copy link

@jnhyatt jnhyatt commented Jul 23, 2024

Sprite backend was not reporting world position or normal for hits. Upon further investigation I found that the backend was also not correctly hit testing sprites if they had rotation about any axis other than the Z axis. This was producing some very surprising results in some cases (i.e. reported hits weren't even close to the sprites).

This pr fixes both of these issues: sprite hits are now determined by intersecting the sprite's bounding rect with a 3d line segment extending from (cursor_x, cursor_y, near_clip) and (cursor_x, cursor_y, far_clip). This should return correct results for any arbitrary sprite transform. Hit position and normal are now being populated as well.

One question I have about the impl is whether backends are supposed to return hits in any particular order. Before, depth was being determined by sprite's Z coordinate, but since sprites now support arbitrary rotations, it's possible that a sprite with a deeper Z value could be picked in front of another. Does that mean that the hits should be sorted after hits are collected? And if so, does that mean that the sprite sorting that happens before the hit-testing can/should be removed?

@aevyrie
Copy link
Owner

aevyrie commented Jul 24, 2024

The answers to your questions are documented here: https://docs.rs/bevy_mod_picking/latest/bevy_mod_picking/backend/struct.PointerHits.html#structfield.picks

An unordered collection of entities and their distance (depth) from the cursor.

and here: https://docs.rs/bevy_mod_picking/latest/bevy_mod_picking/backend/struct.HitData.html#structfield.depth.html

depth only needs to be self-consistent with other PointerHitss using the same RenderTarget. However, it is recommended to use the distance from the pointer to the hit, measured from the near plane of the camera, to the point, in world space.

github-merge-queue bot pushed a commit to bevyengine/bevy that referenced this pull request Aug 26, 2024
# Objective

Add `bevy_picking` sprite backend as part of the `bevy_mod_picking`
upstreamening (#12365).

## Solution

More or less a copy/paste from `bevy_mod_picking`, with the changes
[here](aevyrie/bevy_mod_picking#354). I'm
putting that link here since those changes haven't yet made it through
review, so should probably be reviewed on their own.

## Testing

I couldn't find any sprite-backend-specific tests in `bevy_mod_picking`
and unfortunately I'm not familiar enough with Bevy's testing patterns
to write tests for code that relies on windowing and input. I'm willing
to break the pointer hit system into testable blocks and add some more
modular tests if that's deemed important enough to block, otherwise I
can open an issue for adding tests as follow-up.

## Follow-up work

- More docs/tests
- Ignore pick events on transparent sprite pixels with potential opt-out

---------

Co-authored-by: Aevyrie <aevyrie@gmail.com>
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

Successfully merging this pull request may close these issues.

2 participants