katharostech / bevy_retrograde

Plugin pack for making 2D games with Bevy
Other
297 stars 9 forks source link

Accessing world coordinates via cursor_position #65

Open boyswan opened 3 years ago

boyswan commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

zicklag commented 3 years ago

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. :frowning: 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. :frowning_face:

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 commented 3 years ago

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