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, 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}; use crate::{action::CatControllerState, planet::PLANET_RADIUS};
pub(super) fn spawn_cyberbike( pub(super) fn spawn_cyberbike(
@ -81,5 +81,5 @@ pub(super) fn spawn_cyberbike(
.insert(CatControllerState::default()) .insert(CatControllerState::default())
.id(); .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; use bevy_rapier3d::prelude::Group;
pub(crate) use self::components::*; 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_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); 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}; use super::{CyberSteering, CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP};
pub fn spawn_tires( pub fn spawn_wheels(
commands: &mut Commands, commands: &mut Commands,
bike: Entity, bike: Entity,
conf: &WheelConfig, conf: &WheelConfig,
@ -16,8 +16,6 @@ pub fn spawn_tires(
let (membership, filter) = BIKE_WHEEL_COLLISION_GROUP; let (membership, filter) = BIKE_WHEEL_COLLISION_GROUP;
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 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 };
@ -48,6 +46,9 @@ pub fn spawn_tires(
wheel_poses.push((offset, None)); wheel_poses.push((offset, None));
} }
for (offset, steering) in wheel_poses {
let (mesh, collider) = gen_tires(conf);
let material = StandardMaterial { let material = StandardMaterial {
base_color: Color::Rgba { base_color: Color::Rgba {
red: 0.01, red: 0.01,
@ -60,11 +61,77 @@ pub fn spawn_tires(
..Default::default() ..Default::default()
}; };
for (offset, steering) in wheel_poses { let pbr_bundle = PbrBundle {
material: materials.add(material),
mesh: meshes.add(mesh),
..Default::default()
};
let mass_props = ColliderMassProperties::Density(conf.density);
let suspension_damping = conf.damping;
let suspension_axis = if steering.is_some() {
rake_vec
} else {
Vec3::Y
};
let suspension_joint_builder = PrismaticJointBuilder::new(suspension_axis)
.local_anchor1(offset)
.limits(limits)
.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(suspension_joint)
.insert(not_sleeping)
.id();
let axel_parent_entity = if let Some(steering) = steering {
let neck_builder =
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)
.insert(neck_joint)
.insert(steering)
.insert(not_sleeping)
.id();
neck
} else {
fork_rb_entity
};
let axel_builder = RevoluteJointBuilder::new(Vec3::X);
let axel_joint = MultibodyJoint::new(axel_parent_entity, axel_builder);
let wheel_damping = Damping { let wheel_damping = Damping {
linear_damping: 0.8, linear_damping: 0.8,
..Default::default() ..Default::default()
}; };
commands.spawn(pbr_bundle).insert((
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,
));
}
}
// do mesh shit
fn gen_tires(conf: &WheelConfig) -> (Mesh, Collider) {
let wheel_rad = conf.radius;
let tire_thickness = conf.thickness;
let tire = Tire { let tire = Tire {
radius: wheel_rad, radius: wheel_rad,
ring_radius: tire_thickness, ring_radius: tire_thickness,
@ -106,66 +173,5 @@ pub fn spawn_tires(
&idxs, &idxs,
); );
let pbr_bundle = PbrBundle { (mesh, wheel_collider)
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;
let prismatic_axis = if steering.is_some() {
rake_vec
} else {
Vec3::Y
};
let prismatic_builder = PrismaticJointBuilder::new(prismatic_axis)
.local_anchor1(offset)
.limits(limits)
.motor_position(limits[0], stiffness, damping);
let prismatic_joint = MultibodyJoint::new(bike, prismatic_builder);
let fork_rb_entity = commands
.spawn(RigidBody::Dynamic)
.insert(prismatic_joint)
.insert(not_sleeping)
.id();
let axel_parent_entity = if let Some(steering) = steering {
let neck_builder =
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)
.insert(neck_joint)
.insert(steering)
.insert(not_sleeping)
.id();
neck
} else {
fork_rb_entity
};
let axel_builder = RevoluteJointBuilder::new(Vec3::X);
let axel_joint = MultibodyJoint::new(axel_parent_entity, axel_builder);
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,
));
}
} }