diff --git a/src/bike.rs b/src/bike.rs index a113d61..acf811a 100644 --- a/src/bike.rs +++ b/src/bike.rs @@ -13,7 +13,7 @@ fn spawn_bike( mut meshes: ResMut>, mut materials: ResMut>, ) { - let xform = Transform::from_xyz(4.0, 4.0, 0.0); + let xform = Transform::from_xyz(3.0, 4.0, 0.0); let body_collider = Collider::capsule_endpoints(0.5, Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8)); @@ -37,7 +37,7 @@ fn spawn_bike( .with_children(|builder| { let color = Color::srgb(0.7, 0.05, 0.7); let mut rotation = Transform::default(); // Transform::from_rotation(Quat::from_rotation_y(FRAC_PI_2)); - rotation.rotate_x(FRAC_PI_2); + rotation.rotate_y(FRAC_PI_2); let pbr_bundle = PbrBundle { mesh: meshes.add(Capsule3d::new(0.5, 1.45)), transform: rotation, @@ -66,16 +66,13 @@ pub struct Rearing; #[derive(Component)] pub struct RearHub; -fn spawn_wheels( - mut commands: Commands, - mut meshes: ResMut>, - mut materials: ResMut>, - xform: Transform, - body: Entity, +fn helper( + commands: &mut Commands, + trans: Vec3, + rot: Quat, + meshes: &mut ResMut>, + materials: &mut ResMut>, ) { - let rake_vec = Vec3::new(0.0, -1.0, 0.57).normalize(); // about 30 degrees - let front_pos = xform.translation + *xform.forward() + rake_vec; - let wheel_mesh: Mesh = Torus::new(0.30, 0.40).into(); let wheel_material = StandardMaterial { base_color: Color::srgb(0.01, 0.01, 0.01), @@ -85,21 +82,27 @@ fn spawn_wheels( }; let collider = Collider::convex_decomposition_from_mesh(&wheel_mesh).unwrap(); - let front_hub = commands + let hub = commands .spawn(( FrontHub, - SpatialBundle::from_transform(Transform::from_translation(front_pos)), - RigidBody::Dynamic, - MassPropertiesBundle::new_computed(&Collider::sphere(0.01), 1.0), + SpatialBundle::from_transform(Transform::from_translation(trans)), + RigidBody::Static, + Mesh::from(Sphere::new(0.1)), )) + .with_children(|parent| { + let mesh: Mesh = Sphere::new(0.1).into(); + parent.spawn(PbrBundle { + mesh: meshes.add(mesh), + material: materials.add(wheel_material.clone()), + transform: Transform::from_rotation(rot), + ..Default::default() + }); + }) .id(); - let mut tire_rot = Transform::default(); - tire_rot.rotate_z(FRAC_PI_2); - - let front_tire = commands + let tire = commands .spawn(( - SpatialBundle::from_transform(tire_rot), + SpatialBundle::default(), CyberWheel, RigidBody::Dynamic, collider.clone(), @@ -109,7 +112,7 @@ fn spawn_wheels( )) .with_children(|b| { b.spawn(PbrBundle { - mesh: meshes.add(wheel_mesh.clone()), + mesh: meshes.add(wheel_mesh), material: materials.add(wheel_material.clone()), // transform: Transform::from_translation(front_pos) // .with_rotation(Quat::from_rotation_z(FRAC_PI_2)), @@ -119,7 +122,35 @@ fn spawn_wheels( .id(); // connect hubs and tires to make wheels - commands.spawn(RevoluteJoint::new(front_hub, front_tire).with_aligned_axis(Vec3::X)); + commands.spawn(RevoluteJoint::new(hub, tire).with_aligned_axis(Vec3::X)); +} + +fn spawn_wheels( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, + xform: Transform, + body: Entity, +) { + let rot_x = Quat::from_rotation_x(FRAC_PI_2); + let rot_y = Quat::from_rotation_y(FRAC_PI_2); + let rot_z = Quat::from_rotation_z(FRAC_PI_2); + + let trans0 = Vec3::new(0.0, 0.5, 0.0); + let trans1 = Vec3::new(1.0, 0.5, 0.0); + let trans2 = Vec3::new(2.0, 0.5, 0.0); + + helper(&mut commands, trans0, rot_x, &mut meshes, &mut materials); + helper(&mut commands, trans1, rot_y, &mut meshes, &mut materials); + helper(&mut commands, trans2, rot_z, &mut meshes, &mut materials); + helper( + &mut commands, + Vec3::new(-1.0, 0.5, 0.0), + Quat::default(), + &mut meshes, + &mut materials, + ); + /* // suspension joints connect the hubs and the body commands.spawn(( @@ -127,7 +158,7 @@ fn spawn_wheels( FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + rake_vec), )); - /* + let rear_pos = *xform.back() + xform.translation + rake_vec.y; let rear_hub = commands .spawn((