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()
};
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);

View file

@ -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,
}
}
}

View file

@ -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::<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 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,13 +150,8 @@ 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((
commands.spawn(pbr_bundle).insert((
wheel_collider,
mass_props,
wheel_damping,
@ -133,9 +163,9 @@ pub fn spawn_tires(
CyberWheel,
ExternalForce::default(),
Restitution::new(conf.restitution),
SpatialBundle::default(),
TransformInterpolation::default(),
RigidBody::Dynamic,
))
.insert(SpatialBundle::default());
));
}
}