clean up wheel stuff

This commit is contained in:
Joe Ardent 2023-05-08 21:52:45 -07:00
parent 0745760164
commit c676c93637
3 changed files with 75 additions and 69 deletions

View file

@ -7,7 +7,7 @@ use bevy_rapier3d::prelude::{
ReadMassProperties, Restitution, RigidBody, Sleeping, TransformInterpolation, Velocity,
};
use super::{spawn_tires, CyberBikeBody, Meshterial, WheelConfig, BIKE_BODY_COLLISION_GROUP};
use super::{spawn_wheels, CyberBikeBody, Meshterial, WheelConfig, BIKE_BODY_COLLISION_GROUP};
use crate::{action::CatControllerState, planet::PLANET_RADIUS};
pub(super) fn spawn_cyberbike(
@ -81,5 +81,5 @@ pub(super) fn spawn_cyberbike(
.insert(CatControllerState::default())
.id();
spawn_tires(&mut commands, bike, &wheel_conf, &mut meshterials);
spawn_wheels(&mut commands, bike, &wheel_conf, &mut meshterials);
}

View file

@ -8,7 +8,7 @@ use bevy::prelude::{
use bevy_rapier3d::prelude::Group;
pub(crate) use self::components::*;
use self::{body::spawn_cyberbike, wheels::spawn_tires};
use self::{body::spawn_cyberbike, wheels::spawn_wheels};
pub const BIKE_BODY_COLLISION_GROUP: (Group, Group) = (Group::GROUP_1, Group::GROUP_1);
pub const BIKE_WHEEL_COLLISION_GROUP: (Group, Group) = (Group::GROUP_10, Group::GROUP_10);

View file

@ -7,7 +7,7 @@ use bevy_rapier3d::prelude::{
use super::{CyberSteering, CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP};
pub fn spawn_tires(
pub fn spawn_wheels(
commands: &mut Commands,
bike: Entity,
conf: &WheelConfig,
@ -16,8 +16,6 @@ pub fn spawn_tires(
let (membership, filter) = BIKE_WHEEL_COLLISION_GROUP;
let wheels_collision_group = CollisionGroups::new(membership, filter);
let wheel_y = conf.y;
let wheel_rad = conf.radius;
let tire_thickness = conf.thickness;
let stiffness = conf.stiffness;
let not_sleeping = Sleeping::disabled();
let ccd = Ccd { enabled: true };
@ -48,88 +46,44 @@ 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,
let (mesh, collider) = gen_tires(conf);
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 tire = Tire {
radius: wheel_rad,
ring_radius: tire_thickness,
..Default::default()
};
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()),
material: materials.add(material),
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;
let suspension_damping = conf.damping;
let prismatic_axis = if steering.is_some() {
let suspension_axis = if steering.is_some() {
rake_vec
} else {
Vec3::Y
};
let prismatic_builder = PrismaticJointBuilder::new(prismatic_axis)
let suspension_joint_builder = PrismaticJointBuilder::new(suspension_axis)
.local_anchor1(offset)
.limits(limits)
.motor_position(limits[0], stiffness, damping);
let prismatic_joint = MultibodyJoint::new(bike, prismatic_builder);
.motor_position(limits[0], stiffness, suspension_damping);
let suspension_joint = MultibodyJoint::new(bike, suspension_joint_builder);
let fork_rb_entity = commands
.spawn(RigidBody::Dynamic)
.insert(prismatic_joint)
.insert(suspension_joint)
.insert(not_sleeping)
.id();
@ -150,9 +104,13 @@ pub fn spawn_tires(
let axel_builder = RevoluteJointBuilder::new(Vec3::X);
let axel_joint = MultibodyJoint::new(axel_parent_entity, axel_builder);
let wheel_damping = Damping {
linear_damping: 0.8,
..Default::default()
};
commands.spawn(pbr_bundle).insert((
wheel_collider,
collider,
mass_props,
wheel_damping,
ccd,
@ -169,3 +127,51 @@ pub fn spawn_tires(
));
}
}
// do mesh shit
fn gen_tires(conf: &WheelConfig) -> (Mesh, Collider) {
let wheel_rad = conf.radius;
let tire_thickness = conf.thickness;
let tire = Tire {
radius: wheel_rad,
ring_radius: tire_thickness,
..Default::default()
};
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,
);
(mesh, wheel_collider)
}