Reputation: 461
I am implementing a system that takes care of dragging and dropping inventory items and want a way to return items to their old place if the desired place is invalid. I found a good method for that use case in the smooth_nudge
method.
Bevy provides the
StableInterpolate Trait which provides the smooth_nudge
method to interpolate the position of two Transforms.
I want to know what the best way is to check if the desired target Transform is reached.
A naive implementation could simply compare the position of one transformation with the target transformation, but due to floating arithmetic this approach does not work very well.
#[derive(Component)]
struct DragReturn {
target_position: Vec3,
}
fn move_invalid_drag_to_old_pos(
mut commands: Commands,
mut query: Query<(Entity, &mut Transform, &DragReturn)>,
time: Res<Time>,
) {
for (e, mut t, dr) in &mut query {
// reached position
if t.translation == dr.target_position {
println!("remove DragReturn");
commands.entity(e).remove::<DragReturn>();
}
t.translation
.smooth_nudge(&dr.target_position, 2.0, time.delta_secs());
}
}
Also, one could calculate the distance between the two positions and consider the objects to have arrived at the target if the distance is very small. My question now is if there is a standardized/best practice Bevy way to check this?
Upvotes: 0
Views: 14