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

Accessing world coordinates via cursor_position #65

Open
boyswan opened this issue Oct 5, 2021 · 4 comments
Open

Accessing world coordinates via cursor_position #65

boyswan opened this issue Oct 5, 2021 · 4 comments
Labels
question Further information is requested

Comments

@boyswan
Copy link

boyswan commented Oct 5, 2021

I'm struggling to access world coords with a mouse click. My current attempt is along the lines of:

if let Some(pos) = window.cursor_position() {
    if let Ok((camera_transform, camera)) = q_camera.single() {
        let low = camera.get_target_sizes(window).low;
        let size = Vec2::new(low.x as f32, low.y as f32);
   
        let p = pos - size / 2.0;
        let pos_wld = camera_transform.compute_matrix() * p.extend(0.0).extend(1.0);
        let pos = Vec2::new(pos_wld.x, -pos_wld.y);

        info!("world xy", pos);
    }
}

However I can't seem to figure out the correct way to find the coords and am not sure whether bevy_retrograde will require a different calculation (I'm trying to move a sprite to an ldtk tile).

Apologies for making an issue as I know it's not a bug, but I'm not sure where else to ask and have been stuck on this for a while!

@zicklag zicklag added enhancement New feature or request question Further information is requested and removed enhancement New feature or request labels Oct 5, 2021
@zicklag
Copy link
Member

zicklag commented Oct 5, 2021

Ah, I see. This would be easier if there was a function directly on the camera that would do the calculations for you, but since we are switching to Bevy's renderer, that would mean that you would just do it however Bevy does it, so I think we won't worry about adding a feature for it here.

As soon as I get the chance, I'll give you a code sample showing how to get the world position, after I test it and make sure it works. :)

Feel free to ping me if I don't respond within the next couple days or so!

@boyswan
Copy link
Author

boyswan commented Oct 7, 2021

@zicklag Sorry to nudge, I'm sure you're busy, but I'm totally stuck with this one 😔 Would really appreciate some pointers!

@zicklag
Copy link
Member

zicklag commented Oct 7, 2021

OK, so in the currently released version of Bevy Retrograde this is pretty much a math nightmare and I don't have a solution for it right now. 😦 Essentially positioning the pixels on the screen is all done in a shader and it would take replicating that code in Rust in order to figure out which pixel on the screen refers to what position in space. ( Not horrible but not time I have right now to try and fix something we are going to trash once we switch to Bevy's renderer )

The way that I designed the current renderer is just horrible for this and it should be way simpler, so it is another good reason for moving to Bevy's renderer.

Unfortunately right now I think the best option is to try to use the experimental version of Bevy Retrograde, but that requires using both Bevy Retrograde and Bevy itself from unreleased git versions. Both of which are unstable, especially the rendering because it uses Bevy's new work-in-progress renderer. ☹️

Then you'd have still to find out from the Bevy folks ( because I haven't done it yet ) how to get the world position of the cursor. And I'm not sure I'll have time to help with the experimental version of Bevy Retrograde, so you might just get stuck on that anyway.

I'm sorry to say there isn't a great option for this right now. Sorry I couldn't help more! It should be resolved eventually when the new version of Bevy comes out, and I have time to update Bevy Retrograde after that, but I'm not sure when either are going to happen.

@boyswan
Copy link
Author

boyswan commented Oct 7, 2021

No problem at all, appreciate you taking the time to look into it 👍

@zicklag zicklag mentioned this issue Jun 24, 2022
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants