cyber_rider/src/bike/body.rs
2024-07-13 12:27:36 -07:00

73 lines
2.1 KiB
Rust

use avian3d::prelude::*;
use bevy::{
prelude::{AssetServer, BuildChildren, Commands, Quat, Res, SpatialBundle, Transform, Vec3},
scene::SceneBundle,
};
use super::{spawn_wheels, CyberBikeBody, Meshterial, WheelConfig, BIKE_BODY_COLLISION_GROUP};
use crate::{action::CatControllerState, planet::PLANET_RADIUS};
pub(super) fn spawn_cyberbike(
mut commands: Commands,
asset_server: Res<AssetServer>,
wheel_conf: Res<WheelConfig>,
mut meshterials: Meshterial,
) {
let altitude = PLANET_RADIUS - 10.0;
let mut xform = Transform::from_translation(Vec3::X * altitude)
.with_rotation(Quat::from_axis_angle(Vec3::Z, -89.0f32.to_radians()));
let right = xform.right() * 350.0;
xform.translation += right;
let friction = Friction {
dynamic_coefficient: 0.1,
static_coefficient: 0.6,
combine_rule: CoefficientCombine::Average,
};
let restitution = Restitution {
coefficient: 0.0,
..Default::default()
};
let (membership, filter) = BIKE_BODY_COLLISION_GROUP;
let bike_collision_group = CollisionLayers::new(membership, filter);
let scene = asset_server.load("cb-no-y_up.glb#Scene0");
let spatialbundle = SpatialBundle {
transform: xform,
..Default::default()
};
let body =
Collider::capsule_endpoints(0.5, Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8));
let bike = commands
.spawn(RigidBody::Dynamic)
.insert(spatialbundle)
.insert((
body,
bike_collision_group,
restitution,
friction,
SleepingDisabled,
LinearDamping(0.1),
AngularDamping(2.0),
LinearVelocity::ZERO,
AngularVelocity::ZERO,
ExternalForce::ZERO,
))
.with_children(|rider| {
rider.spawn(SceneBundle {
scene,
..Default::default()
});
})
.insert(CyberBikeBody)
.insert(CatControllerState::default())
.id();
spawn_wheels(&mut commands, bike, &wheel_conf, &mut meshterials);
}