Plonq / bevy_panorbit_camera

A simple pan and orbit camera for the Bevy game engine
Apache License 2.0
186 stars 36 forks source link

Camera transforms through `bevy_egui` reverted when `PanOrbitCamera` regains control #82

Closed ExpertOfNil closed 2 months ago

ExpertOfNil commented 2 months ago

I am attempting to use a bevy_egui window to explicitly set my camera position. The camera positions update with changes made within the bevy_egui window, but once the PanOrbitCamera makes a change, the camera transform reverts back to the position and rotation it had when bevy_egui got focus.

https://github.com/user-attachments/assets/839480be-ac7b-40d8-8809-0a561cca2362

Plonq commented 2 months ago

Hi there. This is expected behaviour. bevy_panorbit_camera has its own state that describes the camera transform (focus, radius, yaw, and pitch). If you change the camera transform directly, then those changes will be overridden the next time bevy_panorbit_camera updates it.

In other words, this plugin 'takes over' the camera transform. If you want to programmatically alter it, you have to modify the plugin state, rather than the transform itself.

That said, there might be a solution. If you set PanOrbitCamera.initialized to false whenever you update the transform directly, then the plugin should recalculate its state based on the new transform. Give that a try and let me know if it works.

ExpertOfNil commented 2 months ago

@Plonq I tried setting PanOrbitCamera.initialized = false directly before and/or directly after changing the camera transform, but this resulted in my bevy_egui interface being unresponsive.

After some trial and error, it seems like I may have to store and augment the PanOrbitCamera transform, as you said previously.

I'll keep working at this and see if I can come up with a solution. Thank you for your help!

Plonq commented 2 months ago

Hmm, PanOrbitCamera probably interferes with the egui stuff on initialisation.

Another idea is to modify focus, radius, yaw, and pitch directly, but calculate them based on a transform. For example, this function calculates radius, yaw, and pitch based on a given translation and focus. It's not public but you could copy paste it.

ExpertOfNil commented 2 months ago

Hmm, PanOrbitCamera probably interferes with the egui stuff on initialisation.

Another idea is to modify focus, radius, yaw, and pitch directly, but calculate them based on a transform. For example, this function calculates radius, yaw, and pitch based on a given translation and focus. It's not public but you could copy paste it.

This is fantastic. I was just headed down that road, so thank you for saving me the effort! I'll report back after I integrate this and do some testing.

ExpertOfNil commented 2 months ago

@Plonq modifying the focus, radius, yaw and pitch directly worked well with the aid of the linked function. Thank you for your help!

Plonq commented 2 months ago

Glad you found a solution