cleanup: simplify position_to_transform

This commit is contained in:
yuni 2024-04-16 19:51:04 +02:00
parent 24edac27e5
commit 7475b104ba

View file

@ -450,58 +450,20 @@ fn handle_cheats(
} }
} }
// A variant of bevy_xpbd_3d::plugins::position_to_transform that adjusts // An extension of bevy_xpbd_3d::plugins::position_to_transform that adjusts
// the rendering position to center entities at the player camera. // the rendering position to center entities at the player camera.
// This avoids rendering glitches when very far away from the origin. // This avoids rendering glitches when very far away from the origin.
pub fn position_to_transform( pub fn position_to_transform(
q_player: Query<&Position, With<actor::PlayerCamera>>, q_player: Query<&Position, With<actor::PlayerCamera>>,
mut q_trans: Query<(&'static mut Transform, &'static Position, &'static Rotation, Option<&'static Parent>)>, mut q_trans: Query<(&'static mut Transform, &'static Position), Without<Parent>>,
parents: Query<(&'static GlobalTransform, Option<&'static Position>, Option<&'static Rotation>), With<Children>>,
) { ) {
if let Ok(player_pos) = q_player.get_single() { if let Ok(player_pos) = q_player.get_single() {
for (mut transform, pos, rot, parent) in &mut q_trans { for (mut transform, pos) in &mut q_trans {
if let Some(parent) = parent { transform.translation = Vec3::new(
// if let Ok((parent_transform, parent_pos, parent_rot)) = parents.get(**parent) { (pos.x - player_pos.x) as f32,
// // Compute the global transform of the parent using its Position and Rotation (pos.y - player_pos.y) as f32,
// let parent_transform = parent_transform.compute_transform(); (pos.z - player_pos.z) as f32,
// let parent_pos = parent_pos.map_or(parent_transform.translation, |pos| { );
// pos.as_vec3()
// // NOTE: I commented out this because it turns a vec3 to a vec4,
// // and I don't understand why bevy_xpbd would do that.
// //.extend(parent_transform.translation.z)
// });
// let parent_rot = parent_rot.map_or(parent_transform.rotation, |rot| {
// rot.as_quat()
// });
// let parent_scale = parent_transform.scale;
// let parent_transform = Transform::from_translation(parent_pos)
// .with_rotation(parent_rot)
// .with_scale(parent_scale);
//
// // The new local transform of the child body,
// // computed from the its global transform and its parents global transform
// let new_transform = GlobalTransform::from(
// Transform::from_translation(
// pos.as_vec3()
// // NOTE: I commented out this because it turns a vec3 to a vec4,
// // and I don't understand why bevy_xpbd would do that.
// //.extend(parent_pos.z + transform.translation.z * parent_scale.z),
// )
// .with_rotation(rot.as_quat()),
// )
// .reparented_to(&GlobalTransform::from(parent_transform));
//
// transform.translation = new_transform.translation;
// transform.rotation = new_transform.rotation;
// }
} else {
transform.translation = Vec3::new(
(pos.x - player_pos.x) as f32,
(pos.y - player_pos.y) as f32,
(pos.z - player_pos.z) as f32,
);
transform.rotation = rot.as_quat();
}
} }
} }
} }