Compare commits
No commits in common. "main" and "torii" have entirely different histories.
|
@ -10,6 +10,3 @@ bevy-inspector-egui = "0.25.1"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
no-mesh = []
|
no-mesh = []
|
||||||
|
|
||||||
[profile.dev.package."*"]
|
|
||||||
opt-level = 3
|
|
||||||
|
|
179
src/bike.rs
179
src/bike.rs
|
@ -8,21 +8,6 @@ use crate::physics::CatControllerState;
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct CyberBikeBody;
|
pub struct CyberBikeBody;
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct CyberWheel;
|
|
||||||
|
|
||||||
// marker for front suspension joint
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct Steering;
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct FrontHub;
|
|
||||||
|
|
||||||
// marker for rear suspension joint
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct Rearing;
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct RearHub;
|
|
||||||
|
|
||||||
fn spawn_bike(
|
fn spawn_bike(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
@ -37,14 +22,13 @@ fn spawn_bike(
|
||||||
let bike = commands
|
let bike = commands
|
||||||
.spawn(SpatialBundle::from_transform(xform))
|
.spawn(SpatialBundle::from_transform(xform))
|
||||||
.insert((
|
.insert((
|
||||||
Name::new("body"),
|
|
||||||
RigidBody::Dynamic,
|
RigidBody::Dynamic,
|
||||||
body_collider,
|
body_collider,
|
||||||
CollisionLayers::from_bits(1, 1),
|
CollisionLayers::from_bits(1, 1),
|
||||||
SleepingDisabled,
|
SleepingDisabled,
|
||||||
CyberBikeBody,
|
CyberBikeBody,
|
||||||
CatControllerState::default(),
|
CatControllerState::default(),
|
||||||
ColliderDensity(20.0),
|
ColliderDensity(0.12),
|
||||||
LinearDamping(0.1),
|
LinearDamping(0.1),
|
||||||
AngularDamping(2.0),
|
AngularDamping(2.0),
|
||||||
LinearVelocity::ZERO,
|
LinearVelocity::ZERO,
|
||||||
|
@ -69,56 +53,28 @@ fn spawn_bike(
|
||||||
spawn_wheels(commands, meshes, materials, xform, bike);
|
spawn_wheels(commands, meshes, materials, xform, bike);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_wheels(
|
#[derive(Component)]
|
||||||
mut commands: Commands,
|
pub struct CyberWheel;
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
||||||
xform: Transform,
|
|
||||||
body: Entity,
|
|
||||||
) {
|
|
||||||
let front_rake = Vec3::new(0.0, -1.0, -0.57).normalize(); // about 30 degrees
|
|
||||||
let front_pos = xform.translation + front_rake;
|
|
||||||
|
|
||||||
let (mesh, collider) = gen_tire();
|
// marker for front suspension joint
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Steering;
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct FrontHub;
|
||||||
|
|
||||||
let front_hub = wheels_helper(
|
// marker for rear suspension joint
|
||||||
&mut commands,
|
#[derive(Component)]
|
||||||
&mut meshes,
|
pub struct Rearing;
|
||||||
&mut materials,
|
#[derive(Component)]
|
||||||
front_pos,
|
pub struct RearHub;
|
||||||
mesh.clone(),
|
|
||||||
collider.clone(),
|
|
||||||
);
|
|
||||||
commands.entity(front_hub).insert(FrontHub);
|
|
||||||
commands.spawn((
|
|
||||||
Steering,
|
|
||||||
FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + front_rake),
|
|
||||||
));
|
|
||||||
|
|
||||||
let rear_rake = Vec3::new(0.0, -1.0, 0.57).normalize();
|
fn helper(
|
||||||
let rear_pos = xform.translation + rear_rake;
|
commands: &mut Commands,
|
||||||
|
meshes: &mut ResMut<Assets<Mesh>>,
|
||||||
let rear_hub = wheels_helper(
|
materials: &mut ResMut<Assets<StandardMaterial>>,
|
||||||
&mut commands,
|
position: Vec3,
|
||||||
&mut meshes,
|
) -> Entity {
|
||||||
&mut materials,
|
let mut tire_mesh: Mesh = Torus::new(0.30, 0.40).into();
|
||||||
rear_pos,
|
|
||||||
mesh,
|
|
||||||
collider,
|
|
||||||
);
|
|
||||||
commands.entity(rear_hub).insert(RearHub);
|
|
||||||
commands.spawn((
|
|
||||||
Rearing,
|
|
||||||
FixedJoint::new(rear_hub, body).with_local_anchor_2(*xform.back() + rear_rake),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
//-************************************************************************
|
|
||||||
// helper fns for the wheels
|
|
||||||
//-************************************************************************
|
|
||||||
|
|
||||||
fn gen_tire() -> (Mesh, Collider) {
|
|
||||||
let mut tire_mesh: Mesh = Torus::new(0.25, 0.40).into();
|
|
||||||
let tire_verts = tire_mesh
|
let tire_verts = tire_mesh
|
||||||
.attribute(Mesh::ATTRIBUTE_POSITION)
|
.attribute(Mesh::ATTRIBUTE_POSITION)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -135,71 +91,76 @@ fn gen_tire() -> (Mesh, Collider) {
|
||||||
.collect::<Vec<[f32; 3]>>();
|
.collect::<Vec<[f32; 3]>>();
|
||||||
tire_mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION);
|
tire_mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION);
|
||||||
tire_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, tire_verts);
|
tire_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, tire_verts);
|
||||||
|
let wheel_material = StandardMaterial {
|
||||||
let collider = Collider::convex_hull_from_mesh(&tire_mesh).unwrap();
|
|
||||||
|
|
||||||
(tire_mesh, collider)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wheels_helper(
|
|
||||||
commands: &mut Commands,
|
|
||||||
meshes: &mut ResMut<Assets<Mesh>>,
|
|
||||||
materials: &mut ResMut<Assets<StandardMaterial>>,
|
|
||||||
position: Vec3,
|
|
||||||
tire_mesh: Mesh,
|
|
||||||
collider: Collider,
|
|
||||||
) -> Entity {
|
|
||||||
let wheel_material = &StandardMaterial {
|
|
||||||
base_color: Color::srgb(0.01, 0.01, 0.01),
|
base_color: Color::srgb(0.01, 0.01, 0.01),
|
||||||
alpha_mode: AlphaMode::Opaque,
|
alpha_mode: AlphaMode::Opaque,
|
||||||
perceptual_roughness: 0.5,
|
perceptual_roughness: 0.5,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
let collider = Collider::convex_decomposition_from_mesh(&tire_mesh).unwrap();
|
||||||
|
let mut tire = None;
|
||||||
|
let tref = &mut tire;
|
||||||
|
|
||||||
let xform = Transform::from_translation(position);
|
let xform = Transform::from_translation(position);
|
||||||
let hub_mesh: Mesh = Sphere::new(0.1).into();
|
|
||||||
|
|
||||||
let hub = commands
|
let hub = commands
|
||||||
.spawn((
|
.spawn((
|
||||||
Name::new("hub"),
|
SpatialBundle::from_transform(xform),
|
||||||
RigidBody::Dynamic,
|
RigidBody::Dynamic,
|
||||||
MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 200.0),
|
MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 0.01),
|
||||||
PbrBundle {
|
|
||||||
mesh: meshes.add(hub_mesh),
|
|
||||||
material: materials.add(wheel_material.clone()),
|
|
||||||
transform: xform,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
))
|
))
|
||||||
.id();
|
.with_children(move |parent| {
|
||||||
|
let mesh: Mesh = Sphere::new(0.1).into();
|
||||||
let tire = commands
|
parent.spawn(PbrBundle {
|
||||||
.spawn((
|
mesh: meshes.add(mesh),
|
||||||
Name::new("tire"),
|
|
||||||
PbrBundle {
|
|
||||||
mesh: meshes.add(tire_mesh),
|
|
||||||
material: materials.add(wheel_material.clone()),
|
material: materials.add(wheel_material.clone()),
|
||||||
transform: xform,
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
});
|
||||||
CyberWheel,
|
let tire_id = parent
|
||||||
RigidBody::Dynamic,
|
.spawn((
|
||||||
collider,
|
CyberWheel,
|
||||||
Friction::new(0.9).with_dynamic_coefficient(0.6),
|
RigidBody::Dynamic,
|
||||||
Restitution::new(0.1),
|
collider.clone(),
|
||||||
ColliderDensity(30.0),
|
ColliderDensity(0.05),
|
||||||
CollisionLayers::from_bits(2, 2),
|
CollisionLayers::from_bits(2, 2),
|
||||||
ExternalTorque::ZERO.with_persistence(false),
|
ExternalTorque::ZERO.with_persistence(false),
|
||||||
CollisionMargin(0.05),
|
))
|
||||||
SweptCcd::NON_LINEAR,
|
.id();
|
||||||
))
|
let _ = tref.insert(tire_id);
|
||||||
|
})
|
||||||
.id();
|
.id();
|
||||||
|
|
||||||
// connect hubs and tires to make wheels
|
// connect hubs and tires to make wheels
|
||||||
commands.spawn(RevoluteJoint::new(hub, tire).with_aligned_axis(Vec3::X));
|
commands.spawn(RevoluteJoint::new(hub, tire.unwrap()).with_aligned_axis(Vec3::X));
|
||||||
hub
|
hub
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn spawn_wheels(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
|
xform: Transform,
|
||||||
|
body: Entity,
|
||||||
|
) {
|
||||||
|
let rake_vec = Vec3::new(0.0, -1.0, -0.57).normalize(); // about 30 degrees
|
||||||
|
let front_pos = xform.translation + rake_vec;
|
||||||
|
|
||||||
|
let front_hub = helper(&mut commands, &mut meshes, &mut materials, front_pos);
|
||||||
|
commands.entity(front_hub).insert(FrontHub);
|
||||||
|
commands.spawn((
|
||||||
|
Steering,
|
||||||
|
FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + rake_vec),
|
||||||
|
));
|
||||||
|
|
||||||
|
let rear_pos = xform.translation + Vec3::new(0.0, -1.0, 0.57).normalize();
|
||||||
|
let rear_hub = helper(&mut commands, &mut meshes, &mut materials, rear_pos);
|
||||||
|
commands.entity(rear_hub).insert(RearHub);
|
||||||
|
commands.spawn((
|
||||||
|
Rearing,
|
||||||
|
FixedJoint::new(rear_hub, body).with_local_anchor_2(*xform.back() + rake_vec.y),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
pub struct CyberBikePlugin;
|
pub struct CyberBikePlugin;
|
||||||
|
|
||||||
impl Plugin for CyberBikePlugin {
|
impl Plugin for CyberBikePlugin {
|
||||||
|
|
27
src/main.rs
27
src/main.rs
|
@ -41,23 +41,16 @@ fn ground_and_light(
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
) {
|
) {
|
||||||
commands
|
commands.spawn((
|
||||||
.spawn((
|
RigidBody::Static,
|
||||||
SpatialBundle::default(),
|
Collider::cuboid(50.0, 0.5, 50.0),
|
||||||
RigidBody::Static,
|
PbrBundle {
|
||||||
Collider::cuboid(50.0, 0.5, 50.0),
|
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
|
||||||
CollisionMargin(0.1),
|
material: materials.add(Color::from(SILVER)),
|
||||||
ColliderDensity(1000.0),
|
transform: Transform::from_xyz(0.0, -3., 0.0),
|
||||||
Friction::new(0.9),
|
..default()
|
||||||
))
|
},
|
||||||
.with_children(|p| {
|
));
|
||||||
p.spawn(PbrBundle {
|
|
||||||
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
|
|
||||||
material: materials.add(Color::from(SILVER)),
|
|
||||||
transform: Transform::from_xyz(0.0, 0.4, 0.0),
|
|
||||||
..default()
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
commands.spawn(PointLightBundle {
|
commands.spawn(PointLightBundle {
|
||||||
point_light: PointLight {
|
point_light: PointLight {
|
||||||
|
|
|
@ -18,9 +18,9 @@ pub struct CatControllerSettings {
|
||||||
impl Default for CatControllerSettings {
|
impl Default for CatControllerSettings {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
kp: 1200.0,
|
kp: 10.0,
|
||||||
kd: 10.0,
|
kd: 1.2,
|
||||||
ki: 50.0,
|
ki: 0.2,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,7 +174,6 @@ impl Plugin for CyberPhysicsPlugin {
|
||||||
.init_resource::<CyberLean>()
|
.init_resource::<CyberLean>()
|
||||||
.register_type::<CyberLean>()
|
.register_type::<CyberLean>()
|
||||||
.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()))
|
.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()))
|
||||||
.insert_resource(SubstepCount(12))
|
|
||||||
.add_systems(Update, (apply_lean, calculate_lean));
|
.add_systems(Update, (apply_lean, calculate_lean));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue