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

Code before:
        self.dispatcher.release();
        self.physics.release();
        self.foundation.release();
    }
}

fn vec3_changed(old: Vec3, new: Vec3) -> bool {
    (new - old).length() > 0.001
}
fn quat_changed(old: Quat, new: Quat) -> bool {
    !old.abs_diff_eq(new, 0.001)
}

/// Syncs physx to the ECS
pub fn sync_ecs_physics() -> SystemGroup {
    let mut new_positions = Vec::new();
    let mut new_rotations = Vec::new();

    let translation_rotation_qs = Arc::new(Mutex::new(QueryState::new()));
    let translation_rotation_q =
        query((translation().changed(), rotation().changed())).incl(physics_shape());
    let translation_rotation_q2 = translation_rotation_q.query.clone();

    // we need a seperate query here for child entities
    // Though they have rotation and translation those values may not update
    // the parents will and local_to_world.changed() will need to be updated
    let hiearchy_transform_qs = Arc::new(Mutex::new(QueryState::new()));
    let hiearchy_transform_q = query(local_to_world().changed()).incl(physics_shape());
    let hiearchy_transform_q2 = translation_rotation_q.query.clone();

    let translation_character_qs = Arc::new(Mutex::new(QueryState::new()));
    let translation_character_q =
        query((translation().changed(), character_controller().changed()))
            .incl(physics_controlled());
    let translation_character_q2 = translation_character_q.query.clone();

    let scale_qs = Arc::new(Mutex::new(QueryState::new()));
    let scale_q = query(scale().changed()).incl(physics_controlled());
    let scale_q2 = scale_q.query.clone();

    let linear_velocity_qs = Arc::new(Mutex::new(QueryState::new()));
    let linear_velocity_q = query(linear_velocity().changed()).incl(physics_controlled());
    let linear_velocity_q2 = scale_q.query.clone();

    let angular_velocity_qs = Arc::new(Mutex::new(QueryState::new()));
    let angular_velocity_q = query(angular_velocity().changed()).incl(physics_controlled());
    let angular_velocity_q2 = scale_q.query.clone();

    SystemGroup::new(
        "sync_ecs_physics",
        vec![

Review comment (suggestion):
Hm... it might make more sense to just replace the translation_rotation one, and then maybe have something that reads back changes to ltw to translation rotation.

In general we need to look over the physics system, it was built a long time ago for slightly different requirements that what's needed now

Code after:
        self.dispatcher.release();
        self.physics.release();
        self.foundation.release();
    }
}

fn vec3_changed(old: Vec3, new: Vec3) -> bool {
    (new - old).length() > 0.001
}
fn quat_changed(old: Quat, new: Quat) -> bool {
    !old.abs_diff_eq(new, 0.001)
}

/// Syncs physx to the ECS
pub fn sync_ecs_physics() -> SystemGroup {
    let mut new_positions = Vec::new();
    let mut new_rotations = Vec::new();

    let translation_rotation_qs = Arc::new(Mutex::new(QueryState::new()));
    let translation_rotation_q =
        query((translation().changed(), rotation().changed())).incl(physics_shape());
    let translation_rotation_q2 = translation_rotation_q.query.clone();

    // we need a seperate query here for child entities
    // Though they have rotation and translation those values may not update
    // the parents will and local_to_world.changed() will need to be updated
    let hiearchy_transform_qs = Arc::new(Mutex::new(QueryState::new()));
    let hiearchy_transform_q = query(local_to_world().changed())
        .incl(physics_shape())
        .incl(local_to_parent());
    let hiearchy_transform_q2 = hiearchy_transform_q.query.clone();

    let translation_character_qs = Arc::new(Mutex::new(QueryState::new()));
    let translation_character_q =
        query((translation().changed(), character_controller().changed()))
            .incl(physics_controlled());
    let translation_character_q2 = translation_character_q.query.clone();

    let scale_qs = Arc::new(Mutex::new(QueryState::new()));
    let scale_q = query(scale().changed()).incl(physics_controlled());
    let scale_q2 = scale_q.query.clone();

    let linear_velocity_qs = Arc::new(Mutex::new(QueryState::new()));
    let linear_velocity_q = query(linear_velocity().changed()).incl(physics_controlled());
    let linear_velocity_q2 = scale_q.query.clone();

    let angular_velocity_qs = Arc::new(Mutex::new(QueryState::new()));
    let angular_velocity_q = query(angular_velocity().changed()).incl(physics_controlled());
    let angular_velocity_q2 = scale_q.query.clone();

    SystemGroup::new(