From 421f34fab77aa52301f0b200c22821e41ccb70da Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Wed, 15 Feb 2023 11:38:36 -0800 Subject: [PATCH] Bring in wheels from rolling2, and shorten the body collider. --- src/action/systems.rs | 6 +- src/bike/body.rs | 4 +- src/bike/wheels.rs | 139 +++++++++++++++--------------------------- 3 files changed, 54 insertions(+), 95 deletions(-) diff --git a/src/action/systems.rs b/src/action/systems.rs index 02bc9e1..c12b362 100644 --- a/src/action/systems.rs +++ b/src/action/systems.rs @@ -108,9 +108,9 @@ pub(super) fn input_forces( // steering let angle = input.yaw * (PI / 2.0); - let mut steering = steering_query.single_mut(); - steering.rotation = Quat::from_axis_angle(Vec3::Y, angle + 90.0f32.to_radians()); - //dbg!(steering.rotation); + //let mut steering = steering_query.single_mut(); + //steering.rotation = Quat::from_axis_angle(Vec3::Y, angle + + // 90.0f32.to_radians()); dbg!(steering.rotation); } /// Don't let the wheels get stuck underneat the planet diff --git a/src/bike/body.rs b/src/bike/body.rs index efe38b7..cc70859 100644 --- a/src/bike/body.rs +++ b/src/bike/body.rs @@ -51,7 +51,7 @@ pub(super) fn spawn_cyberbike( .spawn(RigidBody::Dynamic) .insert(spatialbundle) .insert(( - Collider::capsule(Vec3::new(0.0, 0.0, -1.0), Vec3::new(0.0, 0.0, 1.0), 0.50), + Collider::capsule(Vec3::new(0.0, 0.0, -0.7), Vec3::new(0.0, 0.0, 1.0), 0.50), bike_collision_group, mass_properties, damping, @@ -77,5 +77,5 @@ pub(super) fn spawn_cyberbike( .insert(CatControllerState::default()) .id(); - spawn_tires(&mut commands, bike, xform, &wheel_conf, &mut meshterials); + spawn_tires(&mut commands, &xform, bike, &wheel_conf, &mut meshterials); } diff --git a/src/bike/wheels.rs b/src/bike/wheels.rs index 50a031e..ebf8120 100644 --- a/src/bike/wheels.rs +++ b/src/bike/wheels.rs @@ -1,22 +1,16 @@ -use bevy::prelude::{ - shape::Capsule as Tire, AlphaMode, BuildChildren, Color, Commands, Entity, Mesh, PbrBundle, - Quat, SpatialBundle, StandardMaterial, Transform, Vec3, -}; -use bevy_rapier3d::{ - prelude::{ - Ccd, CoefficientCombineRule, Collider, ColliderMassProperties, CollisionGroups, Damping, - ExternalForce, Friction, LockedAxes, MultibodyJoint, PrismaticJointBuilder, Restitution, - RevoluteJointBuilder, RigidBody, Sleeping, TransformInterpolation, - }, - render::ColliderDebugColor, +use bevy::prelude::{shape::UVSphere as Tire, *}; +use bevy_rapier3d::prelude::{ + Ccd, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction, + MultibodyJoint, PrismaticJointBuilder, Restitution, RigidBody, Sleeping, + TransformInterpolation, }; -use super::{CyberSteering, CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP}; +use super::{CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP}; pub fn spawn_tires( commands: &mut Commands, + xform: &Transform, bike: Entity, - xform: Transform, conf: &WheelConfig, meshterials: &mut Meshterial, ) { @@ -53,13 +47,8 @@ pub fn spawn_tires( }; let friction = Friction { - coefficient: 1.0, - combine_rule: CoefficientCombineRule::Min, - }; - - let restitution = Restitution { - coefficient: 0.8, - combine_rule: CoefficientCombineRule::Average, + coefficient: 0.0, + ..Default::default() }; let mut wheel_poses = Vec::with_capacity(2); @@ -69,7 +58,7 @@ pub fn spawn_tires( let wheel_x = 0.0; let wheel_z = -conf.front_forward; let offset = Vec3::new(wheel_x, wheel_y, wheel_z); - wheel_poses.push((offset, Some(CyberSteering))); + wheel_poses.push(offset); } // rear @@ -77,89 +66,59 @@ pub fn spawn_tires( let wheel_x = 0.0; let wheel_z = conf.rear_back; let offset = Vec3::new(wheel_x, wheel_y, wheel_z); - wheel_poses.push((offset, None)); + wheel_poses.push(offset); } - for (offset, steering) in wheel_poses { + for offset in wheel_poses { + let trans = xform.translation + offset; + let wheel_pos_in_world = Transform::from_rotation(xform.rotation).with_translation(trans); let wheel_damping = Damping { linear_damping: 0.8, ..Default::default() }; - let wheel_collider = Collider::capsule(-Vec3::Y, Vec3::Y, 0.3); + let wheel_collider = Collider::ball(wheel_rad); let mass_props = ColliderMassProperties::Density(0.1); let damping = conf.damping; let prismatic = PrismaticJointBuilder::new(Vec3::Y) .local_anchor1(offset) .limits(limits) - .motor_position(limits[0], stiffness, damping); + .motor_position(-0.1, stiffness, damping); + let joint = MultibodyJoint::new(bike, prismatic); - let fork = commands - .spawn(SpatialBundle { - transform: Transform::from_translation(offset), - ..Default::default() - }) - .set_parent(bike) - .id(); + let spatial_bundle = SpatialBundle { + transform: wheel_pos_in_world, + ..Default::default() + }; - let suspension = MultibodyJoint::new(fork, prismatic); - let sentity = commands + let tire_spundle = SpatialBundle { + transform: Transform::IDENTITY, + ..Default::default() + }; + + commands .spawn(RigidBody::Dynamic) - .insert(suspension) - .insert(SpatialBundle { - transform: Transform::from_translation(offset), - ..Default::default() - }) - .set_parent(bike) - .id(); - - if let Some(steering) = steering { - commands.entity(sentity).insert(steering); - } - - let revolute = RevoluteJointBuilder::new(Vec3::Y); - let axel = MultibodyJoint::new(sentity, revolute); - let wheel_bundle = ( - wheel_collider, - mass_props, - wheel_damping, - ccd, - wheels_collision_group, - friction, - restitution, - CyberWheel, - ExternalForce::default(), - not_sleeping, - ColliderDebugColor(Color::Rgba { - red: 0.1, - green: 0.1, - blue: 0.1, - alpha: 0.5, - }), //axel, - ); - - let _wentity = commands - .spawn(RigidBody::Dynamic) - .insert(SpatialBundle { - // transform: Transform::from_rotation(xform.rotation) - // .with_translation(xform.translation) - transform: Transform::from_rotation(Quat::from_axis_angle( - Vec3::Z, - 90.0f32.to_radians(), - )), - ..Default::default() - }) - .insert(wheel_bundle) - // .with_children(|wheel| { - // wheel - // .spawn(SpatialBundle::default()) - // .insert(pbr_bundle.clone()) - // .insert(TransformInterpolation { - // start: None, - // end: None, - // }); - // }) - .set_parent(sentity) - .id(); + .insert(spatial_bundle) + .insert(( + wheel_collider, + mass_props, + wheel_damping, + ccd, + not_sleeping, + joint, + wheels_collision_group, + friction, + CyberWheel, + ExternalForce::default(), + Restitution::new(0.2), + )) + .with_children(|wheel| { + wheel.spawn(tire_spundle).insert(pbr_bundle.clone()).insert( + TransformInterpolation { + start: None, + end: None, + }, + ); + }); } }