From 0745760164a72af885108497f424fdabb86c20b0 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 8 May 2023 21:18:26 -0700 Subject: [PATCH] use torus for wheels --- src/bike/body.rs | 2 +- src/bike/components.rs | 10 ++-- src/bike/wheels.rs | 126 +++++++++++++++++++++++++---------------- 3 files changed, 84 insertions(+), 54 deletions(-) diff --git a/src/bike/body.rs b/src/bike/body.rs index 8ef8191..c57e560 100644 --- a/src/bike/body.rs +++ b/src/bike/body.rs @@ -39,7 +39,7 @@ pub(super) fn spawn_cyberbike( ..Default::default() }; - let mass_properties = ColliderMassProperties::Density(0.9); + let mass_properties = ColliderMassProperties::Density(1.5); let (membership, filter) = BIKE_BODY_COLLISION_GROUP; let bike_collision_group = CollisionGroups::new(membership, filter); diff --git a/src/bike/components.rs b/src/bike/components.rs index 8619d07..7d28655 100644 --- a/src/bike/components.rs +++ b/src/bike/components.rs @@ -32,16 +32,16 @@ impl Default for WheelConfig { fn default() -> Self { Self { front_forward: 0.8, - rear_back: 1.1, + rear_back: 1.0, y: -0.1, limits: [-0.5, 0.1], - stiffness: 50.0, + stiffness: 150.0, damping: 8.0, - radius: 0.38, - thickness: 0.2, + radius: 0.25, + thickness: 0.11, friction: 1.2, restitution: 0.95, - density: 0.9, + density: 0.05, } } } diff --git a/src/bike/wheels.rs b/src/bike/wheels.rs index 8ce4fc7..9b58d49 100644 --- a/src/bike/wheels.rs +++ b/src/bike/wheels.rs @@ -1,4 +1,4 @@ -use bevy::prelude::{shape::UVSphere as Tire, *}; +use bevy::prelude::{shape::Torus as Tire, *}; use bevy_rapier3d::prelude::{ Ccd, CoefficientCombineRule, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction, MultibodyJoint, PrismaticJointBuilder, Restitution, @@ -17,7 +17,7 @@ pub fn spawn_tires( let wheels_collision_group = CollisionGroups::new(membership, filter); let wheel_y = conf.y; let wheel_rad = conf.radius; - let _tire_thickness = conf.thickness; + let tire_thickness = conf.thickness; let stiffness = conf.stiffness; let not_sleeping = Sleeping::disabled(); let ccd = Ccd { enabled: true }; @@ -25,29 +25,6 @@ pub fn spawn_tires( let (meshes, materials) = meshterials; let rake_vec: Vec3 = Vec3::new(0.0, 1.0, 0.57).normalize(); // about 30 degrees of rake - let tire = Tire { - radius: wheel_rad, - ..Default::default() - }; - - let material = StandardMaterial { - base_color: Color::Rgba { - red: 0.01, - green: 0.01, - blue: 0.01, - alpha: 1.0, - }, - alpha_mode: AlphaMode::Opaque, - perceptual_roughness: 0.5, - ..Default::default() - }; - - let pbr_bundle = PbrBundle { - material: materials.add(material), - mesh: meshes.add(Mesh::from(tire)), - ..Default::default() - }; - let friction = Friction { coefficient: conf.friction, combine_rule: CoefficientCombineRule::Average, @@ -71,13 +48,71 @@ pub fn spawn_tires( wheel_poses.push((offset, None)); } + let material = StandardMaterial { + base_color: Color::Rgba { + red: 0.01, + green: 0.01, + blue: 0.01, + alpha: 1.0, + }, + alpha_mode: AlphaMode::Opaque, + perceptual_roughness: 0.5, + ..Default::default() + }; + for (offset, steering) in wheel_poses { let wheel_damping = Damping { linear_damping: 0.8, ..Default::default() }; + let tire = Tire { + radius: wheel_rad, + ring_radius: tire_thickness, + ..Default::default() + }; - let wheel_collider = Collider::ball(wheel_rad); + let mut mesh = Mesh::from(tire); + let tire_verts = mesh + .attribute(Mesh::ATTRIBUTE_POSITION) + .unwrap() + .as_float3() + .unwrap() + .iter() + .map(|v| { + // + let v = Vec3::from_array(*v); + let m = Mat3::from_rotation_z(90.0f32.to_radians()); + let p = m.mul_vec3(v); + p.to_array() + }) + .collect::>(); + mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION); + mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, tire_verts); + + let mut idxs = Vec::new(); + let indices = mesh.indices().unwrap().iter().collect::>(); + for idx in indices.as_slice().chunks_exact(3) { + idxs.push([idx[0] as u32, idx[1] as u32, idx[2] as u32]); + } + let wheel_collider = Collider::convex_decomposition( + &mesh + .attribute(Mesh::ATTRIBUTE_POSITION) + .unwrap() + .as_float3() + .unwrap() + .iter() + .map(|v| Vec3::from_array(*v)) + .collect::>(), + &idxs, + ); + + let pbr_bundle = PbrBundle { + material: materials.add(material.clone()), + mesh: meshes.add(mesh), + ..Default::default() + }; + + //let wheel_collider = Collider::from_bevy_mesh(&mesh, computed_shape); let mass_props = ColliderMassProperties::Density(conf.density); let damping = conf.damping; @@ -100,7 +135,7 @@ pub fn spawn_tires( let axel_parent_entity = if let Some(steering) = steering { let neck_builder = - RevoluteJointBuilder::new(rake_vec).local_anchor1(Vec3::new(0.0, -0.08, 0.1)); // this adds another 0.1m of trail + RevoluteJointBuilder::new(rake_vec).local_anchor1(Vec3::new(0.0, 0.0, 0.1)); // this adds another 0.1m of trail let neck_joint = MultibodyJoint::new(fork_rb_entity, neck_builder); let neck = commands .spawn(RigidBody::Dynamic) @@ -115,27 +150,22 @@ pub fn spawn_tires( let axel_builder = RevoluteJointBuilder::new(Vec3::X); let axel_joint = MultibodyJoint::new(axel_parent_entity, axel_builder); - // re-orient the joint so that the wheel is correctly oriented - //let real_axel = *axel_joint.data.set_local_axis1(Vec3::X); - //axel_joint.data = real_axel; - commands - .spawn(pbr_bundle.clone()) - .insert(( - wheel_collider, - mass_props, - wheel_damping, - ccd, - not_sleeping, - axel_joint, - wheels_collision_group, - friction, - CyberWheel, - ExternalForce::default(), - Restitution::new(conf.restitution), - TransformInterpolation::default(), - RigidBody::Dynamic, - )) - .insert(SpatialBundle::default()); + commands.spawn(pbr_bundle).insert(( + wheel_collider, + mass_props, + wheel_damping, + ccd, + not_sleeping, + axel_joint, + wheels_collision_group, + friction, + CyberWheel, + ExternalForce::default(), + Restitution::new(conf.restitution), + SpatialBundle::default(), + TransformInterpolation::default(), + RigidBody::Dynamic, + )); } }