From a65e60a3162853cb8430119f36aecf98bcb1ee03 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Wed, 30 Apr 2025 16:54:25 -0700 Subject: [PATCH] use torque for lean, use Single and Populated instead of Query systems won't run if no entities match a Single or Populated query, so no need to return a Result or set the error handler --- src/main.rs | 5 --- src/physics.rs | 103 ++++++++++++++++++++++++------------------------- 2 files changed, 51 insertions(+), 57 deletions(-) diff --git a/src/main.rs b/src/main.rs index da52c31..128b956 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ use avian3d::prelude::{ }; use bevy::{ color::{palettes::css::SILVER, Alpha}, - ecs::error::{warn, GLOBAL_ERROR_HANDLER}, pbr::MeshMaterial3d, prelude::{ children, default, App, AppGizmoBuilder, Assets, ButtonInput, Color, Commands, @@ -24,10 +23,6 @@ use input::CyberInputPlugin; use physics::CyberPhysicsPlugin; fn main() { - GLOBAL_ERROR_HANDLER - .set(warn) - .expect("The error handler can only be set once, globally."); - // initialize Bevy App here let mut app = App::new(); app.add_plugins(( DefaultPlugins, diff --git a/src/physics.rs b/src/physics.rs index 1a96216..f75c372 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -20,9 +20,9 @@ pub struct CatControllerSettings { impl Default for CatControllerSettings { fn default() -> Self { Self { - kp: 45.0, - kd: 15.0, - ki: 2.5, + kp: 55.0, + kd: 20.0, + ki: 12.5, } } } @@ -58,14 +58,14 @@ mod systems { use std::f32::consts::{FRAC_PI_3, FRAC_PI_4}; use avian3d::prelude::{ - ComputedCenterOfMass, ExternalForce, Gravity, LinearVelocity, RayCaster, RayHits, - RigidBodyQueryReadOnly, + ComputedCenterOfMass, ExternalForce, ExternalTorque, Gravity, LinearVelocity, RayCaster, + RayHits, RigidBodyQueryReadOnly, }; use bevy::{ - ecs::error::BevyError, + ecs::system::{Populated, Single}, prelude::{ - ButtonInput, Color, Gizmos, GlobalTransform, KeyCode, Quat, Query, Res, ResMut, Result, - Time, Transform, Vec, Vec3, With, Without, + ButtonInput, Color, Gizmos, GlobalTransform, KeyCode, Quat, Res, ResMut, Time, + Transform, Vec, Vec3, With, Without, }, }; @@ -87,17 +87,17 @@ mod systems { } pub(super) fn calculate_lean( - bike_state: Query<(&LinearVelocity, &Transform), With>, - wheels: Query<&GlobalTransform, With>, + bike_state: Single<(&LinearVelocity, &Transform), With>, + wheels: Populated<&GlobalTransform, With>, input: Res, gravity: Res, mut lean: ResMut, - ) -> Result { + ) { let mut wheels = wheels.iter(); - let w1 = wheels.next().ok_or(BevyError::from("oops"))?; - let w2 = wheels.next().ok_or(BevyError::from("oops"))?; + let w1 = wheels.next().unwrap(); + let w2 = wheels.next().unwrap(); let base = (w1.translation() - w2.translation()).length().abs(); - let (velocity, xform) = bike_state.single()?; + let (velocity, xform) = bike_state.into_inner(); let vel = velocity.dot(*xform.forward()); let v_squared = vel.powi(2); let steering_angle = yaw_to_angle(input.yaw); @@ -111,31 +111,37 @@ mod systems { } else { //lean.lean = 0.0; } - Ok(()) } pub(super) fn apply_lean( - mut bike_query: Query<( - &Transform, - &ComputedCenterOfMass, - &mut ExternalForce, - &mut CatControllerState, - )>, - wheels: Query<&WheelState>, + bike_query: Single<(&Transform, &mut ExternalTorque, &mut CatControllerState)>, + wheels: Populated<(&WheelState, &GlobalTransform, &CyberWheel)>, time: Res