tweak; apply suspension force at contact point, not caster origin

This commit is contained in:
Joe Ardent 2025-05-01 15:45:12 -07:00
parent bcd6fd8561
commit 52cac4a3eb
2 changed files with 29 additions and 34 deletions

View file

@ -92,6 +92,19 @@ fn spawn_bike(
let body_collider = let body_collider =
Collider::capsule_endpoints(0.5, Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8)); Collider::capsule_endpoints(0.5, Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8));
let pbr_bundle = {
let color = Color::srgb(0.7, 0.05, 0.7);
let mut xform = Transform::default();
xform.rotate_x(FRAC_PI_2);
(
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.45))),
xform,
Visibility::Visible,
MeshMaterial3d(materials.add(color)),
TransformInterpolation,
)
};
let mut body = commands.spawn((xform, Visibility::default())); let mut body = commands.spawn((xform, Visibility::default()));
body.insert(( body.insert((
Name::new("body"), Name::new("body"),
@ -108,25 +121,12 @@ fn spawn_bike(
ExternalTorque::ZERO.with_persistence(false), ExternalTorque::ZERO.with_persistence(false),
)); ));
let pbr_bundle = { body.insert(spawn_children(body.id(), meshes, materials))
let color = Color::srgb(0.7, 0.05, 0.7); .with_child(pbr_bundle);
let mut xform = Transform::default();
xform.rotate_x(FRAC_PI_2);
(
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.45))),
xform,
Visibility::Visible,
MeshMaterial3d(materials.add(color)),
TransformInterpolation,
)
};
let parent = body.id();
body.insert(spawn_children(parent, pbr_bundle, meshes, materials));
} }
fn spawn_children( fn spawn_children(
parent: Entity, parent: Entity,
body: impl Bundle,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) -> impl Bundle { ) -> impl Bundle {
@ -147,7 +147,6 @@ fn spawn_children(
let rear_wheel_pos = REAR_ATTACH + (rear_rake * REST_DISTANCE); let rear_wheel_pos = REAR_ATTACH + (rear_rake * REST_DISTANCE);
children![ children![
body,
( (
RayCaster::new(FRONT_ATTACH, Dir3::new_unchecked(front_rake)) RayCaster::new(FRONT_ATTACH, Dir3::new_unchecked(front_rake))
.with_max_hits(1) .with_max_hits(1)

View file

@ -20,9 +20,9 @@ pub struct CatControllerSettings {
impl Default for CatControllerSettings { impl Default for CatControllerSettings {
fn default() -> Self { fn default() -> Self {
Self { Self {
kp: 55.0, kp: 60.0,
kd: 20.0, kd: 30.0,
ki: 12.5, ki: 0.0,
} }
} }
} }
@ -260,11 +260,7 @@ mod systems {
hit_point + force, hit_point + force,
Color::linear_rgb(1., 0.5, 0.2), Color::linear_rgb(1., 0.5, 0.2),
); );
bike_forces.apply_force_at_point( bike_forces.apply_force_at_point(force, hit_point, bike_xform.translation + com.0);
force,
caster.global_origin(),
bike_xform.translation + com.0,
);
} else { } else {
xform.translation = config.attach + (caster.direction.as_vec3() * config.rest_dist); xform.translation = config.attach + (caster.direction.as_vec3() * config.rest_dist);
state.reset(); state.reset();
@ -272,7 +268,7 @@ mod systems {
} }
} }
pub(super) fn steering( pub(super) fn wheel_action(
bike_query: Single< bike_query: Single<
( (
&Transform, &Transform,
@ -291,7 +287,7 @@ mod systems {
let bike_vel = bike_vel.0; let bike_vel = bike_vel.0;
let dt = time.delta().as_secs_f32(); let dt = time.delta().as_secs_f32();
let max_thrust = 1000.0; let max_thrust = 500.0;
let yaw_angle = -yaw_to_angle(input.yaw); let yaw_angle = -yaw_to_angle(input.yaw);
for (mut state, config, wheel) in wheels.iter_mut() { for (mut state, config, wheel) in wheels.iter_mut() {
@ -304,8 +300,8 @@ mod systems {
let forward = normal.cross(*bike_xform.right()); let forward = normal.cross(*bike_xform.right());
let thrust_mag = input.throttle * max_thrust * dt; let thrust_mag = input.throttle * max_thrust * dt;
let (thrust_dir, thrust_force) = match wheel { let (thrust_dir, thrust_force) = match wheel {
CyberWheel::Rear => (forward, thrust_mag * 0.5), CyberWheel::Rear => (forward, thrust_mag),
CyberWheel::Front => (rot * forward, thrust_mag), CyberWheel::Front => (rot * forward, thrust_mag * 0.1),
}; };
let thrust = thrust_force * thrust_dir; let thrust = thrust_force * thrust_dir;
@ -368,11 +364,11 @@ mod systems {
let dt = time.delta_secs(); let dt = time.delta_secs();
let speed = vel.length(); let speed = vel.length_squared();
let dspeed = speed.powf(1.2) * 0.01; let dspeed = speed * 0.0000001;
let dir = vel.normalize_or_zero(); let dir = vel.normalize_or_zero();
let drag = -dspeed * dt * DRAG_COEFF * dir; let drag = -dspeed * dt * DRAG_COEFF * dir;
if speed > 1.0 { if speed > 0.1 {
force.apply_force_at_point( force.apply_force_at_point(
drag, drag,
xform.translation - (0.2 * *xform.down()), xform.translation - (0.2 * *xform.down()),
@ -380,7 +376,7 @@ mod systems {
); );
} }
bevy::log::debug!("speed: {}, drag force: {}", speed, drag.length()); bevy::log::debug!("speed: {}, drag force: {}", vel.length(), drag.length());
gizmos.arrow( gizmos.arrow(
xform.translation, xform.translation,
@ -427,7 +423,7 @@ mod systems {
} }
} }
use systems::{apply_lean, calculate_lean, drag, steering, suspension, tweak}; use systems::{apply_lean, calculate_lean, drag, suspension, tweak, wheel_action};
pub struct CyberPhysicsPlugin; pub struct CyberPhysicsPlugin;
@ -442,7 +438,7 @@ impl Plugin for CyberPhysicsPlugin {
}) })
.add_systems( .add_systems(
FixedUpdate, FixedUpdate,
(calculate_lean, apply_lean, suspension, steering, drag).chain(), (calculate_lean, apply_lean, suspension, wheel_action, drag).chain(),
) )
.add_systems(Update, tweak); .add_systems(Update, tweak);
} }