Repository: AmbientRun/Ambient
File: crates/physics/src/physx.rs (Rust)

Code before:
                    }
                }
            }),
            linear_velocity_q.to_system({
                let linear_velocity_qs = linear_velocity_qs.clone();
                move |q, world, _, _| {
                    let mut qs = linear_velocity_qs.lock();
                    for (id, &vel) in q.iter(world, Some(&mut *qs)) {
                        if let Ok(body) = world.get(id, rigid_dynamic()) {
                            body.set_linear_velocity(vel, true);
                        }
                    }
                }
            }),
            angular_velocity_q.to_system({
                let angular_velocity_qs = angular_velocity_qs.clone();
                move |q, world, _, _| {
                    let mut qs = angular_velocity_qs.lock();
                    for (id, &vel) in q.iter(world, Some(&mut *qs)) {
                        if let Ok(body) = world.get(id, rigid_dynamic()) {
                            body.set_angular_velocity(vel, true);
                        }
                    }
                }
            }),
            contact_offset_q.to_system({
                let contact_offset_qs = contact_offset_qs.clone();
                move |q, world, _, _| {
                    let mut qs = contact_offset_qs.lock();
                    for (id, &off) in q.iter(world, Some(&mut *qs)) {
                        if let Ok(body) = world.get(id, rigid_dynamic()) {
                            for shape in get_shapes(world, id) {
                                shape.set_contact_offset(off);
                            }
                        }
                    }
                }
            }),
            rest_offset_q.to_system({
                let rest_offset_qs = rest_offset_qs.clone();
                move |q, world, _, _| {
                    let mut qs = rest_offset_qs.lock();
                    for (id, &off) in q.iter(world, Some(&mut *qs)) {
                        if let Ok(body) = world.get(id, rigid_dynamic()) {
                            for shape in get_shapes(world, id) {
                                shape.set_rest_offset(off);
                            }
                        }
                    }
                }
            }),

Review comment (suggestion):
So this won't read the contact offset back anywhere, and in that case you can just use the `query` here directly instead of defining it up there. The only reason they are defined up there is to reset them later in the file

Code after:
                move |q, world, _, _| {
                    let mut qs = linear_velocity_qs.lock();
                    for (id, &vel) in q.iter(world, Some(&mut *qs)) {
                        if let Ok(body) = world.get(id, rigid_dynamic()) {
                            body.set_linear_velocity(vel, true);
                        }
                    }
                }
            }),
            angular_velocity_q.to_system({
                let angular_velocity_qs = angular_velocity_qs.clone();
                move |q, world, _, _| {
                    let mut qs = angular_velocity_qs.lock();
                    for (id, &vel) in q.iter(world, Some(&mut *qs)) {
                        if let Ok(body) = world.get(id, rigid_dynamic()) {
                            body.set_angular_velocity(vel, true);
                        }
                    }
                }
            }),
            query(contact_offset().changed()).incl(physics_controlled()).to_system({
                move |q, world, qs, _| {
                    for (id, &off) in q.iter(world, qs) {
                        for shape in get_shapes(world, id) {
                            shape.set_contact_offset(off);
                        }
                    }
                }
            }),
            query(rest_offset().changed()).incl(physics_controlled()).to_system({
                move |q, world, qs, _| {
                    for (id, &off) in q.iter(world, qs) {
                        for shape in get_shapes(world, id) {
                            shape.set_rest_offset(off);
                        }
                    }
                }
            }),
            // Sync PhysX changes to ECS.
            query((rigid_dynamic(), translation(), rotation())).incl(physics_controlled()).to_system(|q, world, qs, _| {
                for (id, (rigid_dynamic, pos, rot)) in q.collect_cloned(world, qs) {
                    let pose = rigid_dynamic.get_global_pose();
                    let new_pos = pose.translation();
                    let new_rot = pose.rotation();
                    if vec3_changed(pos, new_pos) {
                        world.set(id, translation(), new_pos).unwrap();
                    }
                    if quat_changed(rot, new_rot) {
                        world.set(id, rotation(), new_rot).unwrap();
                    }
                }