use torus for wheels

This commit is contained in:
Joe Ardent 2023-05-08 21:18:26 -07:00
parent 1dee14148d
commit 0745760164
3 changed files with 84 additions and 54 deletions

View File

@ -39,7 +39,7 @@ pub(super) fn spawn_cyberbike(
..Default::default() ..Default::default()
}; };
let mass_properties = ColliderMassProperties::Density(0.9); let mass_properties = ColliderMassProperties::Density(1.5);
let (membership, filter) = BIKE_BODY_COLLISION_GROUP; let (membership, filter) = BIKE_BODY_COLLISION_GROUP;
let bike_collision_group = CollisionGroups::new(membership, filter); let bike_collision_group = CollisionGroups::new(membership, filter);

View File

@ -32,16 +32,16 @@ impl Default for WheelConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
front_forward: 0.8, front_forward: 0.8,
rear_back: 1.1, rear_back: 1.0,
y: -0.1, y: -0.1,
limits: [-0.5, 0.1], limits: [-0.5, 0.1],
stiffness: 50.0, stiffness: 150.0,
damping: 8.0, damping: 8.0,
radius: 0.38, radius: 0.25,
thickness: 0.2, thickness: 0.11,
friction: 1.2, friction: 1.2,
restitution: 0.95, restitution: 0.95,
density: 0.9, density: 0.05,
} }
} }
} }

View File

@ -1,4 +1,4 @@
use bevy::prelude::{shape::UVSphere as Tire, *}; use bevy::prelude::{shape::Torus as Tire, *};
use bevy_rapier3d::prelude::{ use bevy_rapier3d::prelude::{
Ccd, CoefficientCombineRule, Collider, ColliderMassProperties, CollisionGroups, Damping, Ccd, CoefficientCombineRule, Collider, ColliderMassProperties, CollisionGroups, Damping,
ExternalForce, Friction, MultibodyJoint, PrismaticJointBuilder, Restitution, ExternalForce, Friction, MultibodyJoint, PrismaticJointBuilder, Restitution,
@ -17,7 +17,7 @@ pub fn spawn_tires(
let wheels_collision_group = CollisionGroups::new(membership, filter); let wheels_collision_group = CollisionGroups::new(membership, filter);
let wheel_y = conf.y; let wheel_y = conf.y;
let wheel_rad = conf.radius; let wheel_rad = conf.radius;
let _tire_thickness = conf.thickness; let tire_thickness = conf.thickness;
let stiffness = conf.stiffness; let stiffness = conf.stiffness;
let not_sleeping = Sleeping::disabled(); let not_sleeping = Sleeping::disabled();
let ccd = Ccd { enabled: true }; let ccd = Ccd { enabled: true };
@ -25,29 +25,6 @@ pub fn spawn_tires(
let (meshes, materials) = meshterials; let (meshes, materials) = meshterials;
let rake_vec: Vec3 = Vec3::new(0.0, 1.0, 0.57).normalize(); // about 30 degrees of rake 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 { let friction = Friction {
coefficient: conf.friction, coefficient: conf.friction,
combine_rule: CoefficientCombineRule::Average, combine_rule: CoefficientCombineRule::Average,
@ -71,13 +48,71 @@ pub fn spawn_tires(
wheel_poses.push((offset, None)); 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 { for (offset, steering) in wheel_poses {
let wheel_damping = Damping { let wheel_damping = Damping {
linear_damping: 0.8, linear_damping: 0.8,
..Default::default() ..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::<Vec<[f32; 3]>>();
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::<Vec<_>>();
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::<Vec<_>>(),
&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 mass_props = ColliderMassProperties::Density(conf.density);
let damping = conf.damping; let damping = conf.damping;
@ -100,7 +135,7 @@ pub fn spawn_tires(
let axel_parent_entity = if let Some(steering) = steering { let axel_parent_entity = if let Some(steering) = steering {
let neck_builder = 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_joint = MultibodyJoint::new(fork_rb_entity, neck_builder);
let neck = commands let neck = commands
.spawn(RigidBody::Dynamic) .spawn(RigidBody::Dynamic)
@ -115,13 +150,8 @@ pub fn spawn_tires(
let axel_builder = RevoluteJointBuilder::new(Vec3::X); let axel_builder = RevoluteJointBuilder::new(Vec3::X);
let axel_joint = MultibodyJoint::new(axel_parent_entity, axel_builder); 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 commands.spawn(pbr_bundle).insert((
.spawn(pbr_bundle.clone())
.insert((
wheel_collider, wheel_collider,
mass_props, mass_props,
wheel_damping, wheel_damping,
@ -133,9 +163,9 @@ pub fn spawn_tires(
CyberWheel, CyberWheel,
ExternalForce::default(), ExternalForce::default(),
Restitution::new(conf.restitution), Restitution::new(conf.restitution),
SpatialBundle::default(),
TransformInterpolation::default(), TransformInterpolation::default(),
RigidBody::Dynamic, RigidBody::Dynamic,
)) ));
.insert(SpatialBundle::default());
} }
} }