Skip to content

Commit 81a8504

Browse files
louis-le-camjakobhellermann
authored andcommitted
Convert global transform to local transform so entities don't go away when moving them
1 parent b1d0f9a commit 81a8504

File tree

1 file changed

+14
-6
lines changed
  • crates/bevy_editor_pls_default_windows/src

1 file changed

+14
-6
lines changed

crates/bevy_editor_pls_default_windows/src/gizmos.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ fn draw_gizmo(
186186
}
187187

188188
for selected in selected_entities.iter() {
189-
let Some(transform) = world.get::<GlobalTransform>(selected) else {
189+
let Some(global_transform) = world.get::<GlobalTransform>(selected) else {
190190
continue;
191191
};
192-
let model_matrix = transform.compute_matrix();
192+
let model_matrix = global_transform.compute_matrix();
193193

194194
let Some(result) = egui_gizmo::Gizmo::new(selected)
195195
.model_matrix(model_matrix.into())
@@ -202,11 +202,19 @@ fn draw_gizmo(
202202
continue;
203203
};
204204

205+
let global_affine = global_transform.affine();
206+
205207
let mut transform = world.get_mut::<Transform>(selected).unwrap();
206-
*transform = Transform {
207-
translation: Vec3::from(<[f32; 3]>::from(result.translation)),
208-
rotation: Quat::from_array(<[f32; 4]>::from(result.rotation)),
209-
scale: Vec3::from(<[f32; 3]>::from(result.scale)),
208+
209+
let parent_affine = global_affine * transform.compute_affine().inverse();
210+
let inverse_parent_transform = GlobalTransform::from(parent_affine.inverse());
211+
212+
let global_transform = Transform {
213+
translation: result.translation.into(),
214+
rotation: result.rotation.into(),
215+
scale: result.scale.into(),
210216
};
217+
218+
*transform = (inverse_parent_transform * global_transform).into();
211219
}
212220
}

0 commit comments

Comments
 (0)