cyber_rider/src/bike/body.rs

78 lines
2.4 KiB
Rust
Raw Normal View History

2025-01-26 01:36:01 +00:00
use avian3d::prelude::{
AngularDamping, AngularVelocity, CoefficientCombine, Collider, ColliderDensity,
CollisionLayers, ExternalTorque, Friction, LinearDamping, LinearVelocity, Restitution,
RigidBody, SleepingDisabled,
};
2023-02-03 01:26:28 +00:00
use bevy::{
2024-07-25 22:49:10 +00:00
core::Name,
2025-01-27 00:45:43 +00:00
prelude::{
AssetServer, BuildChildren, ChildBuild, Commands, Quat, Res, Transform, Vec3, Visibility,
},
scene::SceneRoot,
2023-02-03 01:26:28 +00:00
};
2024-07-18 21:55:09 +00:00
use super::{spawn_wheels, CyberBikeBody, Meshterial, WheelConfig};
use crate::{action::CatControllerState, planet::PLANET_RADIUS, ColliderGroups};
2023-02-03 01:26:28 +00:00
pub(super) fn spawn_cyberbike(
mut commands: Commands,
asset_server: Res<AssetServer>,
wheel_conf: Res<WheelConfig>,
mut meshterials: Meshterial,
) {
2023-02-24 06:08:08 +00:00
let altitude = PLANET_RADIUS - 10.0;
2023-02-03 01:26:28 +00:00
let mut xform = Transform::from_translation(Vec3::X * altitude)
2024-08-04 19:44:22 +00:00
.with_rotation(Quat::from_axis_angle(Vec3::Z, (-89.0f32).to_radians()));
2023-02-03 01:26:28 +00:00
let right = xform.right() * 350.0;
xform.translation += right;
let friction = Friction {
2024-07-12 22:35:37 +00:00
dynamic_coefficient: 0.1,
static_coefficient: 0.6,
combine_rule: CoefficientCombine::Average,
2023-02-03 01:26:28 +00:00
};
let restitution = Restitution {
coefficient: 0.0,
..Default::default()
};
2024-07-18 21:55:09 +00:00
let bike_collision_group =
CollisionLayers::new(ColliderGroups::BikeBody, ColliderGroups::Planet);
2023-02-03 01:26:28 +00:00
let scene = asset_server.load("cb-no-y_up.glb#Scene0");
2024-07-16 00:29:53 +00:00
let body_collider =
2024-07-12 22:35:37 +00:00
Collider::capsule_endpoints(0.5, Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8));
2025-01-27 00:45:43 +00:00
let spatial_bundle = (xform, Visibility::default());
2023-02-03 01:26:28 +00:00
let bike = commands
2025-01-27 00:45:43 +00:00
.spawn(spatial_bundle)
2023-02-03 01:26:28 +00:00
.insert((
2024-07-25 22:49:10 +00:00
Name::new("bike body"),
2024-07-16 00:29:53 +00:00
RigidBody::Dynamic,
body_collider,
2023-02-03 01:26:28 +00:00
bike_collision_group,
restitution,
friction,
2024-07-12 22:35:37 +00:00
SleepingDisabled,
2024-07-16 00:29:53 +00:00
CyberBikeBody,
CatControllerState::default(),
2024-07-25 22:49:10 +00:00
ColliderDensity(20.0),
2024-07-12 22:35:37 +00:00
LinearDamping(0.1),
AngularDamping(2.0),
LinearVelocity::ZERO,
AngularVelocity::ZERO,
2024-07-25 22:49:10 +00:00
ExternalTorque::ZERO.with_persistence(false),
2023-02-03 01:26:28 +00:00
))
.with_children(|rider| {
2025-01-27 00:45:43 +00:00
rider.spawn(SceneRoot(scene));
2023-02-03 01:26:28 +00:00
})
.id();
2024-07-18 21:55:09 +00:00
bevy::log::info!("bike body: {bike:?}");
2024-07-16 05:47:36 +00:00
spawn_wheels(&mut commands, bike, &xform, &wheel_conf, &mut meshterials);
2023-02-03 01:26:28 +00:00
}