#[derive(Default)]
struct Position(Vec2);
fn update_trails(
mut trail_paths: Query<(&mut Path, &Parent), With<Trail>>,
mut previous_pos: Local<Position>,
transforms: Query<&Transform, Changed<RigidBodyVelocityComponent>>,
rapier_config: Res<RapierConfiguration>,
constants: Res<Constants>,
) {
let scale = rapier_config.scale;
let size = 0.3 * scale;
for (mut path, parent) in trail_paths.iter_mut() {
let pos = if let Ok(transform) = transforms.get(parent.0) {
transform.translation.truncate()
} else {
continue;
};
let delta = previous_pos.0 - pos;
previous_pos.0 = pos;
if delta == Vec2::ZERO {
continue;
}
let trail = delta.normalize() * (size * constants.trail_size_scale + delta.length());
let polygon = shapes::Polygon {
points: vec![
trail,
Vec2::new(trail.y, -trail.x).normalize() * size, // 90 degrees clockwize
Vec2::new(-trail.y, trail.x).normalize() * size, // 90 degrees counterclockwize
],
closed: true,
};
*path = ShapePath::build_as(&polygon);
}
}