fix mesh colliders breaking due to CENTER_WORLD_ON_PLAYER

This commit is contained in:
yuni 2024-04-16 02:44:01 +02:00
parent 830d371e36
commit efd85e1433

View file

@ -63,11 +63,11 @@ impl Plugin for WorldPlugin {
if CENTER_WORLD_ON_PLAYER {
// Disable bevy_xpbd's position->transform sync function
app.insert_resource(SyncConfig {
position_to_transform: false,
position_to_transform: true,
transform_to_position: false,
});
// Add own position->transform sync function
app.add_systems(PreUpdate, position_to_transform);
app.add_systems(PostUpdate, position_to_transform.after(bevy_xpbd_3d::plugins::sync::position_to_transform).in_set(bevy_xpbd_3d::plugins::sync::SyncSet::PositionToTransform));
}
}
}
@ -477,39 +477,39 @@ pub fn position_to_transform(
if let Ok(player_pos) = q_player.get_single() {
for (mut transform, pos, rot, parent) in &mut q_trans {
if let Some(parent) = parent {
if let Ok((parent_transform, parent_pos, parent_rot)) = parents.get(**parent) {
// Compute the global transform of the parent using its Position and Rotation
let parent_transform = parent_transform.compute_transform();
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;
}
// if let Ok((parent_transform, parent_pos, parent_rot)) = parents.get(**parent) {
// // Compute the global transform of the parent using its Position and Rotation
// let parent_transform = parent_transform.compute_transform();
// 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,